Module: SweetyBacky::Commander
- Defined in:
- lib/sweety_backy/commander.rb
Class Method Summary collapse
- .clean(opts) ⇒ Object
- .clean_databases(opts) ⇒ Object
- .clean_files(opts) ⇒ Object
- .do_database_backup(database_name, backup_path, opts) ⇒ Object
- .do_files_backup(path, backup_path) ⇒ Object
- .do_md5(path, md5_path) ⇒ Object
- .do_slices(file_path, size) ⇒ Object
- .exists?(path, opts) ⇒ Boolean
- .paths_in(path, opts) ⇒ Object
- .remove_path(path, opts) ⇒ Object
- .tmp_sql_file_path(backup_path) ⇒ Object
Class Method Details
.clean(opts) ⇒ Object
30 31 32 33 |
# File 'lib/sweety_backy/commander.rb', line 30 def self.clean( opts ) clean_files( opts ) clean_databases( opts ) end |
.clean_databases(opts) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/sweety_backy/commander.rb', line 64 def self.clean_databases( opts ) SweetyBacky::Utils.log "cleaning databases on #{opts[:target_path]}/databases/" opts[:databases].each do |database_name| SweetyBacky::Utils.log "cleaning database #{database_name}" [:yearly, :monthly, :weekly, :daily].each do |period| paths_in( "#{opts[:target_path]}/databases/#{database_name}.*.#{period.to_s}.sql.tar.gz", opts ).sort[0..(-1*(opts[period]+1))].each do |file_path| remove_path( file_path, opts ) remove_path( "#{file_path}.md5", opts ) if exists?( "#{file_path}.md5", opts ) end end end end |
.clean_files(opts) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sweety_backy/commander.rb', line 35 def self.clean_files( opts ) SweetyBacky::Utils.log "cleaning files on #{opts[:target_path]}/files/" suffix = opts[:slices_size] ? ".part_*" : "" # suffix support in case of spliting activate suffix_regex = opts[:slices_size] ? /\.part_.*/ : "" # suffix support in case of spliting activate opts[:paths].each do |path| SweetyBacky::Utils.log "cleaning file #{path}" [:yearly, :monthly, :weekly, :daily].each do |period| paths_in( "#{opts[:target_path]}/files/#{SweetyBacky::Utils.namerize( path )}.*.#{period.to_s}.tar.gz#{suffix}", opts ).map do |file_name| file_name.match( "files\/#{SweetyBacky::Utils.namerize( path )}.(\\d{8}).#{period.to_s}.tar.gz#{suffix}" )[1] end.uniq.sort[0..(-1*(opts[period]+1))].each do |date_to_remove| paths_in( "#{opts[:target_path]}/files/#{SweetyBacky::Utils.namerize( path )}.#{date_to_remove}.#{period.to_s}.tar.gz#{suffix}", opts ).each do |file_path| Utils.log( "Removing file: #{file_path}" ) remove_path( file_path, opts ) remove_path( "#{file_path.gsub(suffix_regex, "")}.md5", opts ) if exists?( "#{file_path.gsub(suffix_regex, "")}.md5", opts ) end end end end end |
.do_database_backup(database_name, backup_path, opts) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/sweety_backy/commander.rb', line 10 def self.do_database_backup( database_name, backup_path, opts ) SweetyBacky::Utils.log "doing database backup #{database_name} on #{backup_path}" FileUtils.mkdir_p( File.dirname( backup_path ) ) _tmp_sql_file_path = tmp_sql_file_path(backup_path) database_pass = opts[:database_pass].empty? ? '' : "-p'#{opts[:database_pass]}'" database_host = opts[:database_host].nil? ? '' : "-h#{opts[:database_host]}" database_port = opts[:database_port].nil? ? '' : "-P#{opts[:database_port]}" SweetyBacky::Utils::command( "mysqldump #{database_host} #{database_port} -u#{opts[:database_user]} #{database_pass} #{database_name} > #{_tmp_sql_file_path}" ) SweetyBacky::Utils::command( "tar -cz --same-permissions --file #{backup_path} --directory #{File.dirname(_tmp_sql_file_path)} #{File.basename(_tmp_sql_file_path)}" ) File.delete( _tmp_sql_file_path ) end |
.do_files_backup(path, backup_path) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/sweety_backy/commander.rb', line 3 def self.do_files_backup( path, backup_path ) SweetyBacky::Utils.log "doing files backup of #{path} to #{backup_path}" FileUtils.mkdir_p( File.dirname( backup_path ) ) SweetyBacky::Utils::command( "tar -cz --directory #{path} --same-permissions --file #{backup_path} ." ) end |
.do_md5(path, md5_path) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/sweety_backy/commander.rb', line 108 def self.do_md5( path, md5_path ) digest = Digest::MD5.new(); File.open( path, 'r' ) do |f| f.each_line { |line| digest << line } end result = digest.hexdigest File.open( md5_path, 'w' ) { |f| f.write result } return result end |
.do_slices(file_path, size) ⇒ Object
122 123 124 125 |
# File 'lib/sweety_backy/commander.rb', line 122 def self.do_slices( file_path, size ) SweetyBacky::Utils::command( "split -b #{size}m #{file_path} #{file_path}.part_" ) File.delete( file_path ) end |
.exists?(path, opts) ⇒ Boolean
82 83 84 85 86 87 88 |
# File 'lib/sweety_backy/commander.rb', line 82 def self.exists?( path, opts ) if( opts[:storage_system].to_sym == :s3 ) return SweetyBacky::S3.exists?( path, opts[:s3_opts] ) else return File.exists?( path ) end end |
.paths_in(path, opts) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/sweety_backy/commander.rb', line 90 def self.paths_in( path, opts ) if( opts[:storage_system].to_sym == :s3 ) return SweetyBacky::S3.paths_in( path, opts[:s3_opts] ) else return Dir.glob( path ) end end |
.remove_path(path, opts) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/sweety_backy/commander.rb', line 98 def self.remove_path( path, opts ) if( opts[:storage_system].to_sym == :s3 ) SweetyBacky::Utils.log "cleaning: removing #{opts[:s3_opts][:bucket]}/#{path}" SweetyBacky::S3.delete( path, opts[:s3_opts] ) else SweetyBacky::Utils.log "cleaning: removing #{path}" File.delete( path ) end end |
.tmp_sql_file_path(backup_path) ⇒ Object
26 27 28 |
# File 'lib/sweety_backy/commander.rb', line 26 def self.tmp_sql_file_path(backup_path) File.join( Dir::tmpdir, "#{File.basename( backup_path, '.tar.gz' )}" ) end |