Backup gem not working as expected from crontab - ruby

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!

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

Related

Getting error while running curl command via chef

While running curl command using chef getting error. Block which I am trying to execute:
bash 'create_aemadmin' do
code <<-EOF
curl -u admin:admin -FcreateUser=aemadmin -FauthorizableId=aemadmin -Frep:password=#{node['aem_vm_cookbook']['aem_api_auth']['password']} -Fprofile/familyName=aemadmin http://#{node['aem_vm_cookbook']['host_name']}:8080/libs/granite/security/post/authorizables
EOF
not_if { File.exist?('/home/aemadmin/dce/aemadminusercreate') }
end
Error which i am getting for this:
/bin/chef-client:162:in `<main>'
[2020-09-09T01:48:24-05:00] FATAL: Mixlib::ShellOut::ShellCommandFailed: bash[create_aemadmin] (aem_vm_cookbook::default line 455) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '2'
---- Begin output of "bash" "/tmp/chef-script20200909-12267-2dd1ke" ----
STDOUT:
STDERR: curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
---- End output of "bash" "/tmp/chef-script20200909-12267-2dd1ke" ----
Ran "bash" "/tmp/chef-script20200909-12267-2dd1ke" returned 2
Using this block i am trying to create a user i can see curl is getting values for variable which i am using but it is not able to execute.
Due space issue i was facing this. Now i resolved it

Error running script on Mac OS but runs ok after ssh

I have a .sh script on a Mac OS machine to generate an IPA file using PhoneGap. If I log in the machine via ssh, and then run the script, it runs successfully. But if I try to execute it remotely, by doing an ssh and calling the script as below:
~$ ssh -i ~/.ssh/mykey admin#IP_ADDRESS 'cd /Users/admin/scripts && ./build.sh'
Then I get this error:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- xcodeproj (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from generate_scheme.rb:2:in `<main>'
I have this on the beginning of the .sh file:
#!/bin/bash
PATH=$PATH:/bin:/usr/bin:Users/admin/.rvm/gems/ruby-2.2.4/bin:/Users/admin/.rvm/gems/ruby-2.2.4#global/bin:/Users/admin/.rvm/rubies/ruby-2.2.4/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/admin/.rvm/bin:/Users/admin/.rvm/rubies/ruby-2.2.4/bin/xcodeproj:/Users/admin/.rvm/rubies/ruby-2.2.4/bin/ruby
export PATH
And this is the line that is failing:
ruby generate_scheme.rb
Any help is appreciated, thank you.
Ssh by default establishes a non-login shell and therefore does not pick up whatever environment settings are in place for that user. Specifically, on your system it does not pick up settings for xcodeproj.
Use bash --login to establish a login shell:
ssh -i ~/.ssh/mykey admin#IP_ADDRESS -t 'bash --login -c "cd /Users/admin/scripts;./build.sh"'

Chef Kitchen (kitchen-ec2) serverspec spec failing and causing error

