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
Class Method Details
.clean(opts) ⇒ Object
24 25 26 27 |
# File 'lib/sweety_backy/commander.rb', line 24 def self.clean( opts ) clean_files( opts ) clean_databases( opts ) end |
.clean_databases(opts) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/sweety_backy/commander.rb', line 58 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
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/sweety_backy/commander.rb', line 29 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 |
# 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 = File.join( Dir::tmpdir, "#{File.basename( backup_path, '.tar.gz' )}" ) database_pass = opts[:database_pass].empty? ? '' : "-p'#{opts[:database_pass]}'" SweetyBacky::Utils::command( "mysqldump -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
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/sweety_backy/commander.rb', line 102 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
116 117 118 119 |
# File 'lib/sweety_backy/commander.rb', line 116 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
76 77 78 79 80 81 82 |
# File 'lib/sweety_backy/commander.rb', line 76 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
84 85 86 87 88 89 90 |
# File 'lib/sweety_backy/commander.rb', line 84 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
92 93 94 95 96 97 98 99 100 |
# File 'lib/sweety_backy/commander.rb', line 92 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 |