Class: Rsyncbackup
- Inherits:
-
Object
- Object
- Rsyncbackup
- Includes:
- Methadone::CLILogging
- Defined in:
- lib/rsyncbackup.rb,
lib/rsyncbackup/version.rb,
lib/rsyncbackup/utilities.rb
Constant Summary collapse
- VERSION =
"1.0.2"- DEFAULT_EXCLUSIONS =
File.('.rsyncbackup.exclusions', ENV['HOME'])
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#backup_dir_name ⇒ Object
returns the directory name for the current backup directory name consists of a time format: YYYY-MM-DDTHH-MM-SS.
-
#build_command ⇒ Object
returns the command string to execute with all parameters set.
- #finalize ⇒ Object
-
#full_target_path ⇒ Object
returns the full target path, including backup directory name.
-
#initialize(opts = {}) ⇒ Rsyncbackup
constructor
A new instance of Rsyncbackup.
-
#last_full_backup ⇒ Object
returns the directory name of the last full backup returns nil otherwise.
-
#rsync_executable ⇒ Object
returns the path to the rsync executable If none found, raises an Exception.
- #run ⇒ Object
-
#strip_trailing_separator_if_any(s) ⇒ Object
Strip the trailing directory separator from the rsync source or target.
-
#temp_target_path ⇒ Object
returns the temporary target path.
Constructor Details
#initialize(opts = {}) ⇒ Rsyncbackup
Returns a new instance of Rsyncbackup.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rsyncbackup.rb', line 11 def initialize(opts={}) = { dry_run: false, exclusions: DEFAULT_EXCLUSIONS, archive: true, one_file_system: true, hard_links: true, human_readable: true, inplace: true, numeric_ids: true, delete: true, rsync_cmd: rsync_executable }.merge(opts) [:source] = strip_trailing_separator_if_any([:source]) [:target] = strip_trailing_separator_if_any([:target]) [:link_dest] ||= last_full_backup if logger.warn? && [:verbose] == true logger.level=Logger::INFO end debug "#{caller[0]}options: #{options.inspect}" end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/rsyncbackup.rb', line 9 def end |
Instance Method Details
#backup_dir_name ⇒ Object
returns the directory name for the current backup directory name consists of a time format: YYYY-MM-DDTHH-MM-SS
53 54 55 |
# File 'lib/rsyncbackup/utilities.rb', line 53 def backup_dir_name @backup_dir_name ||= Time.now.strftime("%FT%H-%M-%S") end |
#build_command ⇒ Object
returns the command string to execute with all parameters set
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rsyncbackup/utilities.rb', line 17 def build_command cmd = [] cmd << [:rsync_cmd] cmd << '--verbose --progress --itemize-changes' if logger.info? cmd << '--archive' if [:archive] cmd << '--one-file-system' if [:one_file_system] cmd << '--hard-links' if [:hard_links] cmd << '--human-readable' if [:human_readable] cmd << '--inplace' if [:inplace] cmd << '--numeric-ids' if [:numeric_ids] cmd << '--delete' if [:delete] cmd << "--exclude-file #{options[:exclusions]}" if File.exist?([:exclusions]) cmd << "--link-dest '#{options[:link_dest]}'" if [:link_dest] cmd << "'#{options[:source]}'" cmd << "'#{temp_target_path}'" cmd.join(' ').tap{|t| debug "#{caller[0]} Command: #{t}" } end |
#finalize ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rsyncbackup.rb', line 66 def finalize incomplete = "#{options[:target]}/.incomplete" complete = "#{options[:target]}/#{backup_dir_name}" if File.exist?(incomplete) && !File.exist?(complete) File.rename(incomplete, complete) end File.open("#{options[:target]}/.lastfull",'w') do |fh| fh.puts backup_dir_name end info "Backup saved in #{options[:target]}/#{backup_dir_name}" end |
#full_target_path ⇒ Object
returns the full target path, including backup directory name
58 59 60 |
# File 'lib/rsyncbackup/utilities.rb', line 58 def full_target_path @full_target_path ||= [:target]+"/"+backup_dir_name end |
#last_full_backup ⇒ Object
returns the directory name of the last full backup returns nil otherwise
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rsyncbackup/utilities.rb', line 40 def last_full_backup lastfull = "#{options[:target]}/.lastfull" if File.exist?(lastfull) last_full_directory = IO.readlines(lastfull).first.chomp else nil end end |
#rsync_executable ⇒ Object
returns the path to the rsync executable If none found, raises an Exception
71 72 73 74 75 |
# File 'lib/rsyncbackup/utilities.rb', line 71 def rsync_executable rsync = `which rsync`.chomp raise "No rsync executable. Are you sure it\'s installed?" if rsync.empty? rsync end |
#run ⇒ Object
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 63 64 |
# File 'lib/rsyncbackup.rb', line 38 def run cmd = build_command info "Rsync command: #{cmd}" if [:dry_run] info "Dry run only" end if File.exist? temp_target_path warn "Preexisting temporary target. Moving it aside." File.rename temp_target_path, "#{temp_target_path}-#{"%0.4d" % Random.rand(1000)}" end Open3.popen3(cmd) do |stdin, stdout, stderr, wait| stdin.close until stdout.eof? info stdout.gets end until stderr.eof? errors = stderr.gets end result = wait.value raise "Command failed. Return code: #{result}\n#{errors}" unless result == 0 end end |
#strip_trailing_separator_if_any(s) ⇒ Object
Strip the trailing directory separator from the rsync source or target.
- s
-
string to strip
81 82 83 84 |
# File 'lib/rsyncbackup/utilities.rb', line 81 def strip_trailing_separator_if_any(s) raise "not a String" unless s.is_a?(String) s = s.gsub(%r{/$},'') end |
#temp_target_path ⇒ Object
returns the temporary target path
63 64 65 |
# File 'lib/rsyncbackup/utilities.rb', line 63 def temp_target_path @temp_target_path ||= [:target]+"/.incomplete" end |