Reg -db backup command using ssh remote


#1

Hi,
I ve execute this code, to take a backup of mysql db in remote machine,but it shows error
undefined method `[]’ for nil:NilClass

require 'net/ssh’
require ‘mysql’

ruby_block “Transfer bits” do
block do
HOST = node[‘trans’][‘host’]
USER = node[‘trans’][‘username’]
PASSWORD = "#{node[‘trans’][‘password’]}"
Net::SSH.start( HOST, USER, :password => PASSWORD )do |ssh|

    ssh.exec("mysqldump -u#{node['magentobits']['login_user']} -p#{node['magentobits']['login_password']} --databases -B magento > magentodb.sql")

end
end
end

Error:

Starting Chef Client, version 11.4.4
172.16.1.121
172.16.1.121 resolving cookbooks for run list: [“trans”]
172.16.1.121
172.16.1.121 Synchronizing Cookbooks:
172.16.1.121
172.16.1.121 - trans
172.16.1.121
172.16.1.121 Compiling Cookbooks…
172.16.1.121
172.16.1.121 172.16.1.122
172.16.1.121
172.16.1.121 Converging 1 resources
172.16.1.121
172.16.1.121 Recipe: trans::default
172.16.1.121
172.16.1.121 * ruby_block[Transfer bits] action run
172.16.1.121
172.16.1.121
172.16.1.121 ================================================================================
172.16.1.121
172.16.1.121 Error executing action run on resource 'ruby_block[Transfer bits]'
172.16.1.121
172.16.1.121 ================================================================================
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121 NoMethodError
172.16.1.121
172.16.1.121 -------------
172.16.1.121
172.16.1.121 undefined method []' for nil:NilClass 172.16.1.121 172.16.1.121 172.16.1.121 172.16.1.121 172.16.1.121 Cookbook Trace: 172.16.1.121 172.16.1.121 --------------- 172.16.1.121 172.16.1.121 /var/chef/cache/cookbooks/trans/recipes/default.rb:47:inblock (3 levels) in from_file’
172.16.1.121
172.16.1.121 /var/chef/cache/cookbooks/trans/recipes/default.rb:40:in block (2 levels) in from_file' 172.16.1.121 172.16.1.121 172.16.1.121 172.16.1.121 172.16.1.121 Resource Declaration: 172.16.1.121 172.16.1.121 --------------------- 172.16.1.121 172.16.1.121 # In /var/chef/cache/cookbooks/trans/recipes/default.rb 172.16.1.121 172.16.1.121 172.16.1.121 29: ruby_block "Transfer bits" do 172.16.1.121 172.16.1.121 30: puts node['trans']['host'] 172.16.1.121 172.16.1.121 31: #command = Array["cd /var/www","mysqldump -u#{node['magentobits']['login_user']} -p#{node['magentobits']['login_password']} --databases -B magento > magentodb.sql"] 172.16.1.121 172.16.1.121 32: 172.16.1.121 172.16.1.121 33: 172.16.1.121 172.16.1.121 34: 172.16.1.121 172.16.1.121 35: #puts command.class 172.16.1.121 172.16.1.121 36: block do 172.16.1.121 172.16.1.121 37: HOST = node['trans']['host'] 172.16.1.121 172.16.1.121 38: USER = node['trans']['username'] 172.16.1.121 172.16.1.121 39: PASSWORD = "#{node['trans']['password']}" 172.16.1.121 172.16.1.121 40: Net::SSH.start( HOST, USER, :password => PASSWORD )do |ssh| 172.16.1.121 172.16.1.121 41: #puts "connection" 172.16.1.121 172.16.1.121 42: #ssh.exec('c')ssh.exec('mkdir sam1') 172.16.1.121 172.16.1.121 43: #command.each do |cmd| 172.16.1.121 172.16.1.121 44: #puts cmd, "hiiiiiiiiii" 172.16.1.121 172.16.1.121 45: #ssh.exec("cd /var/www; mysqldump -u#{node['magentobits']['login_user']} -p#{node['magentobits']['login_password']} --databases -B magento > magentodb.sql; mysql -u#{node['magentobits']['login_user']} -p#{node['magentobits']['login_password']} --databases -B bitnami_wordpress > wordpressdb.sql; mkdir Magentobits_V2; mv magentodb.sql Magentobits_V2; mv wordpressdb.sql Magentobits_V2; cp wordpress Magentobits_V2; tar cvzf Magentobits_V2.tar.gz /var/www") 172.16.1.121 172.16.1.121 46: #puts cmd 172.16.1.121 172.16.1.121 47: ssh.exec("mysqldump -u#{node['magentobits']['login_user']} -p#{node['magentobits']['login_password']} --databases -B magento > magentodb.sql") 172.16.1.121 172.16.1.121 48: 172.16.1.121 172.16.1.121 49: 172.16.1.121 172.16.1.121 50: #ssh.scp.download! node['trans']['remotepath'] , node['trans']['localpath'] 172.16.1.121 172.16.1.121 51: #end 172.16.1.121 172.16.1.121 52: end 172.16.1.121 172.16.1.121 53: end 172.16.1.121 172.16.1.121 54: end 172.16.1.121 172.16.1.121 172.16.1.121 172.16.1.121 172.16.1.121 172.16.1.121 172.16.1.121 Compiled Resource: 172.16.1.121 172.16.1.121 ------------------ 172.16.1.121 172.16.1.121 # Declared in /var/chef/cache/cookbooks/trans/recipes/default.rb:29:infrom_file’
172.16.1.121
172.16.1.121
172.16.1.121 ruby_block(“Transfer bits”) do
172.16.1.121
172.16.1.121 action "run"
172.16.1.121
172.16.1.121 retries 0
172.16.1.121
172.16.1.121 retry_delay 2
172.16.1.121
172.16.1.121 block_name "Transfer bits"
172.16.1.121
172.16.1.121 cookbook_name "trans"
172.16.1.121
172.16.1.121 recipe_name "default"
172.16.1.121
172.16.1.121 block #Proc:0x00000002d46208@/var/chef/cache/cookbooks/trans/recipes/default.rb:36
172.16.1.121
172.16.1.121 end
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121 Chef Client failed. 0 resources updated


#2

It looks like the node you’re running this recipe on doesn’t have
‘magentobits’ as an attribute. This means that node[‘magentobits’] is nil.
Which in turn means that node[‘magentobits’][‘login_user’] is the same as
nil[‘login_user’] which throws an error.

I would look wherever you’re defining your magentobits login_user and
password, and make sure that they’re defined and available at compile-time
(or use lazy attribute evaluation).

Matt Moretti

On Wed, Jul 30, 2014 at 7:33 AM, Indra k indra.k@cloudenablers.com wrote:

Hi,
I ve execute this code, to take a backup of mysql db in remote
machine,but it shows error
undefined method `[]’ for nil:NilClass

