Hoof
: A basic pastebin type thing
Snip
Ruby script to create SSH tunnel and run mysqldump
#!/usr/bin/env ruby require 'date' require 'open3' #create variable for database access db_server = "MYSERVER_IP_ADDRESS" db_user = "MY_DATABASE_USER" db_pass = "MY_DATABASE_USER_PASSWORD" #what day is it? Ymd = Date.today.strftime("%Y%m%d") sql_file = "#{Ymd}_dump.sql" #create an ssh tunnel sshcmd = "ssh -L 3307:127.0.0.1:3306 #{db_server} -N" dumpcmd = "mysqldump -A -u#{db_user} -P3307 -h127.0.0.1 -p#{db_pass} > #{sql_file}" #start the sshcmd puts "starting ssh tunnel..." Open3.pipeline_start(sshcmd) do |threads| #sleep for a bit and give the thread time to connect sleep 10 #get the first thread t = threads[0] puts "ssh tunnel has pid: #{t.pid}" #run the dump command puts "dumping database info" system(dumpcmd) puts "data dump is complete, killing ssh tunnel pid #{t.pid}" #kill the ssh tunnel Process.kill("TERM", t.pid) end #tar the sqlfile tarcmd = "tar -czvf #{sql_file}.tgz #{sql_file}" system(tarcmd) system("rm #{sql_file}")