Created
April 26, 2012 05:21
-
-
Save ebeigarts/2496192 to your computer and use it in GitHub Desktop.
Backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Backup | |
# | |
# For more information: | |
# | |
# View the Git repository at https://github.com/meskyanichi/backup | |
# View the Wiki/Documentation at https://github.com/meskyanichi/backup/wiki | |
# View the issue log at https://github.com/meskyanichi/backup/issues | |
# | |
# When you're finished configuring this configuration file, | |
# you can run it from the command line by issuing the following command: | |
# | |
# $ ./backup.rb | |
require "rubygems" | |
gem "backup", "3.0.20" | |
gem "net-ssh", "2.1.4" | |
gem "net-scp", "1.0.4" | |
require "socket" | |
require "backup" | |
Backup.send(:const_set, :TRIGGER, "backup") | |
Backup.send(:const_set, :TIME, Time.now.strftime("%Y.%m.%d.%H.%M.%S")) | |
Backup::Logger.send(:const_set, :QUIET, true) | |
Backup.send(:remove_const, :LOG_PATH) | |
Backup.send(:const_set, :LOG_PATH, "/var/log") | |
FileUtils.mkdir_p(Backup::TMP_PATH) | |
FileUtils.mkdir_p(Backup::CACHE_PATH) | |
FileUtils.mkdir_p(Backup::DATA_PATH) | |
FileUtils.mkdir_p(File.join(Backup::DATA_PATH, Backup::TRIGGER)) | |
Backup::Model.new(:backup, 'Backup') do | |
unless `which mongo`.empty? | |
database_names = `echo "show dbs" | mongo --quiet | grep -v "bye" | grep -v "local" | awk '{ print $1 }'`.split("\n") | |
database_names.reject { |database_name| database_name =~ /_(test|dev|development)$/ } | |
database_names.each do |database_name| | |
database Backup::Database::MongoDB do |db| | |
db.name = database_name | |
end | |
end | |
end | |
unless `which mysql`.empty? | |
database_names = `echo "show databases" | mysql | grep -v "Database"`.split("\n") | |
database_names.reject { |database_name| database_name =~ /_(test|dev|development)$/ } | |
database_names.each do |database_name| | |
database Backup::Database::MySQL do |db| | |
db.name = database_name | |
db.socket = '/var/run/mysqld/mysqld.sock' | |
db.additional_options = ['--single-transaction', '--quick'] | |
end | |
end | |
end | |
store_with Backup::Storage::SCP do |server| | |
server.ip = 'office.makit.lv' | |
server.port = 22 | |
server.username = 'backup' | |
server.path = "/var/backups/#{Socket.gethostname}" | |
server.keep = 30 | |
end | |
Dir["/var/www/*"].each do |path| | |
if Dir.exists?("#{path}/shared/uploads") | |
user_name = File.basename(path) | |
sync_with Backup::Syncer::RSync do |rsync| | |
rsync.ip = 'office.makit.lv' | |
rsync.port = 22 | |
rsync.username = 'backup' | |
rsync.path = "/var/backups/#{Socket.gethostname}/www-#{user_name}" | |
rsync.mirror = true | |
rsync.compress = true | |
rsync.directories do |directory| | |
directory.add "#{path}/shared/uploads" | |
end | |
end | |
end | |
end | |
end.perform! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment