Rails:将Backup gem用于本地Postgres数据库

时间:2018-10-02 22:13:16

标签: ruby-on-rails postgresql

我正在尝试建立一种简单的方法来对数据库进行定期备份,以支持live rails 5.2应用程序。

在将其部署到服务器之前,我想确保它首先在本地似乎正常工作。

我运行了backup generate:model --trigger=db_backup --databases='postgresql' --storages='local' --compressor='gzip' --notifiers='mail'来生成db_backup.rb文件

由于从我的credentials.yml.enc文件检索参数时出现问题,我开始对信息进行如下硬编码:

db_backup.rb

# encoding: utf-8
##
# Backup Generated: db_backup
# Once configured, you can run the backup with the following command:
#
# $ backup perform -t db_backup [-c <path_to_configuration_file>]
#
# For more information about Backups components, see the documentation     at:
# http://backup.github.io/backup
#
Model.new(:db_backup, 'Description for db_backup') do

##
# PostgreSQL [Database]
#
database PostgreSQL do |db|
# To dump all databases, set `db.name = :all` (or leave blank)
db.name               = "my_db_name"
db.username           = "postgres"        
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

##
# Local (Copy) [Storage]
#
store_with Local do |local|
  local.path       = "~/backups/"
  local.keep       = 20
  # local.keep       = Time.now - 2592000 # Remove all backups older than 1 month.
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                 = "my email"
  mail.to                   = "my email"
 # 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               = "mail.google.com" 
  mail.user_name            = "my email"
  mail.password             = "my_password!" 
  mail.authentication       = "login"
  mail.encryption           = :starttls
end

end

运行上面的代码(backup perform -t db_backup)会产生错误:

[2018/10/02 21:29:25][info] Database::PostgreSQL Started...
[2018/10/02 21:29:25][info] Using Compressor::Gzip for compression.
[2018/10/02 21:29:25][info]   Command: '/usr/bin/gzip'
[2018/10/02 21:29:25][info]   Ext: '.gz'
[2018/10/02 21:29:25][error] Model::Error: Backup for Description for db_backup (db_backup) Failed!
[2018/10/02 21:29:25][error] --- Wrapped Exception ---
[2018/10/02 21:29:25][error] Database::PostgreSQL::Error: Dump Failed!
[2018/10/02 21:29:25][error]   Pipeline STDERR Messages:
[2018/10/02 21:29:25][error]   (Note: may be interleaved if multiple commands returned error messages)
[2018/10/02 21:29:25][error]
[2018/10/02 21:29:25][error]   pg_dump: [archiver (db)] connection to database "my_db_name" failed: could not connect to server: No such file or directory
[2018/10/02 21:29:25][error]    Is the server running locally and accepting
[2018/10/02 21:29:25][error]    connections on Unix domain socket "/tmp/pg.sock/.s.PGSQL.5432"?
[2018/10/02 21:29:25][error]   The following system errors were returned:
[2018/10/02 21:29:25][error]   Errno::EPERM: Operation not permitted - 'pg_dump' returned exit code: 1

对此问题进行阅读似乎是一个相对普遍的问题,尤其是对于Mac用户而言,但我还没有找到一个明确的解决方案

从命令行运行pg_dump -U postgres -h localhost -p 5432 my_db_name > name_of_backup_file可以成功运行,并在当前文件夹中生成数据库的备份

感谢您的帮助

0 个答案:

没有答案