Class: Backup::Storage
- Inherits:
-
Object
- Object
- Backup::Storage
- Extended by:
- Attribute
- Defined in:
- lib/backup/storage.rb
Instance Attribute Summary collapse
-
#changes ⇒ Object
Returns the value of attribute changes.
-
#config ⇒ Object
Returns the value of attribute config.
-
#name ⇒ Object
Returns the value of attribute name.
-
#subject_prefix ⇒ Object
Returns the value of attribute subject_prefix.
Instance Method Summary collapse
- #backup(server) ⇒ Object
- #backup_mysql ⇒ Object
- #backup_rsync ⇒ Object
- #check_backuped_mysql(target_path, key) ⇒ Object
- #commit_changes ⇒ Object
-
#initialize(name, config) ⇒ Storage
constructor
A new instance of Storage.
- #run_with_changes(shell) ⇒ Object
- #send_mail(message) ⇒ Object
Methods included from Attribute
Constructor Details
#initialize(name, config) ⇒ Storage
Returns a new instance of Storage.
8 9 10 11 |
# File 'lib/backup/storage.rb', line 8 def initialize(name, config) self.name = name self.config = config end |
Instance Attribute Details
#changes ⇒ Object
Returns the value of attribute changes.
6 7 8 |
# File 'lib/backup/storage.rb', line 6 def changes @changes end |
#config ⇒ Object
Returns the value of attribute config.
6 7 8 |
# File 'lib/backup/storage.rb', line 6 def config @config end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/backup/storage.rb', line 6 def name @name end |
#subject_prefix ⇒ Object
Returns the value of attribute subject_prefix.
6 7 8 |
# File 'lib/backup/storage.rb', line 6 def subject_prefix @subject_prefix end |
Instance Method Details
#backup(server) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/backup/storage.rb', line 13 def backup(server) @server = server @server_config = @server.config @backup_path = "#{config.path}/#{@server.name}" @ssh_host = "-p#{@server_config.port || 22} #{@server_config.host}" @scp_host = "-P#{@server_config.port || 22} #{@server_config.host}" @rsync_host = "-e 'ssh -p#{@server_config.port || 22}' #{@server_config.host}" self.changes = [] backup_rsync backup_mysql commit_changes send_mail(changes.join("\n")) end |
#backup_mysql ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/backup/storage.rb', line 42 def backup_mysql target_path = File.join(@backup_path, "mysql") FileUtils.mkdir_p target_path @server_config.mysql.map do |key, mysql| mysql_config = "" mysql_config += " -u#{mysql.user}" if mysql.user mysql_config += " -p#{mysql.password}" if mysql.password mysql_config += " --databases #{mysql.databases.to_a.join(' ')}" if mysql.databases mysql_config += " --tables #{mysql.tables.to_a.join(' ')}" if mysql.tables mysql_config += " #{mysql.}" if mysql. tmpfile = Tempfile.new('mysql.sql') run_with_changes("ssh #{@ssh_host} 'mysqldump #{mysql_config} > #{tmpfile.path}'") && run_with_changes("scp #{@scp_host}:#{tmpfile.path} '#{target_path}/#{key}.sql'") && run_with_changes("ssh #{@ssh_host} 'rm #{tmpfile.path}'") check_backuped_mysql(target_path, key) if config.mysql_check and (mysql.check || mysql.check.nil?) end end |
#backup_rsync ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/backup/storage.rb', line 30 def backup_rsync target_path = File.join(@backup_path, "rsync") FileUtils.mkdir_p target_path @server_config.rsync.to_a.map do |path| remote_path = path.is_a?(Hash) ? path.first[0] : path target_name = File.basename(path.is_a?(Hash) ? path.first[1] : path) run_with_changes("rsync -ravk #{@rsync_host}:#{remote_path.sub(/\/?$/,'/')} '#{File.join(target_path, target_name)}'") end end |
#check_backuped_mysql(target_path, key) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/backup/storage.rb', line 63 def check_backuped_mysql(target_path, key) dbconfig = config.mysql_config self.changes << "DBCheck running -- checking #{target_path}/#{key}.sql #{Time.now}" mysql_command = "mysql -h#{dbconfig[:host]} -u#{dbconfig[:user]} #{dbconfig[:password] ? "-p#{dbconfig[:password]}" : ""}" system("#{mysql_command} -e 'drop database #{dbconfig[:database]};'") system("#{mysql_command} -e 'create database #{dbconfig[:database]};'") status = run_with_changes("#{mysql_command} #{dbconfig[:database]} < #{target_path}/#{key}.sql") ? "SUCCESSFUL" : "FAILURE" self.changes << "DBCheck finished #{status} -- #{Time.now}" end |
#commit_changes ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/backup/storage.rb', line 76 def commit_changes Dir.chdir(@backup_path) do run_with_changes("git init") unless system("git status") run_with_changes("git add .") run_with_changes("git commit -am '#{Time.now.strftime("%Y-%m-%d %H:%M")}'") end end |
#run_with_changes(shell) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/backup/storage.rb', line 84 def run_with_changes(shell) self.changes << "== #{shell}" result = Backup::Main.run(shell) self.subject_prefix = "[ERROR]" unless result self.changes << result result end |
#send_mail(message) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/backup/storage.rb', line 92 def send_mail() Dir.chdir(@backup_path) do smtp_config = config.smtp Mail.defaults { delivery_method :smtp, smtp_config } if smtp_config Backup::Main.email(:from => @server_config.email, :to => @server_config.email, :subject => "#{self.subject_prefix} #{@server.name} backed up at #{Time.now}", :body => , :charset => 'utf-8', :content_type => 'text/plain; charset=utf-8' ) if @server_config.email end end |