├── requirements.txt ├── README.md ├── LICENSE └── backup-firebase.py /requirements.txt: -------------------------------------------------------------------------------- 1 | boto==2.26.1 2 | requests==2.2.1 3 | argparse==1.2.1 4 | distribute==0.6.34 5 | python-firebase==1.2 6 | wsgiref==0.1.2 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Backup Firebase data to AWS S3 2 | ============================== 3 | 4 | Detailed blog post here: http://www.seanmeadows.com/2014/03/firebase-continuous-backup/ 5 | 6 | Using python, you can automatically (with cron or heroku scheduler) backup all 7 | your Firebase data and store it safely on AWS S3. 8 | 9 | The result is a new .json file in your S3 bucket whenever you run this script. 10 | 11 | Install 12 | ------- 13 | 14 | virtualenv -p python2.7 --distribute venv 15 | source venv/bin/activate 16 | pip install -r requirements.txt 17 | 18 | 19 | Run 20 | --- 21 | 22 | Set the necessary environment variables. Then run: 23 | 24 | python backup-firebase.py 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Sean Meadows 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /backup-firebase.py: -------------------------------------------------------------------------------- 1 | from firebase import firebase 2 | import os 3 | import datetime 4 | import json 5 | import logging 6 | from boto.s3.connection import S3Connection 7 | from boto.s3.key import Key 8 | 9 | ## FIREBASE ACCESS INFO ## 10 | firebase_url = 'https://your-app.firebaseio.com/' #Or get from environment variable 11 | firebase_secret = os.environ['FIREBASE_SECRET'] 12 | firebase_username = 'my-backup-app' #Username is not actually checked on firebase 13 | 14 | ## AWS S3 ACCESS INFO ## 15 | # Do not store keys in version control 16 | s3_key = os.environ['AWS_ACCESS_KEY_ID'] 17 | s3_secret = os.environ['AWS_SECRET_ACCESS_KEY'] 18 | s3_bucket = os.environ['AWS_BUCKET'] 19 | 20 | # Set logging level 21 | logging.basicConfig(level=logging.INFO) 22 | logger = logging.getLogger(__name__) 23 | 24 | def connect_firebase(): 25 | f = firebase.FirebaseApplication(firebase_url, None) 26 | 27 | #Use secret to access ALL data!i 28 | # Adjust based on your access requirements. 29 | f.authentication = firebase.FirebaseAuthentication(firebase_secret, firebase_username, admin=True) 30 | return f 31 | 32 | logger.info('Starting firebase data backup now...') 33 | 34 | #Use UTC time in key name 35 | now = datetime.datetime.utcnow() 36 | name = 'firebase_' + now.strftime('%Y-%m-%d--%H-%M-%S.%f') + '.json' 37 | 38 | f = connect_firebase() 39 | data = f.get('/', None) 40 | 41 | logging.info('Saving data as s3 object %s' % name) 42 | 43 | s3 = S3Connection(s3_key, s3_secret) 44 | bucket = s3.get_bucket(s3_bucket) 45 | k = Key(bucket) 46 | k.key = name 47 | 48 | #Save key with json data to S3 49 | k.set_contents_from_string(unicode(json.dumps(data, ensure_ascii=False))) 50 | 51 | logger.info('Done.') 52 | --------------------------------------------------------------------------------