I am learning Chef and the testing process using Kitchen with the ec2 driver and have the following serverspec file:
require "serverspec"
set :backend, :exec
describe "rbenv" do
describe file("/home/ec2-user/.rbenv") do
it { should be_directory }
end
describe command("rbenv versions") do
its(:exit_status) { should eq 0 }
end
end
When I run kitchen verify amazon-linux the first test passes but the second one fails and causes an error:
rbenv
File "/home/ec2-user/.rbenv"
should be directory
Command "rbenv versions"
exit_status
should eq 0 (FAILED - 1)
Failures:
1) rbenv Command "rbenv versions" exit_status should eq 0
Failure/Error: its(:exit_status) { should eq 0 }
expected: 0
got: 127
(compared using ==)
/bin/sh -c rbenv\ versions
# /tmp/verifier/suites/serverspec/rbenv_spec.rb:11:in `block (3 levels) in <top (required)>'
Finished in 0.05481 seconds (files took 0.30304 seconds to load)
2 examples, 1 failure
Failed examples:
rspec /tmp/verifier/suites/serverspec/rbenv_spec.rb:11 # rbenv Command "rbenv versions" exit_status should eq 0
/opt/chef/embedded/bin/ruby -I/tmp/verifier/suites/serverspec -I/tmp/verifier/gems/gems/rspec-support-3.4.1/lib:/tmp/ver
ifier/gems/gems/rspec-core-3.4.4/lib /opt/chef/embedded/bin/rspec --pattern /tmp/verifier/suites/serverspec/\*\*/\*_spec.rb --c
olor --format documentation --default-path /tmp/verifier/suites/serverspec failed
!!!!!! Ruby Script [/tmp/verifier/gems/gems/busser-serverspec-0.5.9/lib/busser/runner_plugin/../serverspec/runner.rb /tm
p/verifier/suites/serverspec] exit code was 1
>>>>>> Verify failed on instance <default-amazon-linux>.
>>>>>> Please see .kitchen/logs/default-amazon-linux.log for more details
>>>>>> ------Exception-------
>>>>>> Class: Kitchen::ActionFailed
>>>>>> Message: SSH exited (1) for command: [sh -c '
BUSSER_ROOT="/tmp/verifier"; export BUSSER_ROOT
GEM_HOME="/tmp/verifier/gems"; export GEM_HOME
GEM_PATH="/tmp/verifier/gems"; export GEM_PATH
GEM_CACHE="/tmp/verifier/gems/cache"; export GEM_CACHE
sudo -E /tmp/verifier/bin/busser test
']
>>>>>> ----------------------
I logged into the server manually and ran the same command /bin/sh -c rbenv\ versions and it worked fine.
Can anyone tell if there's something I'm doing wrong here?
EDIT 1
I just discovered that when serverspec runs the command the $PATH variable is different from what it is when I log in manually. I added puts ENV['PATH'] to the test and the output was this:
/sbin:/bin:/usr/sbin:/usr/bin
However, when I login manually I get this:
/home/ec2-user/.rbenv/shims:/home/ec2-user/.rbenv/bin:/home/ec2-user/.rbenv/shims:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/.local/bin:/home/ec2-user/bin
I changed the command in the test to /home/ec2-user/.rbenv/rbenv versions and it passed. Any idea what I need to do to make the paths work correctly here?
EDIT 2
So I've discovered that serverspec is running the commands as root, which means that the changes to ec2-user's path that took place during kitchen converge are unavailable.
I don't know if there's a more idiomatic way to do this with serverspec, but I changed my command to this and the test now passes:
runuser -l ec2-user -c 'rbenv versions'
This allows root to run a command as another user. Unless someone knows of a better way to do this kind of test I'll add this as the answer to the question.
serverspec runs its commands as root, so changes made to any other user's path during convergence are irrelevant.
Using the Linux runuser command will allow root to run a command as another user, thereby gaining access to that user's $PATH:
runuser -l ec2-user -c 'rbenv versions'

Heroku go-getting-started not running localy

Does anyone know why i'm unable to run the go-getting-started application localy?
go get github.com/heroku/go-getting-started/cmd/...
cd $env:GOPATH/src/github.com/heroku/go-getting-started
PS C:\Users\XXXX\gocode\src\github.com\heroku\go-getting-started> heroku local
[OKAY] Loaded ENV .env File as KEY=VALUE Format
[OKAY] Trimming display Output to 107 Columns
11:28:05 PM web.1 | 'go-getting-started' is not recognized as an internal or external command,
11:28:05 PM web.1 | operable program or batch file.
[DONE] Killing all processes with signal null
11:28:05 PM web.1 Exited Abnormally
PS C:\Users\XXXX\gocode\src\github.com\heroku\go-getting-started> code .
According to the Heroku Dev Center:
If you see an error about an unrecognized command or “command not found”, then $GOPATH/bin is likely not in your $PATH or the trailing ... was missing from the go get github.com/heroku/go-getting-started/.... command used during “Prepare the App”.
Make sure $GOPATH/bin (in your case, C:\Users\XXXX\gocode\bin) is in your $PATH variable. The server binary, go-getting-started, is already there, you just need to make it available to the command line.

Permission denied when piping to a file

I'm trying to pipe the output of a "git pull" command to both a file and stdout using the following script:
STD_OUT=`mktemp`
git pull | $STD_OUT
rm -f $STD_OUT
This results in:
./test.sh: line 2: /tmp/tmp.BITQRbsMSI: Permission denied
error: git-pull died of signal 13
Why am I denied permission to the temp file I just created and what's the alternative?
"Piping to a file" is not a legal operation in bash (or any other shell). The thing following a | has to be a command. If you want to redirect the output from the git operation into the file, use the redirect operator >:
git pull > $STD_OUT

Resources