RAILS项目自动备份数据库并上传到七牛云

这里用到一个gem,名为backup。坑是有的,不然可能不会有这篇教程。

  • 文档停留在4.0版本,有些新功能没有描述文档
  • 按4.0文档说明,是无法上传到七牛云的(国内主机无法上传到亚玛逊S3存储,国内大多上传到七牛云存储)

以下教程将解决这些问题。

  1. 在服务器root用户下安装Gem, 并指定版本:

    1
    gem install backup -v 5.0.0.beta.1
  2. 初始化执行文件:

    1
    2
    3
    backup generate:model --trigger my_backup \
    --databases="postgresql" --storages="qiniu" \
    --compressor="gzip" --notifiers="mail"
  3. 在root用户根目录下~/Backup/model/my_backup.rb文件,配置一下相关的key:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    # 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 = 5432
    db.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 Credentials
    qiniu.access_key = "my_access_key"
    qiniu.secret_key = "my_secret_key"
    qiniu.bucket = "bucket-name"
    qiniu.keep = 5
    qiniu.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 = true
    mail.on_warning = true
    mail.on_failure = true
    mail.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 = 587
    mail.domain = "your.host.name"
    mail.user_name = "sender@email.com"
    mail.password = "my_password"
    mail.authentication = "plain"
    mail.encryption = :starttls
    end
    end
  4. 保存后,每当要备份时,执行备份的指令:

    1
    $ backup perform --trigger my_backup
  5. 添加cron任务,每天定时自动备份:

    • $ crontab -e指令进入编辑,在文件的最后添加:

      1
      3 8 * * * /bin/bash -l -c '/usr/local/bin/backup perform -t my_backup'

      (以上代码表示,每天上午8点03分执行一次备份指令)

    • 最后保存退出即可。

完成!