Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
176 lines (156 sloc) 4.91 KB
#!/usr/bin/env ruby
require 'rubygems'
require 'find'
require 'fileutils'
require 'zlib'
require '/srv/serverAdministration/misc/functions.rb'
require 'right_aws'
require 'yaml'
# Remove warning: "warning: peer certificate won't be verified in this SSL session"
class Net::HTTP
alias_method :old_initialize, :initialize
def initialize(*args)
old_initialize(*args)
@ssl_context = OpenSSL::SSL::SSLContext.new
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
end
def prepare_http
files = []
Find.find('/var/log') do |path|
if FileTest.directory?(path) and path.include?('httpd') and !path.include?(Time.now.strftime("%Y%m%d")) and path.include?('201')
backup_file_name = '/mnt/' + path.split('-')[1] + '-httpd-' + local_ip.to_s + '.tgz'
`tar -cf #{backup_file_name} -C / #{path[1..-1]}`
files.push(backup_file_name)
FileUtils.rm_rf(path)
end
end
files
end
def prepare_squid
files = []
Find.find('/var/log') do |path|
if path.include?('squid') and path.include?('access') and path.include?('gz')
backup_file_name = '/mnt/' + path.split('-')[1].split('.')[0] + '-squid-' + local_ip.to_s + '.gz'
FileUtils.move(path, backup_file_name)
files.push(backup_file_name)
end
if path.include?('squid') and path.include?('gz') and !path.include?('access')
FileUtils.rm(path)
end
end
files
end
def prepare_www
files = []
today = Time.now.strftime("%A")
Dir.chdir('/www/')
Dir.foreach('/www/') do |path|
next if ['.', '..', 'cache'].include?(path)
backup_file_name = '/mnt/' + today + '-' + path + '.tar'
`tar -cf #{backup_file_name} -C / mnt/srv/www/etrade/#{path}`
files.push(backup_file_name)
end
files
end
def prepare_gforcesdata
files = []
today = Time.now.strftime("%A")
Dir.chdir('/ebs/www/')
projects = %w(project1 project2 project3)
projects.each do |path|
puts ' ' + path.to_s
puts ' tar start: ' + Time.now.to_s
backup_file_name = '/mnt/' + today + '-' + path + '.tar'
options=""
case path
when 'project1'
path += '/shared/system'
when 'project2'
path += '/shared/system'
end
`tar -cf #{backup_file_name} -C / ebs/www/#{path} #{options}`
puts ' tar end : ' + Time.now.to_s + ', size: ' + File.size(backup_file_name).to_s
if File.size(backup_file_name) > 5000000000
`split -b 5000000000 #{backup_file_name} #{backup_file_name}-part- && rm #{backup_file_name}`
Dir.glob('/mnt/*-part-*') do |parts|
files.push(parts)
end
else
files.push(backup_file_name)
end
end
files
end
def prepare_db
files = []
today = Time.now.strftime("%A")
`psql -t -l -h localhost -U postgres | awk '{print $1}'`.each do |dbname|
next if ['template0','template1'].include?(dbname.chop)
backup_file_name = '/mnt/' + today + '-' + dbname.chop + '.dump'
`pg_dump -h localhost -U postgres -f #{backup_file_name} #{dbname.chop}`
files.push(backup_file_name)
end
files
end
def prepare_mysql
files = []
today = Time.now.strftime("%A")
`mysql -s -uroot -ppassword -e "show databases"`.each do |dbname|
next if ['Database'].include?(dbname.chop)
backup_file_name = '/mnt/' + today + '-' + dbname.chop + '.dump'
`mysqldump -uroot -ppassword #{dbname.chop} > #{backup_file_name}`
files.push(backup_file_name)
end
files
end
def prepare_svn
files = []
today = Time.now.strftime("%A")
Dir.chdir('/ebs/var/lib/svn')
Dir.foreach('/ebs/var/lib/svn') do |repo|
next if ['.', '..','db2svn'].include?(repo)
backup_file_name = '/mnt/' + today + '-' + repo + '.dump'
`svnadmin dump /ebs/var/lib/svn/#{repo} > #{backup_file_name} -q`
files.push(backup_file_name)
end
files
end
def prepare_mongo
files = []
today = Time.now.strftime("%Y%m%d")
backup_file_name = '/mnt/' + today + '-mongo.dump.tar.bz2'
`mongodump -h localhost -d db -u username -p password -o /tmp/ > /dev/null 2>&1`
`tar -cjf #{backup_file_name} -C /tmp/project .`
FileUtils.rm_rf('/tmp/zubibu')
files.push(backup_file_name)
files
end
def prepare_solr
files = []
today = Time.now.strftime("%Y%m%d")
backup_file_name = '/mnt/' + today + '-solr.tar.gz'
Dir.chdir('/usr/share/apache-solr')
`tar -czf #{backup_file_name} -C /usr/share/apache-solr .`
files.push(backup_file_name)
files
end
def backup(m, module_config,s3)
files = send('prepare_' + m)
files.uniq.each do |file|
s3.put(module_config['bucket'], file.sub('/mnt/', ''), File.open(file))
FileUtils.rm(file)
end
end
begin
config = YAML::load(File.read('/etc/backup.conf'))
rescue
puts 'Configuration file: ' + $!
exit
end
s3 = RightAws::S3Interface.new(config['access_key_id'],config['secret_access_key'], {:multi_thread => false, :logger => Logger.new("/var/log/backup-#{Time.now.strftime("%Y%m%d")}.log")})
puts Time.now.to_s
config['modules'].each {|m, values|
backup(m, values,s3) if values['enabled']
puts m + ' ' + Time.now.to_s
}