项目作者: aeke

项目描述 :
:floppy_disk: a simple Firebase database backup tool
高级语言: Python
项目地址: git://github.com/aeke/firebase-backup.git
创建时间: 2016-09-30T11:44:01Z
项目社区:https://github.com/aeke/firebase-backup

开源协议:

下载


Firebase Database Backup Script

This script downloads the entire Firebase database and uploads it as a GZipped JSON file to Firebase Storage.

Prerequisites

  • Python 3.x
  • pyrebase library
  • httplib2 library
  • gz library

Installation

First, ensure you have Python 3 and pip installed on your machine. Then, install the required Python libraries using pip:

  1. pip install -r requirements.txt

Configuration

Update the config dictionary in the script with your Firebase project information:

  1. config = {
  2. "apiKey": "YOUR-API-KEY",
  3. "authDomain": "YOUR-FIREBASE-DOMAIN.firebaseapp.com",
  4. "databaseURL": "https://YOUR-FIREBASE-DOMAIN.firebaseio.com",
  5. "storageBucket": "YOUR-FIREBASE-DOMAIN.appspot.com",
  6. "serviceAccount": "YOUR-CLIENTSECRET-FILE.json"
  7. }

Replace the placeholders with your actual Firebase project information.

Usage

Run the script using Python:

  1. python backup.py

This will:

  1. Initialize the Firebase application.
  2. Download the database as a JSON file.
  3. Compress the JSON file into a GZ file.
  4. Upload the GZ file to Firebase Storage.

Code Explanation

Function Definitions

  • initialize_firebase(config): Initializes the Firebase app and returns database, authentication, and storage objects.
  • get_file_names(): Generates file names for the JSON and GZ files based on the current UTC time.
  • download_database(auth, config, http_client): Downloads the entire database from Firebase using a JWT token for authentication.
  • upload_backup(storage, gzip_name): Uploads the GZipped database backup to Firebase Storage.

Main Script

  1. Initialize Firebase:
    1. db, auth, storage = initialize_firebase(config)
  2. Generate file names:
    1. upload_name, gzip_name = get_file_names()
  3. Download the database:
    1. data = download_database(auth, config, httplib2.Http())
  4. Compress the data:
    1. with gzip.open(gzip_name, 'wb') as f_out:
    2. f_out.write(data)
  5. Upload the backup:
    1. upload_backup(storage, gzip_name)

Logging

The script uses the logging library to log information about its execution. By default, it logs at the INFO level.

License

This project is licensed under the MIT License.