Backup a Ruby in Rails app (Gitlab) with Cron job - ruby-on-rails

I tried ti backup my gitlab project (it's based on ROR) with a bash script triggered with a CRON job.
The bash script is ok except the rake migrations wiche returns an error:
gitlabBackUp.sh: 12:
/home/backup/scripts/gitlabBackUp.sh: bundle: not found
Here is the way i did it in my gitlabBackUp.sh
# Export the data
bundle exec rake gitlab:backup:create --trace RAILS_ENV=production
I tried without the bundle exec but it returns
/home/backup/scripts/gitlabBackUp.sh: 14:
/home/backup/scripts/gitlabBackUp.sh: rake: not found
Any tips?
EDIT:
I finally get the cron task get working with:
/usr/local/bin/bundle exec /usr/local/bin/rake gitlab:backup:create --trace RAILS_ENV=production

PATH is probably not well specified in cron. You can:
1) Call (exec) bash with -l e.g. exec bash -l /path/to/real-script.sh
Or perhaps directly in cron like 0 16 * * * /bin/bash -l '/home/backup/scripts/gitlabBackUp.sh'
2) Explicitly specify path for bundle and rake e.g. /usr/local/bin/bundle exec /usr/binrake gitlab:backup:create --trace RAILS_ENV=production
You can always know the location of bundle and rake through which bundle and which rake; or type -P bundle and type -P rake.

Related

Running delayed job using bundle exec

In general, I will start the delayed_job using cmd:
RAILS_ENV=production bin/delayed_job start
Now I wrote a simple upstart script to automatically start the delayed job after reboot.
I found that if I wrote the cmd above in the script, it will fail.
# this is the execution in the upstart conf
exec RAILS_ENV=production bin/delayed_job start
After some google and test, I found this would work in my script:
exec bundle exec /usr/bin/env RAILS_ENV=production bin/delayed_job start
This resulted in my question, I guess /usr/bin/env RAILS_ENV=production is to pass the environment variable into the bin/delay_job script, how about bundle exec here?
Without having bundle exec in the script, it will throw error regarding to
require': cannot load such file -- bundler/setup (LoadError).
I realized that using bundle exec helps run rails under some context of the environment, but why is it necessary in the script?
Or is bundle exec necessary when I want to execute cmd like bin/delayed_job or rake in the script?
Thanks.
updated
I copied the upstart script to another node and executed it the same way.
However, it throws the incompatible library version error...
I run
bundle exec /usr/bin/env RAILS_ENV=production bin/delayed_job start
directly on terminal and it worked as expected, but failed when putting the command above in a script.
Could someone tell me why the result differed when executing the same command but in different place?

How to run a bash script to update a Rails app from outside its directory?