require 'net/ssh’
require 'mysql’
ruby_block “Transfer bits” do
block do
HOST = node[‘trans’][‘host’]
USER = node[‘trans’][‘username’]
PASSWORD = "#{node[‘trans’][‘password’]}"
Net::SSH.start( HOST, USER, :password => PASSWORD )do |ssh|

    ssh.exec("mysqldump -u#{node['magentobits']['login_user']}

-p#{node[‘magentobits’][‘login_password’]} --databases -B magento >
magentodb.sql")

end
end
end

Error:

Starting Chef Client, version 11.4.4
172.16.1.121
172.16.1.121 resolving cookbooks for run list: [“trans”]
172.16.1.121
172.16.1.121 Synchronizing Cookbooks:
172.16.1.121
172.16.1.121 - trans
172.16.1.121
172.16.1.121 Compiling Cookbooks…
172.16.1.121
172.16.1.121 172.16.1.122
172.16.1.121
172.16.1.121 Converging 1 resources
172.16.1.121
172.16.1.121 Recipe: trans::default
172.16.1.121
172.16.1.121 * ruby_block[Transfer bits] action run
172.16.1.121
172.16.1.121
172.16.1.121

172.16.1.121
172.16.1.121 Error executing action run on resource 'ruby_block[Transfer
bits]'
172.16.1.121
172.16.1.121

