Permalink
Browse files

version 2.0

  • Loading branch information...
1 parent 3ef3b3a commit d019b4fd5ec722fac992739f390f2277f424a1d4 @fguillen committed Jul 2, 2011
View
5 .gitignore
@@ -0,0 +1,5 @@
+.rvmrc
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
View
1 .rvmrc_example
@@ -0,0 +1 @@
+rvm 1.8.7@sweety_backy
View
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in sweety_backy.gemspec
+gemspec
View
15 Rakefile
@@ -0,0 +1,15 @@
+# require 'bundler/gem_tasks'
+
+require 'rake'
+require 'rake/testtask'
+require 'bundler'
+
+Bundler::GemHelper.install_tasks
+
+task :default => :test
+
+Rake::TestTask.new do |t|
+ t.libs << '.'
+ t.test_files = FileList['test/*_test.rb']
+ t.verbose = true
+end
View
18 bin/sweety_backy.rb
@@ -0,0 +1,18 @@
+require 'benchmark'
+# require 'sweety_backy'
+require File.dirname(__FILE__) + "/../lib/sweety_backy"
+
+if( ARGV[0].nil? )
+ SweetyBacky::Utils.log "use: $ ruby sweety_backy.rb <config_file_path>"
+ exit 1
+end
+
+lapsus_time =
+ Benchmark.realtime do
+ SweetyBacky::Utils.log "--------------------"
+ SweetyBacky::Utils.log "Starting SweetyBacky"
+ sb = SweetyBacky::Runner.new( ARGV[0] )
+ sb.run
+ end
+
+SweetyBacky::Utils.log "SweetyBacky on #{lapsus_time} seconds"
View
12 configuration.example.yml
@@ -1,12 +0,0 @@
-includes: ['/tmp/a', '/tmp/b']
-excludes: ['/tmp/a/exclude', '.cvs*']
-databases: ['application1_db', 'application2_db']
-database_user: root
-database_pass: pass
-path: '/tmp/backup'
-yearly: 2
-monthly: 12
-weekly: 4
-daily: 7
-tar_path: '/bin/tar'
-mysqldump_path: '/usr/bin/mysqldump'
View
12 configuration.test.yml
@@ -1,12 +0,0 @@
-includes: [ "#tmp_dir#/to_back" ]
-excludes: [ "#tmp_dir#/to_back/b", "#tmp_dir#/to_back/c", "*exclude*" ]
-databases: ['test']
-database_user: root
-database_pass:
-path: '#tmp_dir#'
-yearly: 1
-monthly: 4
-weekly: 2
-daily: 5
-tar_path: '/usr/bin/tar'
-mysqldump_path: '/usr/local/bin/mysqldump'
View
4 lib/sweety_backy.rb
@@ -0,0 +1,4 @@
+require "#{File.dirname(__FILE__)}/sweety_backy/version"
+require "#{File.dirname(__FILE__)}/sweety_backy/runner"
+require "#{File.dirname(__FILE__)}/sweety_backy/utils"
+require "#{File.dirname(__FILE__)}/sweety_backy/commander"
View
58 lib/sweety_backy/commander.rb
@@ -0,0 +1,58 @@
+module SweetyBacky
+ module Commander
+ def self.do_files_backup( path, backup_path, opts )
+ 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
+
+ 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]} #{opts[: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
+
+ def self.clear( opts )
+ clear_files( opts )
+ clear_databases( opts )
+ end
+
+ def self.clear_files( opts )
+ SweetyBacky::Utils.log "cleaning files"
+
+ opts[:paths].each do |path|
+ SweetyBacky::Utils.log "cleaning file #{path}"
+
+ [:yearly, :monthly, :weekly, :daily].each do |period|
+ Dir.glob( "#{opts[:backup_path]}/files/#{SweetyBacky::Utils.namerize( path )}.*.#{period.to_s}.*" ).sort[0..(-1*(opts[period]+1))].each do |file_path|
+ File.delete( file_path )
+ end
+ end
+ end
+ end
+
+ def self.clear_databases( opts )
+ SweetyBacky::Utils.log "cleaning databases"
+
+ opts[:databases].each do |database_name|
+ SweetyBacky::Utils.log "cleaning database #{database_name}"
+
+ [:yearly, :monthly, :weekly, :daily].each do |period|
+ Dir.glob( "#{opts[:backup_path]}/databases/#{database_name}.*.#{period.to_s}.*" ).sort[0..(-1*(opts[period]+1))].each do |file_path|
+ File.delete( file_path )
+ end
+ end
+ end
+ end
+
+ end
+end
View
62 lib/sweety_backy/runner.rb
@@ -0,0 +1,62 @@
+require 'rubygems'
+require 'fileutils'
+require 'tmpdir'
+require 'yaml'
+require File.dirname(__FILE__) + "/utils.rb"
+
+module SweetyBacky
+ class Runner
+ attr_reader :opts
+
+ def initialize( path = nil )
+ if( !path.nil? )
+ config( SweetyBacky::Utils.read_opts( path ) )
+ end
+ end
+
+ def config( opts )
+ @opts = {
+ :paths => [],
+ :databases => [],
+ :yearly => 1,
+ :monthly => 1,
+ :weekly => 2,
+ :daily => 4,
+ :tar_path => '/usr/bin/tar',
+ :mysqldump_path => '/usr/bin/mysqldump'
+ }.merge( opts )
+ end
+
+ def do_backup
+ do_files_backup
+ do_databases_backup
+ end
+
+ def do_files_backup
+ @opts[:paths].each do |path|
+ backup_path = "#{@opts[:backup_path]}/files/#{SweetyBacky::Utils.namerize( path )}.#{Date.today.strftime('%Y%m%d')}.#{SweetyBacky::Utils.period}.tar.gz"
+ SweetyBacky::Commander.do_files_backup( path, backup_path, @opts )
+ end
+ end
+
+ def do_databases_backup
+ @opts[:databases].each do |database_name|
+ backup_path = "#{@opts[:backup_path]}/databases/#{database_name}.#{Date.today.strftime('%Y%m%d')}.#{SweetyBacky::Utils.period}.sql.tar.gz"
+ SweetyBacky::Commander.do_database_backup( database_name, backup_path, @opts)
+ end
+ end
+
+ def run
+ begin
+ do_backup
+ SweetyBacky::Commander.clear( @opts )
+ rescue => e
+ SweetyBacky::Utils.log "ERROR: #{e}"
+ SweetyBacky::Utils.log "I should send and email at this moment"
+ end
+ end
+
+
+ end
+end
+
View
0 lib/sweety_backy/sweety_backy_execute.rb
No changes.
View
62 lib/sweety_backy/utils.rb
@@ -0,0 +1,62 @@
+module SweetyBacky
+ class Utils
+
+ def self.period
+ return 'yearly' if( is_last_day_of_year? )
+ return 'monthly' if( is_last_day_of_month? )
+ return 'weekly' if( is_last_day_of_week? )
+ return 'daily'
+ end
+
+ def self.is_last_day_of_year?
+ return Date.new(Date.today.year, 12, -1) == Date.today
+ end
+
+ def self.is_last_day_of_month?
+ return Date.new(Date.today.year, Date.today.month, -1) == Date.today
+ end
+
+ def self.is_last_day_of_week?
+ return Date.today.cwday == 7
+ end
+
+ def self.command( _command )
+ Utils.log "command: #{_command}"
+
+ result = %x( #{_command} 2>&1 )
+
+ raise "ERROR: on command: '#{_command}', result: '#{result}'" if $?.exitstatus != 0
+
+ return result
+ end
+
+ def self.log( msg )
+ puts "#{Time.now.strftime("%Y-%m-%d %H:%M")}: #{msg}"
+ end
+
+ def self.namerize( path )
+ path.gsub('/', '.').gsub(/^\./, '')
+ end
+
+ def self.read_opts( conf_path )
+ SweetyBacky::Utils::log "conf_path: #{conf_path}"
+
+ opts = YAML.load( File.read( conf_path ) )
+
+ # symbolize keys
+ opts.keys.each do |key|
+ opts[key.to_sym] = opts.delete(key)
+ end
+
+ SweetyBacky::Utils::log "configuration:"
+ SweetyBacky::Utils::log "------------"
+ opts.each_pair do |key, value|
+ SweetyBacky::Utils::log "#{key}: #{(value.instance_of? Array) ? value.join(' | ') : value}"
+ end
+
+ # TODO: test all options are ok
+
+ return opts
+ end
+ end
+end
View
3 lib/sweety_backy/version.rb
@@ -0,0 +1,3 @@
+module SweetyBacky
+ VERSION = "0.0.1"
+end
View
24 sweety_backy.gemspec
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "sweety_backy"
+
+Gem::Specification.new do |s|
+ s.name = "sweety_backy"
+ s.version = SweetyBacky::VERSION
+ s.authors = ["Fernando Guillen"]
+ s.email = ["fguillen.mail@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "SweetyBacky"
+
+ s.add_development_dependency "bundler", ">= 1.0.0.rc.6"
+ s.add_development_dependency "mocha"
+ s.add_development_dependency "delorean"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
View
82 sweety_backy.rb
@@ -1,82 +0,0 @@
-require 'rubygems'
-require 'fileutils'
-require 'tmpdir'
-require 'yaml'
-require File.dirname(__FILE__) + "/utils.rb"
-
-class SweetyBacky
-
- def initialize( conf_path )
- @opts = Utils::read_opts( conf_path )
- end
-
- def do_backup
- block = ""
- if( Utils::is_last_day_of_year? )
- block = 'yearly'
- elsif( Utils::is_last_day_of_month? )
- block = 'monthly'
- elsif( Utils::is_last_day_of_week? )
- block = 'weekly'
- else
- block = 'daily'
- end
-
- # files
- path = "#{@opts[:path]}/files/#{Date.today.strftime('%Y%m%d')}.#{block}.tar.gz"
- self.do_files_backup( path )
-
- # databases
- path = "#{@opts[:path]}/databases/#{Date.today.strftime('%Y%m%d')}.#{block}.sql.tar.gz"
- self.do_databases_backup( path )
- end
-
- def do_files_backup( path )
- Utils::log "doing files backup"
-
- FileUtils.mkdir_p( File.dirname( path ) )
- exclude_file_path = File.join( Dir::tmpdir, "#{Time.now.to_i}_exclude.txt" )
- File.open( exclude_file_path, 'w' ) { |f| f.write @opts[:excludes].join("\n") }
- Utils::command( "#{@opts[:tar_path]} -cz --same-permissions --file #{path} --exclude-from #{exclude_file_path} #{@opts[:includes].join(' ')}" )
- File.delete( exclude_file_path )
- end
-
- def do_databases_backup( path )
- Utils::log "doing databases backup"
-
- FileUtils.mkdir_p( File.dirname( path ) )
- tmp_sql_file_path = File.join( Dir::tmpdir, "#{File.basename( path, '.tar.gz' )}" )
- database_pass = (@opts[:database_pass].nil? || @opts[:database_pass]=='') ? '' : "-p'#{@opts[:database_pass]}'"
- Utils::command( "#{@opts[:mysqldump_path]} -u#{@opts[:database_user]} #{database_pass} --databases #{@opts[:databases].join(' ')} > #{tmp_sql_file_path}" )
- Utils::command( "#{@opts[:tar_path]} -cz --same-permissions --file #{path} --directory #{File.dirname(tmp_sql_file_path)} #{File.basename(tmp_sql_file_path)}" )
- File.delete( tmp_sql_file_path )
- end
-
- def clear
- Utils::log "cleaning"
-
- [:yearly, :monthly, :weekly, :daily].each do |block|
- Dir.glob( "#{@opts[:path]}/files/*.#{block.to_s}.*" ).sort[0..(-1*(@opts[block]+1))].each do |file_path|
- File.delete( file_path )
- end
-
- Dir.glob( "#{@opts[:path]}/databases/*.#{block.to_s}.*" ).sort[0..(-1*(@opts[block]+1))].each do |file_path|
- File.delete( file_path )
- end
- end
- end
-
- def run
- begin
- do_backup
- clear
- rescue => e
- Utils::log "ERROR: #{e}"
- Utils::log "I should send and email at this moment"
- end
- end
-
-
-end
-
-
View
17 sweety_backy_execute.rb
@@ -1,17 +0,0 @@
-require 'benchmark'
-require File.dirname(__FILE__) + "/sweety_backy.rb"
-
-if( ARGV[0].nil? )
- Utils::log "use: $ ruby sweety_backy_execute.rb <config_file_path>"
- exit 1
-end
-
-lapsus_time =
- Benchmark.realtime do
- Utils::log "--------------------"
- Utils::log "Starting SweetyBacky"
- sb = SweetyBacky.new( ARGV[0] )
- sb.run
- end
-
-Utils::log "SweetyBacky on #{lapsus_time} seconds"
View
179 sweety_backy_test.rb
@@ -1,179 +0,0 @@
-require 'rubygems'
-require 'test/unit'
-require 'mocha'
-require 'delorean'
-require File.dirname(__FILE__) + "/sweety_backy.rb"
-
-class SweetyBackyTest < Test::Unit::TestCase
-
- def setup
- # Moching log
- Utils.stubs(:log)
-
- # tmp dir
- @tmp_dir = File.join( Dir::tmpdir, "sweety_backy_#{Time.now.to_i}" )
- Dir.mkdir( @tmp_dir )
-
- # prepare test folders
- Dir.mkdir( @tmp_dir + "/to_back/" )
- %w( a b c d ).each do |dir|
- Dir.mkdir( @tmp_dir + "/to_back/#{dir}" )
- File.open( @tmp_dir + "/to_back/#{dir}/wadus.txt", 'w' ) { |f| f.write 'wadus' }
- end
- File.open( @tmp_dir + "/to_back/a/exclude.txt", 'w' ) { |f| f.write 'wadus' }
-
- # configuration file
- File.open( File.dirname(__FILE__) + "/configuration.test.tmp.yml", 'w' ) do |f|
- f.write File.read( File.dirname(__FILE__) + "/configuration.test.yml" ).gsub( "#tmp_dir#", @tmp_dir )
- end
-
- # SweetyBacky
- @sb = SweetyBacky.new( File.dirname(__FILE__) + "/configuration.test.tmp.yml" )
- end
-
- def teardown
- FileUtils.rm_rf @tmp_dir if File.exists?(@tmp_dir)
- File.delete( File.dirname(__FILE__) + "/configuration.test.tmp.yml" ) if File.exists?( File.dirname(__FILE__) + "/configuration.test.tmp.yml" )
- end
-
- def test_do_files_backup
- @sb.do_files_backup( "#{@tmp_dir}/back.tar.gz" )
-
- result = %x(tar -tzvf #{@tmp_dir}/back.tar.gz)
-
- puts result
-
- assert_match( "to_back/a/wadus.txt", result )
- assert_match( "to_back/d/wadus.txt", result )
- assert_no_match(/to_back\/b\/wadus.txt/, result )
- assert_no_match(/to_back\/c\/wadus.txt/, result )
- assert_no_match(/to_back\/a\/exclude.txt/, result )
- end
-
- def test_do_databases_backup
- @sb.do_databases_backup( "#{@tmp_dir}/back.sql.tar.gz" )
-
- result = %x(tar -tzvf #{@tmp_dir}/back.sql.tar.gz)
- assert_match( /\sback.sql$/, result )
- end
-
- def test_do_backup_yearly
- Delorean.time_travel_to( '2009-12-31' ) do
- @sb.do_backup
- end
-
- assert( File.exists?( "#{@tmp_dir}/files/20091231.yearly.tar.gz") )
- assert( File.exists?( "#{@tmp_dir}/databases/20091231.yearly.sql.tar.gz") )
- end
-
- def test_do_backup_monthly
- Delorean.time_travel_to( '2010-01-31' ) do
- @sb.do_backup
- end
-
- assert( File.exists?( "#{@tmp_dir}/files/20100131.monthly.tar.gz") )
- assert( File.exists?( "#{@tmp_dir}/databases/20100131.monthly.sql.tar.gz") )
- end
-
- def test_do_backup_weekly
- Delorean.time_travel_to( '2010-01-03' ) do
- @sb.do_backup
- end
-
- assert( File.exists?( "#{@tmp_dir}/files/20100103.weekly.tar.gz") )
- assert( File.exists?( "#{@tmp_dir}/databases/20100103.weekly.sql.tar.gz") )
- end
-
- def test_do_backup_daily
- Delorean.time_travel_to( '2010-01-04' ) do
- @sb.do_backup
- end
-
- assert( File.exists?( "#{@tmp_dir}/files/20100104.daily.tar.gz") )
- assert( File.exists?( "#{@tmp_dir}/databases/20100104.daily.sql.tar.gz") )
- end
-
- def test_clear
- Dir.mkdir( "#{@tmp_dir}/files" ) unless File.exists?( "#{@tmp_dir}/files" )
- Dir.mkdir( "#{@tmp_dir}/databases" ) unless File.exists?( "#{@tmp_dir}/databases" )
-
- [
- '20081231.yearly',
- '20091231.yearly',
- '20100131.monthly',
- '20100228.monthly',
- '20100331.monthly',
- '20100430.monthly',
- '20100531.monthly',
- '20100630.monthly',
- '20100704.weekly',
- '20100711.weekly',
- '20100718.weekly',
- '20100725.weekly',
- '20100720.daily',
- '20100721.daily',
- '20100722.daily',
- '20100723.daily',
- '20100724.daily',
- '20100726.daily'
- ].each do |file_part|
- File.open( "#{@tmp_dir}/files/#{file_part}.tar.gz", 'w' ) { |f| f.write 'wadus' }
- File.open( "#{@tmp_dir}/databases/#{file_part}.sql.tar.gz", 'w' ) { |f| f.write 'wadus' }
- end
-
- # puts @tmp_dir
- # exit 1
-
- @sb.clear
-
- files_keeped = Dir.glob( "#{@tmp_dir}/files/*" ).join( "\n" )
- databases_keeped = Dir.glob( "#{@tmp_dir}/databases/*" ).join( "\n" )
-
- # files to keep
- [
- '20091231.yearly',
- '20100331.monthly',
- '20100430.monthly',
- '20100531.monthly',
- '20100630.monthly',
- '20100718.weekly',
- '20100725.weekly',
- '20100721.daily',
- '20100722.daily',
- '20100723.daily',
- '20100724.daily',
- '20100726.daily'
- ].each do |file_part|
- assert_match( "#{file_part}.tar.gz", files_keeped )
- assert_match( "#{file_part}.sql.tar.gz", databases_keeped )
- end
-
- # files to deleted
- [
- '20081231.yearly',
- '20100131.monthly',
- '20100228.monthly',
- '20100704.weekly',
- '20100711.weekly',
- '20100720.daily'
- ].each do |file_part|
- assert_no_match( /#{file_part}.tar.gz/, files_keeped )
- assert_no_match( /#{file_part}.sql.tar.gz/, databases_keeped )
- end
- end
-
- def test_run
- @sb.expects(:do_backup)
- @sb.expects(:clear)
- @sb.run
- end
-
- def test_error_on_command
- assert_raise RuntimeError do
- Utils::command( 'command_not_exists' )
- end
- end
-
-
-end
-
View
130 test/commander_test.rb
@@ -0,0 +1,130 @@
+require "#{File.dirname(__FILE__)}/test_helper"
+
+class SweetyBackyTest < Test::Unit::TestCase
+
+ def setup
+ SweetyBacky::Utils.stubs(:log)
+
+ # tmp dir
+ @tmp_dir = File.join( Dir::tmpdir, "sweety_backy_#{Time.now.to_i}" )
+ Dir.mkdir( @tmp_dir )
+ end
+
+ def teardown
+ FileUtils.rm_rf @tmp_dir if File.exists?(@tmp_dir)
+ end
+
+ def test_do_files_backup
+ SweetyBacky::Commander.do_files_backup(
+ "#{FIXTURES_PATH}/path",
+ "#{@tmp_dir}/back.tar.gz",
+ {}
+ )
+
+ result = %x(tar -tzvf #{@tmp_dir}/back.tar.gz)
+
+ assert_match( "./", result )
+ assert_match( "./file1.txt", result )
+ assert_match( "./a/", result )
+ assert_match( "./b/file3.txt", result )
+ end
+
+ def test_do_databases_backup
+ SweetyBacky::Commander.do_database_backup(
+ "test",
+ "#{@tmp_dir}/back.sql.tar.gz",
+ {
+ :database_user => "test",
+ :database_pass => ""
+ }
+ )
+
+ result = %x(tar -tzvf #{@tmp_dir}/back.sql.tar.gz)
+
+ assert_match( /\sback.sql$/, result )
+ end
+
+
+ def test_clear
+ opts = {
+ :paths => [ 'name1', 'name2' ],
+ :databases => [ 'name1', 'name2' ],
+ :yearly => 1,
+ :monthly => 2,
+ :weekly => 3,
+ :daily => 4,
+ :backup_path => @tmp_dir
+ }
+
+ Dir.mkdir( "#{@tmp_dir}/files" ) unless File.exists?( "#{@tmp_dir}/files" )
+ Dir.mkdir( "#{@tmp_dir}/databases" ) unless File.exists?( "#{@tmp_dir}/databases" )
+
+ [
+ 'name1.20081231.yearly',
+ 'name1.20081232.yearly',
+ 'name2.20091231.yearly',
+ 'name1.20100131.monthly',
+ 'name1.20100228.monthly',
+ 'name1.20100331.monthly',
+ 'name2.20100430.monthly',
+ 'name2.20100531.monthly',
+ 'name2.20100630.monthly',
+ 'name1.20100704.weekly',
+ 'name1.20100711.weekly',
+ 'name1.20100718.weekly',
+ 'name1.20100725.weekly',
+ 'name1.20100720.daily',
+ 'name2.20100721.daily',
+ 'name2.20100722.daily',
+ 'name2.20100723.daily',
+ 'name2.20100724.daily',
+ 'name2.20100726.daily'
+ ].each do |file_part|
+ File.open( "#{@tmp_dir}/files/#{file_part}.tar.gz", 'w' ) { |f| f.write 'wadus' }
+ File.open( "#{@tmp_dir}/databases/#{file_part}.sql.tar.gz", 'w' ) { |f| f.write 'wadus' }
+ end
+
+ # puts @tmp_dir
+ # exit 1
+
+ SweetyBacky::Commander.clear( opts )
+
+ files_keeped = Dir.glob( "#{@tmp_dir}/files/*" ).join( "\n" )
+ databases_keeped = Dir.glob( "#{@tmp_dir}/databases/*" ).join( "\n" )
+
+ # files to keep
+ [
+ 'name1.20081232.yearly',
+ 'name2.20091231.yearly',
+ 'name1.20100228.monthly',
+ 'name1.20100331.monthly',
+ 'name2.20100531.monthly',
+ 'name2.20100630.monthly',
+ 'name1.20100718.weekly',
+ 'name1.20100725.weekly',
+ 'name1.20100720.daily',
+ 'name2.20100722.daily',
+ 'name2.20100723.daily',
+ 'name2.20100724.daily',
+ 'name2.20100726.daily'
+ ].each do |file_part|
+ assert_match( "#{file_part}.tar.gz", files_keeped )
+ assert_match( "#{file_part}.sql.tar.gz", databases_keeped )
+ end
+
+ # files to deleted
+ [
+ 'name1.20081231.yearly',
+ 'name1.20100131.monthly',
+ 'name2.20100430.monthly',
+ 'name1.20100704.weekly',
+ 'name2.20100721.daily'
+ ].each do |file_part|
+ assert_no_match( /#{file_part}.tar.gz/, files_keeped )
+ assert_no_match( /#{file_part}.sql.tar.gz/, databases_keeped )
+ end
+ end
+
+
+end
+
View
9 test/fixtures/config.yml
@@ -0,0 +1,9 @@
+paths: [ "path1", "path2" ]
+databases: [ "db1", "db2" ]
+yearly: 1
+monthly: 2
+weekly: 3
+daily: 4
+backup_path: '/backup_path'
+database_user: 'database_user'
+database_pass: 'database_pass'
View
1 test/fixtures/path/a/file2.txt
@@ -0,0 +1 @@
+file2
View
1 test/fixtures/path/b/file3.txt
@@ -0,0 +1 @@
+file3
View
1 test/fixtures/path/file1.txt
@@ -0,0 +1 @@
+file1
View
91 test/runner_test.rb
@@ -0,0 +1,91 @@
+require "#{File.dirname(__FILE__)}/test_helper"
+
+class RunnerTest < Test::Unit::TestCase
+
+ def setup
+ SweetyBacky::Utils.stubs(:log)
+
+ # tmp dir
+ @tmp_dir = File.join( Dir::tmpdir, "sweety_backy_#{Time.now.to_i}" )
+ Dir.mkdir( @tmp_dir )
+
+ # runner
+ @opts = {
+ :paths => [ "#{FIXTURES_PATH}/path" ],
+ :databases => [ "test" ],
+ :yearly => 1,
+ :monthly => 1,
+ :weekly => 2,
+ :daily => 4,
+ :backup_path => @tmp_dir,
+ :database_user => 'test',
+ :database_pass => ''
+ }
+
+ @runner = SweetyBacky::Runner.new
+ @runner.config( @opts )
+ end
+
+ def teardown
+ FileUtils.rm_rf @tmp_dir if File.exists?(@tmp_dir)
+ end
+
+ def test_do_backup_yearly
+ Delorean.time_travel_to( '2009-12-31' ) do
+ @runner.do_backup
+ end
+
+ assert( File.exists?( "#{@tmp_dir}/files/#{SweetyBacky::Utils.namerize( @opts[:paths][0] )}.20091231.yearly.tar.gz") )
+ assert( File.exists?( "#{@tmp_dir}/databases/test.20091231.yearly.sql.tar.gz") )
+ end
+
+ def test_do_backup_monthly
+ Delorean.time_travel_to( '2010-01-31' ) do
+ @runner.do_backup
+ end
+
+ assert( File.exists?( "#{@tmp_dir}/files/#{SweetyBacky::Utils.namerize( @opts[:paths][0] )}.20100131.monthly.tar.gz") )
+ assert( File.exists?( "#{@tmp_dir}/databases/test.20100131.monthly.sql.tar.gz") )
+ end
+
+ def test_do_backup_weekly
+ Delorean.time_travel_to( '2010-01-03' ) do
+ @runner.do_backup
+ end
+
+ assert( File.exists?( "#{@tmp_dir}/files/#{SweetyBacky::Utils.namerize( @opts[:paths][0] )}.20100103.weekly.tar.gz") )
+ assert( File.exists?( "#{@tmp_dir}/databases/test.20100103.weekly.sql.tar.gz") )
+ end
+
+ def test_do_backup_daily
+ Delorean.time_travel_to( '2010-01-04' ) do
+ @runner.do_backup
+ end
+
+ assert( File.exists?( "#{@tmp_dir}/files/#{SweetyBacky::Utils.namerize( @opts[:paths][0] )}.20100104.daily.tar.gz") )
+ assert( File.exists?( "#{@tmp_dir}/databases/test.20100104.daily.sql.tar.gz") )
+ end
+
+ def test_run
+ @runner.expects(:do_backup)
+ SweetyBacky::Commander.expects(:clear)
+
+ @runner.run
+ end
+
+ def test_initialize_with_config_file
+ runner = SweetyBacky::Runner.new( "#{FIXTURES_PATH}/config.yml" )
+
+ assert_equal( [ "path1", "path2" ], runner.opts[:paths] )
+ assert_equal( [ "db1", "db2" ], runner.opts[:databases] )
+ assert_equal( 1, runner.opts[:yearly] )
+ assert_equal( 2, runner.opts[:monthly] )
+ assert_equal( 3, runner.opts[:weekly] )
+ assert_equal( 4, runner.opts[:daily] )
+ assert_equal( '/backup_path', runner.opts[:backup_path] )
+ assert_equal( 'database_user', runner.opts[:database_user] )
+ assert_equal( 'database_pass', runner.opts[:database_pass] )
+ end
+
+end
+
View
9 test/test_helper.rb
@@ -0,0 +1,9 @@
+require "#{File.dirname(__FILE__)}/../lib/sweety_backy"
+
+require 'rubygems'
+require 'test/unit'
+require 'mocha'
+require 'delorean'
+
+
+FIXTURES_PATH = File.expand_path "#{File.dirname(__FILE__)}/fixtures"
View
39 test/utils_test.rb
@@ -0,0 +1,39 @@
+require "#{File.dirname(__FILE__)}/test_helper"
+
+class UtilsTest < Test::Unit::TestCase
+ def setup
+ SweetyBacky::Utils.stubs(:log)
+ end
+
+ def test_error_on_command
+ assert_raise RuntimeError do
+ SweetyBacky::Utils::command( 'command_not_exists' )
+ end
+ end
+
+ def test_namerize
+ assert_equal(
+ 'Users.fguillen.Develop.Ruby.SweetyBacky.test.fixtures.path',
+ SweetyBacky::Utils.namerize( '/Users/fguillen/Develop/Ruby/SweetyBacky/test/fixtures/path' )
+ )
+
+ assert_equal( 'path', SweetyBacky::Utils.namerize( '/path' ) )
+ assert_equal( 'path', SweetyBacky::Utils.namerize( 'path' ) )
+ end
+
+ def test_read_opts
+ opts = SweetyBacky::Utils.read_opts( "#{FIXTURES_PATH}/config.yml" )
+
+ assert_equal( [ "path1", "path2" ], opts[:paths] )
+ assert_equal( [ "db1", "db2" ], opts[:databases] )
+ assert_equal( 1, opts[:yearly] )
+ assert_equal( 2, opts[:monthly] )
+ assert_equal( 3, opts[:weekly] )
+ assert_equal( 4, opts[:daily] )
+ assert_equal( '/backup_path', opts[:backup_path] )
+ assert_equal( 'database_user', opts[:database_user] )
+ assert_equal( 'database_pass', opts[:database_pass] )
+ end
+
+
+end
View
49 utils.rb
@@ -1,49 +0,0 @@
-class Utils
- def self.is_last_day_of_year?
- return Date.new(Date.today.year, 12, -1) == Date.today
- end
-
- def self.is_last_day_of_month?
- return Date.new(Date.today.year, Date.today.month, -1) == Date.today
- end
-
- def self.is_last_day_of_week?
- return Date.today.cwday == 7
- end
-
- def self.command( _command )
- Utils::log "command: #{_command}"
- result = %x( #{_command} 2>&1 )
-
- raise "ERROR: on command: '#{_command}', result: '#{result}'" if $?.exitstatus != 0
-
- return result
- end
-
- def self.read_opts( conf_path )
- Utils::log "conf_path: #{conf_path}"
- raw_config = File.read( conf_path )
- opts = YAML.load(raw_config)
-
- # symbolize keys
- opts.keys.each do |key|
- opts[key.to_sym] = opts.delete(key)
- end
-
-
- Utils::log "configuration:"
- Utils::log "------------"
- opts.each_pair do |key, value|
- Utils::log "#{key}: #{(value.instance_of? Array) ? value.join(' | ') : value}"
- end
-
-
- # TODO: test all options are ok
-
- return opts
- end
-
- def self.log( msg )
- puts "#{Time.now.strftime("%Y-%m-%d %H:%M")}: #{msg}"
- end
-end

0 comments on commit d019b4f

Please sign in to comment.