Elastic Beanstalk-SQLite数据库

时间:2018-07-23 21:12:55

标签: sqlite amazon-web-services amazon-s3 amazon-ec2 elastic-beanstalk

我在Elastic Beanstalk上托管了一个简单的JavaScript网站。该应用程序的一部分使用SQLite数据库来处理日志记录和分析。

每当将新版本的站点部署到Elastic Beanstalk实例时,它都会破坏先前的版本,从而使我失去SQLite数据库的内容。

有人对此问题有解决方案吗?将SQLite数据库存储在S3存储桶中可以工作吗?

我知道我可以使用RDS数据库,但是我试图避免重写代码。

1 个答案:

答案 0 :(得分: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中,因此它们会在正确的时间运行。这些文件按文件名的字母顺序执行,因此是我选择的名称。

用于将数据库备份到S3的良好Shell脚本:https://github.com/lumerit/s3-shell-backups