为了解决这个问题,备份 gem 应该安装在 Linux 机器的 app 容器之外。
备份生成器命令用于生成备份配置文件。
backup generate:model --trigger oktob_database_backup --databases="postgresql" --storages="s3" --compressor="gzip" --notifiers="mail"
它将在 Linux 机器中创建备份配置文件/root/Backup/models/oktob_database_backup.rb
,与 Dokku 或应用容器无关,该文件包含一些与数据库连接、S3 信息和电子邮件帐户相关的必要配置以发送通知。
/root/Backup/models/oktob_database_backup.rb
文件包含:
# encoding: utf-8
##
# Backup Generated: oktob_db_backup
# Once configured, you can run the backup with the following command:
#
# $ backup perform -t oktob_db_backup [-c <path_to_configuration_file>]
#
# For more information about Backup's components, see the documentation at:
# http://backup.github.io/backup
#
Model.new(:oktob_database_backup, 'Oktob Production Database Backup') do
##
# PostgreSQL [Database]
#
database PostgreSQL do |db|
db.name = ENV['DATABASE_NAME']
db.username = ENV['DATABASE_USERNAME']
db.password = ENV['DATABASE_PASSWORD']
db.host = ENV['DATABASE_HOST']
db.port = ENV['DATABASE_PORT']
end
##
# Amazon Simple Storage Service [Storage]
#
store_with S3 do |s3|
# AWS Credentials
s3.access_key_id = ENV['AWS_ACCESS_KEY_ID']
s3.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
s3.region = "us-west-2"
s3.bucket = ENV['AWS_DATABASE_BACKUP_BUCKET_NAME']
s3.path = "/"
end
##
# Gzip [Compressor]
#
compress_with Gzip
##
# Mail [Notifier]
#
# The default delivery method for Mail Notifiers is 'SMTP'.
# See the documentation for other delivery options.
#
notify_by Mail do |mail|
mail.on_success = true
mail.on_warning = true
mail.on_failure = true
mail.from = ENV['EMAIL_ADDRESS']
mail.to = ENV['EMAIL_ADDRESS']
mail.address = "smtp.gmail.com"
mail.port = 587
mail.domain = "oktob.io"
mail.user_name = ENV['SMTP_USERNAME']
mail.password = ENV['SMTP_PASSWORD']
mail.authentication = "plain"
mail.encryption = :starttls
end
end
配置存储在/etc/environment
文件中:
## Database
export DATABASE_NAME=''
export DATABASE_USERNAME=''
export DATABASE_PASSWORD=''
export DATABASE_HOST=''
export DATABASE_PORT=''
## Amazon S3
export AWS_ACCESS_KEY_ID=''
export AWS_SECRET_ACCESS_KEY=''
export AWS_DATABASE_BACKUP_BUCKET_NAME=''
## Email
export EMAIL_ADDRESS=''
export SMTP_USERNAME=''
export SMTP_PASSWORD=''
要手动执行备份,我们可以使用 type backup perform -t oktob_database_backup
,这个命令会做 3 件事:
- 远程连接到 Postgres Dokku 容器并进行数据库转储
- 将 abckup 文件存储到
oktob-database-backup
S3 存储桶中。
- 发送电子邮件通知至
EMAIL_ADDRESS
使用 cron 作业每隔一小时完成一次备份,该作业添加crontab -e
为:
0 * * * * /bin/bash -l -c '/usr/local/rvm/gems/ruby-2.0.0-p647/bin/backup perform -t oktob_database_backup'
我们可以通过在命令行中/usr/local/rvm/gems/ruby-2.0.0-p647/bin/backup
键入来获取零件,以获取路径。which backup
好文章要检查:
每小时生产服务器数据库和文件备份
将 PostgreSQL 从 Rails 项目备份到 Amazon S3