A
A
artem_music2016-10-25 10:00:58
ruby
artem_music, 2016-10-25 10:00:58

Running a ruby ​​script with cron?

I wrote a script that makes a backup of the database and sends it to Google Drive:

def db_backup
  puts "Backing up DB..."
  time = Time.new
  date = time.strftime("%d.%m.%y_%H.%M.%S")

  %x[pg_dump dbuser > ~/backups/db.sql]
  %x[tar czPf ~/backups/db_#{date}.tar.gz ~/backups/db.sql]
  %x[rm ~/backups/db.sql]
  %x[drive upload --file ~/backups/db_#{date}.tar.gz -p 0B-UlAr4zyFN-OUtsQVI0ZVE]
  %x[rm ~/backups/db_#{date}.tar.gz]

  puts "File was uploaded"
end

if __FILE__ == $0
  db_backup
end

When run from the terminal, everything works fine - a dump is created, loaded, etc., but when you try to set up automatic execution of this script, absolutely nothing happens. Here is what I wrote in crontab -e: I
* * * * * ruby /home/username/myapp/db_backup.rb
also tried the following:
* * * * * /bin/bash -l -c 'ruby /home/username/myapp/db_backup.rb'

Unfortunately, to no avail. How to do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
artem_music, 2016-10-25
@artem_music

The solution turned out to be banal: put down the rights to run the script, set the full paths to everything (including the library for working with google drive).
Script:

#!/home/user/.rvm/gems/ruby-2.2.5/wrappers/ruby 
def db_backup 
  puts "Backing up DB..." 
  time = Time.new 
  date = time.strftime("%d.%m.%y_%H.%M.%S") 

  # формируем бэкап 
  %x[pg_dump dbname > ~/backups/db.sql] 
  # сжимаем 
  %x[tar czPf ~/backups/db_#{date}.tar.gz ~/backups/db.sql] 
  # удаляем дамп 
  %x[rm ~/backups/db.sql] 
  # загружаем сжатый бэкап на google drive (FOLDER_ID - идентификатор папки на google drive) 
  %x[/usr/sbin/drive upload --file ~/backups/db_#{date}.tar.gz -p FOLDER_ID] 
  # удаляем сжатый бэкап с сервера 
  %x[rm ~/backups/db_#{date}.tar.gz] 

  puts "File was uploaded" 
end 

if __FILE__ == $0 
  db_backup 
end

Cron:
0 4 * * * /home/user/.rvm/gems/ruby-2.2.5/wrappers/ruby /home/user/myapp/db_backup.rb

R
Roman Mirilaczvili, 2016-10-25
@2ord

Try this:
* * * * * cd $HOME && $HOME /.rvm/gems/ruby-2.2.5/wrappers/ruby $HOME/myapp/db_backup.rb
And it's better to add to the script header
and add the rights to run
A in crontab
* * * * * cd $HOME && $HOME/myapp/db_backup.rb

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question