-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
14 changed files
with
315 additions
and
99 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,29 @@ | ||
spec = Gem::Specification.new do |s| | ||
s.name = "aejis_backup" | ||
|
||
s.author = "Andrey Savchenko" | ||
s.email = "[email protected]" | ||
s.license = 'BSD' | ||
|
||
s.version = "0.1.0" | ||
s.date = Time.now | ||
|
||
s.homepage = "http://github.com/Aejis/backup" | ||
s.summary = "Swiss knife for unix backups" | ||
s.description = "Gem for easy backup your database and files. Supported databases: Postgresql. Supported storages: Amazon S3, local disk" | ||
|
||
s.files = Dir.glob("{bin,lib,spec}/**/*") + ["README"] | ||
|
||
s.bindir = 'bin' | ||
s.executables = ["backup"] | ||
s.default_executable= 'backup' | ||
|
||
s.require_path = "lib" | ||
|
||
s.test_files = Dir.glob("spec/**/*") | ||
|
||
s.add_development_dependency "rspec", ">= 2.0.0" | ||
s.add_development_dependency "fog", ">= 0.3.20" | ||
|
||
s.requirements = ["GNU or BSD tar"] | ||
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,28 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'optparse' | ||
require 'rubygems' | ||
require 'aejis_backup' | ||
|
||
options = {} | ||
optparse = OptionParser.new do |opts| | ||
opts.banner = "\nUsage: backup [options]\n " | ||
|
||
opts.on('-c', '--config [PATH]', "Path to backup configuration") do |config| | ||
options[:config] = config | ||
end | ||
|
||
opts.on('-b x,y,z', Array, "List of backups to run") do |list| | ||
options[:list] = list | ||
end | ||
|
||
opts.on_tail('-s', '--silent', 'Do not print messages to STDOUT') do | ||
BACKUP_SILENT = true | ||
end | ||
end | ||
|
||
optparse.parse! | ||
|
||
AejisBackup.config_path = options[:config] | ||
AejisBackup.load | ||
AejisBackup.run!(options[:list]) |
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 |
---|---|---|
@@ -1,18 +1,62 @@ | ||
require "fileutils" | ||
require "rubygems" | ||
require "aejis_backup/helpers" | ||
require "aejis_backup/configuration" | ||
|
||
module AejisBackup | ||
|
||
# def config_setter(*names) | ||
# names.each do |name| | ||
# instance_eval %{ | ||
# def #{name}(#{name}=nil) | ||
# #{name} ? (@#{name} = #{name}) : @#{name} | ||
# end | ||
# alias :#{name}= :#{name} | ||
# } | ||
# end | ||
# end | ||
class <<self | ||
include AejisBackup::Helpers | ||
|
||
end | ||
def config_path(path=false) | ||
path ? (@@config_path = path) : @@config_path | ||
end | ||
alias :config_path= :config_path | ||
|
||
require "aejis_backup/configuration" | ||
def config(&block) | ||
return @@config unless block_given? | ||
@@config = AejisBackup::Configuration.new | ||
@@config.instance_eval(&block) | ||
end | ||
|
||
def load | ||
require @@config_path | ||
end | ||
|
||
def run!(*names) | ||
# Do backup for all backup sets if sets not defined | ||
names = config.backups.keys if names.first.nil? | ||
|
||
names.each do |name| | ||
say("Backup '#{name}'", :yellow) | ||
|
||
# Get backup, define file- and dirnames | ||
backup = config.backups[name] | ||
dirname = "backup-#{Time.now.to_i.to_s}" | ||
tmpdir = File.join(backup.tmpdir, dirname) | ||
|
||
# Make temporary directory | ||
# TODO - Create normal exceptions | ||
FileUtils.makedirs(tmpdir) or raise "You have no access to #{backup.tmpdir} directory" | ||
|
||
# Get all defined sources | ||
backup.sources.each do |source_name| | ||
source = config.sources[source_name] | ||
source.get!(File.join(tmpdir, source_name)) | ||
end | ||
|
||
# Pack backups to tar archive | ||
archive = tmpdir + '.tar' | ||
run("tar -C #{backup.tmpdir} -c -f #{archive} #{dirname}", "Create final archive", :green) | ||
|
||
# Store this archive | ||
config.storages[backup.target].store!(archive) | ||
|
||
# Remove temp dirs and report | ||
say("Cleanup", :green) | ||
FileUtils.rm_rf([archive, tmpdir], :secure => true) | ||
say("Done!", :yellow) | ||
end | ||
end | ||
end | ||
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
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 |
---|---|---|
@@ -1,6 +1,57 @@ | ||
module AejisBackup | ||
module Adapter | ||
class Archive < Abstract | ||
class Archive | ||
include AejisBackup::Helpers | ||
|
||
def initialize | ||
config_accessor :path, :format | ||
compression_level 6 | ||
end | ||
|
||
def compression_level(level=false) | ||
return @compression_level unless level | ||
if level.is_a?(Symbol) or level.is_a?(String) | ||
@compression_level = case level.to_sym | ||
when :lowest | ||
1 | ||
when :low | ||
3 | ||
when :middle | ||
5 | ||
when :high | ||
7 | ||
when :highest | ||
9 | ||
end | ||
elsif level.is_a? Numeric | ||
level = level.round | ||
@compression_level = if level < 1 | ||
1 | ||
elsif level > 9 | ||
9 | ||
end | ||
end | ||
end | ||
|
||
def get!(tmpfile) | ||
dir, target = File.dirname(path), File.basename(path) | ||
run("tar -C #{dir} -c #{target} | #{archive(tmpfile)}", "Archiving #{path} with #{format}", :green) | ||
end | ||
|
||
private | ||
|
||
def archive(tmpfile) | ||
case format | ||
when :bz2, :bzip, :bzip2 | ||
"bzip2 -z -#{compression_level} > #{tmpfile}.tar.bz2" | ||
when :gz | ||
"gzip -#{compression_level} > #{tmpfile}.tar.gz" | ||
when :xz | ||
"xz -z -#{compression_level} > #{tmpfile}.tar.xz" | ||
else | ||
"bzip2 -z -#{compression_level} > #{tmpfile}.tar.bz2" | ||
end | ||
end | ||
end | ||
end | ||
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
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,20 @@ | ||
module AejisBackup | ||
class Backup | ||
def initialize | ||
@tmpdir = '/tmp' | ||
end | ||
|
||
def sources(*names) | ||
names.length == 0 ? @sources : (@sources = names) | ||
end | ||
alias :source :sources | ||
|
||
def target(name=false) | ||
name ? (@target = name) : @target | ||
end | ||
|
||
def tmpdir(path=false) | ||
path ? (@tmpdir = path) : @tmpdir | ||
end | ||
end | ||
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
Oops, something went wrong.