Reputation: 12608
I have a simple javascript site that I am hosting on Elastic Beanstalk. Part of this app uses an SQLite database to handle logging and analytics.
Whenever a new version of the site is deployed to the Elastic Beanstalk instance it destroys the previous version and in turn I lose the contents of my SQLite database.
Does anybody have any solutions to this issue? Would storing the SQLite database in an S3 bucket work?
I know I can use an RDS database but I am trying to avoid rewriting my code.
Upvotes: 4
Views: 2932
Reputation: 4349
Yeah, backing up to S3 makes sense. You can use platform hooks to do this almost elegantly. Create an .ebextensions
directory in the root of your app, and in it, make a file called sqlite_backup.conf
:
files:
/opt/elasticbeanstalk/hooks/preinit/01_sqlite_backup.sh:
mode: "000755"
owner: root
group: root
content: |
#!/bin/sh
# insert shell script which backs up sqlite to s3, something like the following:
# set backup directory variables
SRCDIR='/tmp/s3backups'
DESTDIR='path/to/s3folder'
BUCKET='s3bucket'
NOWDATE=`date +%Y-%m-%d`
sqlite3 test.db ‘.dump’ > $SRCDIR/dbbackup
cd $SRCDIR
tar -czPf $NOWDATE-backup.tar.gz dbbackup
# upload backup to s3
/usr/bin/s3cmd put $SRCDIR/$NOWDATE-backup.tar.gz s3://$BUCKET/$DESTDIR/
# check if these persist across deploys - they shouldn't, but if they do, you don't have to backup to S3 (you also have to worry about filling up the disk).
And another called sqlite_restore.conf
:
files:
/opt/elasticbeanstalk/hooks/postinit/99_sqlite_restore.sh:
mode: "000755"
owner: root
group: root
content: |
#!/bin/sh
# insert shell script which restores sqlite from s3
Due to their placement in /opt/elasticbeanstalk/hooks/(pre|post)init
they will be run at the right time. The files are executed in alphabetic order by filename, hence the names I picked.
Good shell scripts for backing up DBs to S3: https://github.com/lumerit/s3-shell-backups
Upvotes: 5