备份 gem 未按预期从 crontab 运行

Backup gem not working as expected from crontab

我在 /Users/***/Backup/models 中有 production_backup.rb 文件。

当我从终端执行以下命令时,我的数据库备份成功了

  backup perform --trigger production_backup

现在我有一个 ruby 脚本,代码如下 /Users/***/Backup/my_backup.rb

  cmd = "backup perform --trigger production_backup"
  exec cmd

当我将上面的代码作为如下 ruby 脚本执行时,备份已成功创建

  ruby my_backup.rb

现在我遇到了问题,当我想从我的 crontab 执行相同的 ruby 文件时,它不起作用。 我的 crontab 包含

 33 13 * * * /usr/bin/ruby /Users/***/Backup/my_backup.rb >> /Users/***/Backup/cronoutput.log 2>&1

此作业在指定时间执行,但我在日志文件中收到以下错误,即

/Users/***/Backup/my_backup.rb:4:in `exec': No such file or directory - backup (Errno::ENOENT)
from /Users/***/Backup/my_backup.rb:4:in `<main>'

根据@honey 的建议,我更新了 crontab 以直接执行命令,但现在在日志中我收到以下错误

 [2021/04/02 08:47:03][error] Model::Error: Backup for backup (production_backup) Failed!
 [2021/04/02 08:47:03][error] --- Wrapped Exception ---
 [2021/04/02 08:47:03][error] Archive::Error: Failed to Create Archive 'app_archive'
 [2021/04/02 08:47:03][error]   Pipeline STDERR Messages:
 [2021/04/02 08:47:03][error]   (Note: may be interleaved if multiple commands returned error messages)
 [2021/04/02 08:47:03][error]   
 [2021/04/02 08:47:03][error]   tar: /Users/***/Desktop/my_backups: Couldn't visit directory: Unknown error: -1
 [2021/04/02 08:47:03][error]   tar: Error exit delayed from previous errors.
 [2021/04/02 08:47:03][error]   The following system errors were returned:
 [2021/04/02 08:47:03][error]   Errno::EPERM: Operation not permitted - 'tar' returned exit code: 1
 [2021/04/02 08:47:03][error] 
 [2021/04/02 08:47:03][error] Backtrace:
 [2021/04/02 08:47:03][error]   /Users/***/.rvm/rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/backup-5.0.0.beta.1/lib/backup/archive.rb:95:in `perform!'
 [2021/04/02 08:47:03][error]   /Users/***/.rvm/rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/backup- 
 5.0.0.beta.1/lib/backup/model.rb:301:in `each'
 [2021/04/02 08:47:03][error]   /Users/***/.rvm/rubies/ruby- 
 2.7.2/lib/ruby/gems/2.7.0/gems/backup-5.0.0.beta.1/lib/backup/model.rb:301:in `block in perform!'
 [2021/04/02 08:47:03][error]   /Users/***/.rvm/rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/backup-5.0.0.beta.1/lib/backup/model.rb:300:in `each'
 [2021/04/02 08:47:03][error]   /Users/***/.rvm/rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/backup-5.0.0.beta.1/lib/backup/model.rb:300:in `perform!'
 [2021/04/02 08:47:03][error]   /Users/***/.rvm/rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/backup-5.0.0.beta.1/lib/backup/cli.rb:155:in `perform'
 [2021/04/02 08:47:03][error]   /Users/***/.rvm/rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
 [2021/04/02 08:47:03][error]   /Users/***/.rvm/rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
 [2021/04/02 08:47:03][error]   /Users/***/.rvm/rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
 [2021/04/02 08:47:03][error]   /Users/***/.rvm/rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/thor-0.20.3/lib/thor/base.rb:466:in `start'
 [2021/04/02 08:47:03][error]   /Users/***/.rvm/rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/backup-5.0.0.beta.1/bin/backup:5:in `<top (required)>'
 [2021/04/02 08:47:03][error]   /Users/***/.rvm/rubies/ruby-2.7.2/bin/backup:23:in `load'
 [2021/04/02 08:47:03][error]   /Users/***/.rvm/rubies/ruby-2.7.2/bin/backup:23:in `<main>'
 [2021/04/02 08:47:03][warn] Cleaner::Error: Cleanup Warning
 [2021/04/02 08:47:03][warn]   The temporary packaging folder still exists!
 [2021/04/02 08:47:03][warn]   '/Users/***/Backup/.tmp/production_backup'
 [2021/04/02 08:47:03][warn]   This folder may contain completed Archives and/or Database backups.
 [2021/04/02 08:47:03][warn]   
 [2021/04/02 08:47:03][warn]   Make sure you check these files before the next scheduled backup for
 [2021/04/02 08:47:03][warn]   'backup '
 [2021/04/02 08:47:03][warn]   These files will be removed at that time!

可以直接从crontab执行命令

 33 13 * * * /bin/bash -l -c 'backup perform -t production_backup' >> /Users/***/Backup/cronoutput.log 2>&1

将以下代码放入您的 /Users/***/Backup/config.rb

Utilities.configure do
  tar       '/usr/bin/tar'
  # redis_cli '/opt/redis/redis-cli'
  tar_dist :gnu
  pg_dump '/opt/homebrew/bin/pg_dump'
end