Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6f25acb
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
require 'pathname' | ||
require 'escape' | ||
|
||
class Tarsnap | ||
|
||
class << self | ||
|
||
attr_accessor :binary_mapping | ||
|
||
def prefix | ||
@prefix = lookup_default_prefix if !defined?(@prefix) || @prefix.to_s.strip.empty? | ||
Pathname(@prefix) | ||
end | ||
|
||
def prefix=(value) | ||
@prefix = value | ||
end | ||
|
||
def bin_path(name) | ||
name = binary_mapping[name] if name.is_a?(Symbol) | ||
name.include?('/') ? name : prefix.join("bin", name).to_s | ||
end | ||
|
||
def list | ||
names = [] | ||
status, output = exec!(:tarsnap, "--list-archives") | ||
output.each_line { |l| names << l.strip } if status | ||
names | ||
end | ||
|
||
def delete!(name, options = {}) | ||
status, _ = exec!(:tarsnap, "-d", "-f", name, *hash_to_arguments(options)) | ||
status | ||
end | ||
|
||
def create!(name, *files) | ||
options = extract_options(files) | ||
status, _ = exec!(:tarsnap, "-c", "-f", name, *(hash_to_arguments(options) + files)) | ||
status | ||
end | ||
|
||
protected | ||
|
||
def hash_to_arguments(options = {}) | ||
arguments = [] | ||
options.each_pair do |k, v| | ||
case k.to_s | ||
when "cd", "C" | ||
arguments << "-C" | ||
arguments << v.to_s | ||
when "cache_dir", "cache-dir" | ||
arguments << "--cachedir" | ||
arguments << v.to_s | ||
when "aggressive_networking", "aggressive-networking" | ||
arguments << "--#{k ? "" : "no-"}aggressive-networking" | ||
when "l", "check-links", "check_links" | ||
arguments << "--check-links" | ||
when "keyfile", "key_file", "key-file" | ||
arguments << "--keyfile" | ||
arguments << v.to_s | ||
when "dry-run", "dry_run", "dry-run" | ||
arguments << "--dry-run" | ||
end | ||
end | ||
arguments | ||
end | ||
|
||
def exec!(key, *args) | ||
binary = bin_path(key) | ||
command = Escape.shell_command([binary] + args.map { |a| a.to_s }).to_s | ||
output = `#{command}` | ||
return $?.success?, output | ||
end | ||
|
||
def lookup_default_prefix | ||
output = `which tarsnap`.strip | ||
output.empty? ? '/usr/local' : File.dirname(output).gsub("/bin", "") | ||
end | ||
|
||
def extract_options(array) | ||
array.last.is_a?(Hash) ? array.pop : {} | ||
end | ||
|
||
end | ||
|
||
self.binary_mapping ||= {} | ||
binary_mapping[:tarsnap] = "tarsnap" | ||
binary_mapping[:tarsnap_keygen] = "tarsnap-keygen" | ||
binary_mapping[:tarsnap_keymgmt] = "tarsnap-keymgmt" | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class Tarsnap::Backup | ||
|
||
DATE_FORMAT = "%Y%m%d" | ||
FORMAT_REGEXP = /\d{4}\d{2}\d{2}/ | ||
|
||
DEFAULTS = { | ||
:daily => 7, | ||
:weekly => 3, | ||
:monthly => 6 | ||
} | ||
|
||
def initialize(name, options = {}) | ||
@name = name | ||
@options = DEFAULTS.merge(options) | ||
@chdir = options.delete(:cd) | ||
@paths = [] | ||
end | ||
|
||
def add_archive(name) | ||
@paths << "@#{name}" | ||
end | ||
|
||
def add_path(path) | ||
@paths << path.to_s | ||
end | ||
|
||
def create!(date = Time.now) | ||
archive_name = "#{@name}-#{date.utc.strftime(FORMAT_REGEXP)}" | ||
args = generate_files | ||
options = {} | ||
options[:C] = @chdir if !@chdir.to_s.strip.empty? | ||
args << options | ||
Tarsnap.create!(archive_name, *args) | ||
end | ||
|
||
def archives | ||
Tarsnap.list.grep(/^#{Regexp.escape(@name)}\-#{FORMAT_REGEXP}$/) | ||
end | ||
|
||
protected | ||
|
||
def generate_files | ||
@paths.map { |f| f.to_s } | ||
end | ||
|
||
end |