I have this script that should be running from the /~ directory:
#!/bin/bash
APP=/root/apps/monitoring
cd $APP
git pull
rake assets:precompile RAILS_ENV=production
touch $APP/tmp/restart.txt
As you can see, it pulls new commits and updates the assets and restarts Apache. The problem is when it runs the line rake assets:precompile RAILS_ENV=production, It says:
Could not find proper version of rake (12.1.0) in any of the sources
Run `bundle install` to install missing gems.
Which is strange because I am supposed to be inside the app's folder (/root/apps/monitoring) when this command is executed. What I am doing wrong?
You may need to load rvm in the script (https://rvm.io/workflow/scripting) and may be select proper ruby/gemset.
Also you can consider using wrapper for bundle created with rvm wrapper
Please try
#!/bin/bash
APP=/root/apps/monitoring
cd $APP
git pull
bundle exec rake assets:precompile RAILS_ENV=production
touch $APP/tmp/restart.txt
With bundle exec it should work.
I run rake tasks using bash script wrappers. The trick is to use the source command to load in the rvm environment after the task is started
example.sh
#!/bin/bash
# change the directory
cd /home/ubuntu/GSeries
# load rvm ruby
source /home/ubuntu/.rvm/environments/ruby-2.4.2#mygemset
bundle exec rake db:prune_logs RAILS_ENV="production" &>> /home/ubuntu/GSeries/log/prune_logs.log

How to run a rake task regularly with using CRON?

I've written a rake task for my Rails application and I'd need to run this tasks regularly with using CRON.
If I have a URL that I need to ping with CRON, I do it like this:
0 */6 * * * curl https://www.website.com/something
But how to "ping" a rake task?
The application is located in /home/deployer/apps/myapp-production/current and is running on DigitalOceal (Ubuntu server - nginx).
Thanks.
EDIT: This is my command:
0 */6 * * * cd /home/deployer/apps/myapp-production/current && RAILS_ENV=production bundle exec rake db:backup
But the output is:
/bin/sh: 1: bundle: not found
When I run just rake db:backup on my laptop (locally), everything works just well.
Do I have incorrect the path in the CRON task?
EDIT2: When I run the command cd /home/deployer/apps/myapp-production/current && RAILS_ENV=production bundle exec rake db:backup manually from the command line, everything is working, but not from the CRON.
Use whenever gem. It provides nice DSL like:
every :day, :at => '12:20am', :roles => [:app] do
rake "app_server:task"
end

bundle exec rake doesn't run in cron

I did some researching but all cron and bundle exec related didn't cover the problem I am having so excuse me again if this had been discussed.
I am running Ubuntu 13.10 and have a Ruby On Rails app that have few rake tasks that needs to be run on Cron every few minutes or so.
I run a whenever gem, with the help of which, this syntax
every 3.minutes do
rake 'update_balance'
end
transforms to this line in crontab file
0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent'
And when I copy this line exactly
/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent'
and run in it console, it runs perfectly fine and updates several records in database, as supposed.
But when set to cron, I can see it running in /var/log/syslog file, but nothing is really executed.
May 13 13:06:01 sandbox2 CRON[9656]: (root) CMD (/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent')
May 13 13:06:01 sandbox2 CRON[9655]: (CRON) info (No MTA installed, discarding output)
May 13 13:09:01 sandbox2 CRON[9789]: (root) CMD (/bin/bash -l -c 'cd /var/fruby/releases/20140513091404 && RAILS_ENV=production bundle exec rake update_balance --silent')
Even when I add &>/tmp/mycommand.log to crontab command, every next launch of cron command just completely truncates this file, however again, If I launch it manually, it works nicely and leaves me with this output.
2014-05-13T11:11:25Z 10292 TID-2asbo INFO: Sidekiq client with redis options {:url=>"redis://127.0.0.1"}
Sent task for updating 2 users
Any help with this issue is much appreciated.
Thanks.
I've encountered problems like this before due to the fact that cron runs as a different user. With rake in particular, i have to use the full path to rake since the cron user doesn't have the right folder in it's PATH.
So, my cron lines for rake tasks look like this:
30 8 * * 1 cd /ebs/www/apps/myproject/www && /usr/local/bin/rake mailer:send_weekly_expiring_users_reminder RAILS_ENV=production

Rake Task from Crontab?

Stack:
Apache2
Rails 2.3.8
RedHat Linux
Ruby Enterprise 1.8.7
Got the following rake task in my app user's crontab which is meant to pull records into a database table every 15 min:
*/15 * * * * app_user cd /var/www/apps/my_app/current/ && rake thing:do_stuff RAILS_ENV=production
I can see that the cron daemon is running this task in the cron log, but the database table it's supposed to pull records into doesn't change. This task is working without error when I run it manually in the /var/www/apps/my_app/current directory, and pulls records into the table as I expect it to.
I reset the PATH variable in the crontab to reflect using REE, thinking maybe the default path wouldn't jive with /opt/ruby-enterprise...
How do I get this rake task to actually run with cron?
0,15,30,45 * * * * /bin/bash -l -c 'cd /var/www/apps/my_app/current && RAILS_ENV=production bundle exec rake thing:do_stuff --silent'
Try to use full path to rake binary (run in console which rake and replace rake with full path).
For example, if which rake returns the following path:
/Users/bob/.rvm/gems/ruby-1.9.3-p194/bin/rake
You should use the following command to run the rake task:
/Users/bob/.rvm/bin/rvm all do bundle exec rake allocator:snapshot
and I prefer whenever gem for cron jobs in ruby
How to detect if task failed in cron? On fail cron tries to send email. So you can configure postfix to use your smtp settings (from google for example), and add file ~/.forward containing only your email to home directory of user who is running that cronjob in your system.

Resources