2

I'm trying to integrate automatic backup mechanism to Rails app and decided to use backup gem. Backup file is valid and runs successfully manually, but when i deployed it on VPS it didn't run by cron at all. Even log files were empty.

schedule.rb:

every :day, :at => '2:00 am' do
  command "backup perform --trigger database_backup --config_file config/backup.rb --log-path log --tmp-path tmp",
          :output => {:error => 'log/cron_backup_error.log', :standard => 'log/cron_backup.log'}
end

crontab -l command results:

0 2 * * * /bin/bash -l -c 'backup perform --trigger database_backup --config_file config/backup.rb --log-path log --tmp-path tmp >> log/cron_backup.log 2>> log/cron_backup_error.log'

If i run any of these 2 commands on VPS manually it will work, but they doesn't work by cron. All other scheduled tasks run fine too.

Appreciate any help you can provide!

Anton Jyha
  • 100
  • 5
  • it's environment variables, Bash probably does not know anything about ruby paths, so you might need to add ruby paths to the top of your crontab file this can be achieved easily if you use `rvm` with `rvm cron setup` – bjhaid Jan 11 '14 at 08:29

3 Answers3

2

Add this to the top of schedule.rb

env :PATH, ENV['PATH']

That will ensure that cron's path is the same as your current one

pguardiario
  • 53,827
  • 19
  • 119
  • 159
2
  1. Try to use this configuration:

    job_type :backup, "cd :path && bundle exec backup perform -t database_backup -c config/backup/config.rb -l tmp/backup/log -d tmp/backup/data --tmp-path=tmp/backup/tmp --cache-path=tmp/backup/cache"
    
    every :day, :at => '2:00 am' do
      backup ""
    end
    

    If you use rvm:

    job_type :backup, "cd :path && rvm 1.9.3 do bundle exec backup perform -t database_backup -c config/backup/config.rb -l tmp/backup/log -d tmp/backup/data --tmp-path=tmp/backup/tmp --cache-path=tmp/backup/cache"
    

    Run after:

    $ whenever -w
    
  2. Try gem backup-rails.

0

How do you run whenever on the server to update the crontab? Are you using Capistrano for deployment?

If you're using Capistrano you should have something like this in your deploy.rb:

set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"

If you're doing it manually, make sure you run it through bundler:

bundle exec whenever ...

You should set up your environment variables as part of your whenever jobs:

whenever --update-crontab app_name --set environment=production

Alternatively you can do this within your whenever script:

set :environment, 'production'

Or per job:

every :day, :at => '2:00 am' do
  command "...", :output => { args_removed }, :environment => 'production' 
end  
Jon
  • 10,678
  • 2
  • 36
  • 48