Search code examples
rubybackup

Backup gem not working as expected from crontab


I have production_backup.rb file in /Users/***/Backup/models.

When I execute following command from terminal, my database backup was successful

  backup perform --trigger production_backup

Now I have a ruby script with following code /Users/***/Backup/my_backup.rb

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

When I execute above code as a ruby script as below, backup was created successfully

  ruby my_backup.rb

Now I am coming to the problem, when I wanted to execute the same ruby file from my crontab, its not working. My crontab contains

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

This job is executed at the specified time but I am getting the following error in my log file i.e.,

/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>'

From @honey suggestion, I have updated crontab to directly execute the command, but now inside the logs I am getting following errors

 [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!

Solution

  • You can execute the command directly from crontab

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

    Place the following code in your /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