【问题标题】:Elastic Beanstalk - SQLite databaseElastic Beanstalk - SQLite 数据库
【发布时间】:2018-12-31 09:47:33
【问题描述】:
我在 Elastic Beanstalk 上托管了一个简单的 javascript 网站。此应用程序的一部分使用 SQLite 数据库来处理日志记录和分析。
每当将站点的新版本部署到 Elastic Beanstalk 实例时,它都会破坏以前的版本,进而丢失我的 SQLite 数据库的内容。
有人对此问题有任何解决方案吗?将 SQLite 数据库存储在 S3 存储桶中是否可行?
我知道我可以使用 RDS 数据库,但我试图避免重写我的代码。
【问题讨论】:
标签:
sqlite
amazon-web-services
amazon-s3
amazon-ec2
amazon-elastic-beanstalk
【解决方案1】:
是的,备份到 S3 是有意义的。您可以使用platform hooks 几乎优雅地执行此操作。在应用的根目录中创建一个.ebextensions 目录,并在其中创建一个名为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).
还有一个叫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
由于它们在/opt/elasticbeanstalk/hooks/(pre|post)init 中的位置,它们将在正确的时间运行。这些文件按文件名的字母顺序执行,因此是我选择的名称。
用于将 DB 备份到 S3 的良好 shell 脚本:https://github.com/lumerit/s3-shell-backups