Answer the question
In order to leave comments, you need to log in
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
* * * * * ruby /home/username/myapp/db_backup.rb
* * * * * /bin/bash -l -c 'ruby /home/username/myapp/db_backup.rb'
Answer the question
In order to leave comments, you need to log in
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
0 4 * * * /home/user/.rvm/gems/ruby-2.2.5/wrappers/ruby /home/user/myapp/db_backup.rb
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 questionAsk a Question
731 491 924 answers to any question