This script downloads the entire Firebase database and uploads it as a GZipped JSON file to Firebase Storage.
- Python 3.x
pyrebase
libraryhttplib2
librarygz
library
First, ensure you have Python 3 and pip installed on your machine. Then, install the required Python libraries using pip:
pip install -r requirements.txt
Update the config
dictionary in the script with your Firebase project information:
config = {
"apiKey": "YOUR-API-KEY",
"authDomain": "YOUR-FIREBASE-DOMAIN.firebaseapp.com",
"databaseURL": "https://YOUR-FIREBASE-DOMAIN.firebaseio.com",
"storageBucket": "YOUR-FIREBASE-DOMAIN.appspot.com",
"serviceAccount": "YOUR-CLIENTSECRET-FILE.json"
}
Replace the placeholders with your actual Firebase project information.
Run the script using Python:
python backup.py
This will:
- Initialize the Firebase application.
- Download the database as a JSON file.
- Compress the JSON file into a GZ file.
- Upload the GZ file to Firebase Storage.
- 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.
- Initialize Firebase:
db, auth, storage = initialize_firebase(config)
- Generate file names:
upload_name, gzip_name = get_file_names()
- Download the database:
data = download_database(auth, config, httplib2.Http())
- Compress the data:
with gzip.open(gzip_name, 'wb') as f_out: f_out.write(data)
- Upload the backup:
upload_backup(storage, gzip_name)
The script uses the logging
library to log information about its execution. By default, it logs at the INFO
level.
This project is licensed under the MIT License.