Skip to content

Instantly share code, notes, and snippets.

@Grafikart Grafikart/backup.sh
Created Jul 7, 2015

Embed
What would you like to do?
Backup.sh (ruby)
#!/usr/bin/env ruby
require 'date'
servers = {
"hostname" => "directory_name",
"hostname" => "directory_name",
"hostname" => "directory_name",
"hostname" => "directory_name",
"hostname" => "directory_name"
}
excludes = ['information_schema', 'apsc', 'horde', 'mysql', 'psa', 'roundcubemail', 'sitebuilders', 'sitebuilder5']
root = "/home/backup"
date = Date.today()
servers.each do |server, hostname|
directory = File.join(root, hostname)
if !Dir.exists?(directory)
puts "Creation du dossier #{directory}"
Dir.mkdir(directory)
Dir.mkdir(File.join(directory, 'sql'))
end
if date.monday?
suffix = "weekly-" + (date.day.to_f / 7).ceil.to_s
else
suffix = "daily-" + date.strftime('%u')
end
puts "##############################"
puts "## Sauvegarde de #{hostname} ##"
puts "##############################"
system "rsync -avz --exclude-from 'exclude-list.txt' --delete root@#{server}:/var/www/vhosts/#{server}/www #{File.join(directory, suffix)}"
puts "##############################"
puts "## Sauvegarde des bases de donnees"
puts "##############################"
databases = %x[ ssh root@#{server} 'mysql -uadmin -p`cat /etc/psa/.psa.shadow` -e "show databases" -s --skip-column-names' ]
databases.lines.each do |database|
database.gsub! /\n/,''
if !excludes.include?(database) && !database.include?('phpmyadmin')
puts %x[ ssh root@#{server} 'mysqldump -uadmin -p`cat /etc/psa/.psa.shadow` #{database} | gzip -fc > /tmp/dump.sql.gz' ]
puts %x[ scp root@#{server}:/tmp/dump.sql.gz #{File.join(directory, "sql", database + "-" + suffix + ".sql.gz")} ]
puts %x[ ssh root@#{server} rm /tmp/dump.sql.gz ]
puts "== db == : #{database}-#{suffix}.sql.gz"
end
end
puts "##############################"
puts "## Done ###"
puts "##############################"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.