这里用到一个gem,名为backup。坑是有的,不然可能不会有这篇教程。
- 文档停留在4.0版本,有些新功能没有描述文档
- 按4.0文档说明,是无法上传到七牛云的(国内主机无法上传到亚玛逊S3存储,国内大多上传到七牛云存储)
以下教程将解决这些问题。
在服务器root用户下安装Gem, 并指定版本:
1gem install backup -v 5.0.0.beta.1初始化执行文件:
123backup generate:model --trigger my_backup \--databases="postgresql" --storages="qiniu" \--compressor="gzip" --notifiers="mail"在root用户根目录下~/Backup/model/my_backup.rb文件,配置一下相关的key:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374# encoding: utf-8### Backup Generated: my_backup# Once configured, you can run the backup with the following command:## $ backup perform -t my_backup [-c <path_to_configuration_file>]## For more information about Backup's components, see the documentation at:# http://backup.github.io/backup#Model.new(:my_backup, 'Description for my_backup') do### PostgreSQL [Database]#database PostgreSQL do |db|# To dump all databases, set `db.name = :all` (or leave blank)db.name = "my_database_name"db.username = "my_username"db.password = "my_password"db.host = "localhost"db.port = 5432db.socket = "/tmp/pg.sock"# When dumping all databases, `skip_tables` and `only_tables` are ignored.db.skip_tables = ["skip", "these", "tables"]db.only_tables = ["only", "these", "tables"]db.additional_options = ["-xc", "-E=utf8"]end### Qiniu [Storage]#store_with Qiniu do |qiniu|# Qiniu Credentialsqiniu.access_key = "my_access_key"qiniu.secret_key = "my_secret_key"qiniu.bucket = "bucket-name"qiniu.keep = 5qiniu.path = "path/to/backups"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 = truemail.on_warning = truemail.on_failure = truemail.from = "sender@email.com"mail.to = "receiver@email.com"mail.cc = "cc@email.com"mail.bcc = "bcc@email.com"mail.reply_to = "reply_to@email.com"mail.address = "smtp.gmail.com"mail.port = 587mail.domain = "your.host.name"mail.user_name = "sender@email.com"mail.password = "my_password"mail.authentication = "plain"mail.encryption = :starttlsendend保存后,每当要备份时,执行备份的指令:
1$ backup perform --trigger my_backup添加cron任务,每天定时自动备份:
$ crontab -e
指令进入编辑,在文件的最后添加:13 8 * * * /bin/bash -l -c '/usr/local/bin/backup perform -t my_backup'(以上代码表示,每天上午8点03分执行一次备份指令)
最后保存退出即可。