172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121 NoMethodError
172.16.1.121
172.16.1.121 -------------
172.16.1.121
172.16.1.121 undefined method []' for nil:NilClass 172.16.1.121 172.16.1.121 172.16.1.121 172.16.1.121 172.16.1.121 Cookbook Trace: 172.16.1.121 172.16.1.121 --------------- 172.16.1.121 172.16.1.121 /var/chef/cache/cookbooks/trans/recipes/default.rb:47:inblock (3 levels) in from_file’
172.16.1.121
172.16.1.121 /var/chef/cache/cookbooks/trans/recipes/default.rb:40:in
`block (2 levels) in from_file’
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121 Resource Declaration:
172.16.1.121
172.16.1.121 ---------------------
172.16.1.121
172.16.1.121 # In /var/chef/cache/cookbooks/trans/recipes/default.rb
172.16.1.121
172.16.1.121
172.16.1.121 29: ruby_block “Transfer bits” do
172.16.1.121
172.16.1.121 30: puts node[‘trans’][‘host’]
172.16.1.121
172.16.1.121 31: #command = Array[“cd /var/www”,“mysqldump
-u#{node[‘magentobits’][‘login_user’]}
-p#{node[‘magentobits’][‘login_password’]} --databases -B magento >
magentodb.sql”]
172.16.1.121
172.16.1.121 32:
172.16.1.121
172.16.1.121 33:
172.16.1.121
172.16.1.121 34:
172.16.1.121
172.16.1.121 35: #puts command.class
172.16.1.121
172.16.1.121 36: block do
172.16.1.121
172.16.1.121 37: HOST = node[‘trans’][‘host’]
172.16.1.121
172.16.1.121 38: USER = node[‘trans’][‘username’]
172.16.1.121
172.16.1.121 39: PASSWORD = "#{node[‘trans’][‘password’]}"
172.16.1.121
172.16.1.121 40: Net::SSH.start( HOST, USER, :password => PASSWORD )do
|ssh|
172.16.1.121
172.16.1.121 41: #puts "connection"
172.16.1.121
172.16.1.121 42: #ssh.exec(‘c’)ssh.exec(‘mkdir sam1’)
172.16.1.121
172.16.1.121 43: #command.each do |cmd|
172.16.1.121
172.16.1.121 44: #puts cmd, "hiiiiiiiiii"
172.16.1.121
172.16.1.121 45: #ssh.exec("cd /var/www; mysqldump
-u#{node[‘magentobits’][‘login_user’]}
-p#{node[‘magentobits’][‘login_password’]} --databases -B magento >
magentodb.sql; mysql -u#{node[‘magentobits’][‘login_user’]}
-p#{node[‘magentobits’][‘login_password’]} --databases -B bitnami_wordpress

wordpressdb.sql; mkdir Magentobits_V2; mv magentodb.sql Magentobits_V2;
mv wordpressdb.sql Magentobits_V2; cp wordpress Magentobits_V2; tar cvzf
Magentobits_V2.tar.gz /var/www")
172.16.1.121
172.16.1.121 46: #puts cmd
172.16.1.121
172.16.1.121 47: ssh.exec(“mysqldump
-u#{node[‘magentobits’][‘login_user’]}
-p#{node[‘magentobits’][‘login_password’]} --databases -B magento >
magentodb.sql”)
172.16.1.121
172.16.1.121 48:
172.16.1.121
172.16.1.121 49:
172.16.1.121
172.16.1.121 50: #ssh.scp.download! node[‘trans’][‘remotepath’] ,
node[‘trans’][‘localpath’]
172.16.1.121
172.16.1.121 51: #end
172.16.1.121
172.16.1.121 52: end
172.16.1.121
172.16.1.121 53: end
172.16.1.121
172.16.1.121 54: end
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121 Compiled Resource:
172.16.1.121
172.16.1.121 ------------------
172.16.1.121
172.16.1.121 # Declared in
/var/chef/cache/cookbooks/trans/recipes/default.rb:29:in `from_file’
172.16.1.121
172.16.1.121
172.16.1.121 ruby_block(“Transfer bits”) do
172.16.1.121
172.16.1.121 action "run"
172.16.1.121
172.16.1.121 retries 0
172.16.1.121
172.16.1.121 retry_delay 2
172.16.1.121
172.16.1.121 block_name "Transfer bits"
172.16.1.121
172.16.1.121 cookbook_name "trans"
172.16.1.121
172.16.1.121 recipe_name "default"
172.16.1.121
172.16.1.121 block #<Proc:0x00000002d46208@
/var/chef/cache/cookbooks/trans/recipes/default.rb:36>
172.16.1.121
172.16.1.121 end
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121
172.16.1.121 Chef Client failed. 0 resources updated