backup 1.3.4 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +121 -53
- data/Rakefile +3 -8
- data/VERSION +1 -1
- data/backup.gemspec +20 -34
- data/generators/backup/backup_generator.rb +31 -0
- data/generators/backup/templates/config/backup.rb +60 -0
- data/generators/backup/templates/migrations/create_backup_tables.rb +24 -0
- data/generators/backup/templates/tasks/backup.rake +42 -0
- data/lib/backup.rb +64 -12
- data/lib/backup/adapters/archive.rb +53 -0
- data/lib/backup/adapters/base.rb +65 -0
- data/lib/backup/adapters/mysql.rb +48 -0
- data/lib/backup/configuration/adapter.rb +17 -0
- data/lib/backup/configuration/base.rb +38 -0
- data/lib/backup/configuration/helpers.rb +12 -0
- data/lib/backup/configuration/storage.rb +17 -0
- data/lib/backup/connection/s3.rb +28 -13
- data/lib/backup/record/s3.rb +90 -0
- data/lib/backup/record/scp.rb +92 -0
- data/lib/backup/storage/s3.rb +14 -0
- data/lib/backup/storage/scp.rb +28 -0
- metadata +18 -35
- data/generators/backup_files/backup_files_generator.rb +0 -72
- data/generators/backup_files/templates/backup.sqlite3 +0 -0
- data/generators/backup_files/templates/config.rake +0 -20
- data/generators/backup_files/templates/db.rake +0 -62
- data/generators/backup_files/templates/s3.rake +0 -231
- data/generators/backup_files/templates/s3.yml +0 -120
- data/generators/backup_files/templates/setup.rake +0 -28
- data/generators/backup_files/templates/ssh.rake +0 -226
- data/generators/backup_files/templates/ssh.yml +0 -119
- data/lib/backup/adapter/assets.rb +0 -57
- data/lib/backup/adapter/custom.rb +0 -91
- data/lib/backup/adapter/mysql.rb +0 -65
- data/lib/backup/adapter/sqlite3.rb +0 -49
- data/lib/backup/backup_record/s3.rb +0 -90
- data/lib/backup/backup_record/ssh.rb +0 -90
- data/lib/backup/base.rb +0 -92
- data/lib/backup/connection/base.rb +0 -13
- data/lib/backup/connection/ssh.rb +0 -19
- data/lib/backup/encrypt.rb +0 -18
- data/lib/backup/transfer/base.rb +0 -13
- data/lib/backup/transfer/s3.rb +0 -36
- data/lib/backup/transfer/ssh.rb +0 -30
@@ -1,90 +0,0 @@
|
|
1
|
-
require 'net/ssh'
|
2
|
-
|
3
|
-
module Backup
|
4
|
-
module BackupRecord
|
5
|
-
class SSH < ActiveRecord::Base
|
6
|
-
|
7
|
-
# Establishes a connection with the SQLite3
|
8
|
-
# local database to avoid conflict with users
|
9
|
-
# Production database.
|
10
|
-
establish_connection(
|
11
|
-
:adapter => "sqlite3",
|
12
|
-
:database => "db/backup.sqlite3",
|
13
|
-
:pool => 5,
|
14
|
-
:timeout => 5000 )
|
15
|
-
|
16
|
-
# Scopes
|
17
|
-
default_scope :order => 'created_at desc'
|
18
|
-
|
19
|
-
# Callbacks
|
20
|
-
after_save :destroy_old_backups
|
21
|
-
|
22
|
-
# Attributes
|
23
|
-
attr_accessor :options, :keep_backups, :ip, :user
|
24
|
-
|
25
|
-
# Receives the options hash and stores it
|
26
|
-
# Sets the S3 values
|
27
|
-
def set_options(options)
|
28
|
-
self.options = options
|
29
|
-
self.backup_file = options[:backup_file]
|
30
|
-
self.backup_path = options[:ssh][:path]
|
31
|
-
self.keep_backups = options[:keep_backups]
|
32
|
-
self.adapter = options[:adapter]
|
33
|
-
self.index = options[:index]
|
34
|
-
self.ip = options[:ssh][:ip]
|
35
|
-
self.user = options[:ssh][:user]
|
36
|
-
end
|
37
|
-
|
38
|
-
# This will only be triggered by the rake task
|
39
|
-
# rake backup:db:destroy:ssh
|
40
|
-
#
|
41
|
-
# This will loop through all the configured adapters
|
42
|
-
# and destroy all "Backup" database records for the
|
43
|
-
# SSH table and delete all backed up files from the
|
44
|
-
# remote server on which they are stored.
|
45
|
-
def self.destroy_all_backups(adapter, options, index)
|
46
|
-
backups = Backup::BackupRecord::SSH.all(:conditions => {:adapter => adapter, :index => index})
|
47
|
-
unless backups.empty?
|
48
|
-
Net::SSH.start(options['ssh']['ip'], options['ssh']['user']) do |ssh|
|
49
|
-
# Loop through all backups that should be destroyed and remove them from remote server.
|
50
|
-
backups.each do |backup|
|
51
|
-
puts "Destroying old backup: #{backup.backup_file}.."
|
52
|
-
ssh.exec("rm #{File.join(backup.backup_path, backup.backup_file)}")
|
53
|
-
backup.destroy
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
# Destroys backups when the backup limit has been reached
|
62
|
-
# This is determined by the "keep_backups:" parameter
|
63
|
-
# First all backups will be fetched.
|
64
|
-
def destroy_old_backups
|
65
|
-
if keep_backups.is_a?(Integer)
|
66
|
-
backups = Backup::BackupRecord::SSH.all(:conditions => {:adapter => adapter, :index => index})
|
67
|
-
backups_to_destroy = Array.new
|
68
|
-
backups.each_with_index do |backup, index|
|
69
|
-
if index >= keep_backups then
|
70
|
-
backups_to_destroy << backup
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
if backups_to_destroy
|
75
|
-
# Establish a connection with the remote server through SSH
|
76
|
-
Net::SSH.start(ip, user) do |ssh|
|
77
|
-
# Loop through all backups that should be destroyed and remove them from remote server.
|
78
|
-
backups_to_destroy.each do |backup|
|
79
|
-
puts "Destroying old backup: #{backup.backup_file}.."
|
80
|
-
ssh.exec("rm #{File.join(backup.backup_path, backup.backup_file)}")
|
81
|
-
backup.destroy
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
data/lib/backup/base.rb
DELETED
@@ -1,92 +0,0 @@
|
|
1
|
-
module Backup
|
2
|
-
class Base
|
3
|
-
|
4
|
-
attr_accessor :options, :backup_time
|
5
|
-
|
6
|
-
def initialize(options = {})
|
7
|
-
self.options = options
|
8
|
-
self.backup_time = Time.now.strftime("%Y%m%d%H%M%S")
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
# Sets up the default paths and stores them in the options hash
|
14
|
-
# It also ensures the directory to where the temporary files are stored
|
15
|
-
# exists. If it doesn't it'll be created
|
16
|
-
# It will store the backup_path and the backup_file names
|
17
|
-
# The backup_file name is prefixed with the timestamp of the initialize time.
|
18
|
-
def setup_paths(path, type = nil)
|
19
|
-
%x{ mkdir -p #{RAILS_ROOT}/tmp/backups/#{path} }
|
20
|
-
options[:backup_path] = "#{RAILS_ROOT}/tmp/backups/#{path}"
|
21
|
-
|
22
|
-
if options[:file].is_a?(Array)
|
23
|
-
options[:backup_file] = "#{backup_time}-#{options[:file].first}.#{type}"
|
24
|
-
else
|
25
|
-
options[:backup_file] = "#{backup_time}-#{options[:file]}.#{type}"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
# Initializes one of the transfer methods
|
30
|
-
# Currently there are two transfer methods available
|
31
|
-
#
|
32
|
-
# - Amazon S3
|
33
|
-
# - SSH
|
34
|
-
def transfer
|
35
|
-
case options[:use]
|
36
|
-
when :s3 then Backup::Transfer::S3.new(options)
|
37
|
-
when :ssh then Backup::Transfer::SSH.new(options)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
# Records a backup to the "backup.sqlite3" database
|
42
|
-
# Will destroy any old backups that exceed the amount of
|
43
|
-
# backups that are allowed to be stored on either S3 or any
|
44
|
-
# remote server through SSH.
|
45
|
-
def record
|
46
|
-
case options[:use]
|
47
|
-
when :s3
|
48
|
-
backup = Backup::BackupRecord::S3.new
|
49
|
-
backup.set_options(options)
|
50
|
-
backup.save
|
51
|
-
|
52
|
-
when :ssh
|
53
|
-
backup = Backup::BackupRecord::SSH.new
|
54
|
-
backup.set_options(options)
|
55
|
-
backup.save
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
# Encrypts the backup file
|
60
|
-
# Only if the encrypt option is specified inside the .yml config file
|
61
|
-
# Otherwise, the encryption will be not be executed.
|
62
|
-
# Encryption is OPTIONAL.
|
63
|
-
def encrypt
|
64
|
-
unless options[:encrypt].blank?
|
65
|
-
Backup::Encrypt.new(options).run
|
66
|
-
options[:backup_file] = "#{options[:backup_file]}.enc"
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
# Removes files that were stored in the tmp/backups/* directory of the Rails application
|
71
|
-
# It completely cleans up the backup folders so theres no trash stored on the production server
|
72
|
-
def remove_temp_files
|
73
|
-
%x{ rm #{File.join(options[:backup_path], "*")} }
|
74
|
-
end
|
75
|
-
|
76
|
-
# Removes files that were generated for the transfer
|
77
|
-
# This can remove either a single file or an array of files
|
78
|
-
# Depending on whether the options[:file] is an Array or a String
|
79
|
-
def remove_original_file
|
80
|
-
unless options[:keep_original_files].eql?(true)
|
81
|
-
if options[:file].is_a?(Array)
|
82
|
-
options[:file].each do |file|
|
83
|
-
%x{ rm #{File.join(options[:path], file)} }
|
84
|
-
end
|
85
|
-
else
|
86
|
-
%x{ rm #{File.join(options[:path], options[:file])} }
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|
92
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module Backup
|
2
|
-
module Connection
|
3
|
-
class SSH < Backup::Connection::Base
|
4
|
-
|
5
|
-
def initialize(options = {})
|
6
|
-
super(options)
|
7
|
-
end
|
8
|
-
|
9
|
-
# Initializes the transfer to the specified server using SSH.
|
10
|
-
# This will first ensure there is a directory, if there is not, a new one will be created
|
11
|
-
# After the directory has been confirmed, the transfer process will be initialized.
|
12
|
-
def transfer
|
13
|
-
%x{ ssh #{options[:ssh][:user]}@#{options[:ssh][:ip]} mkdir -p #{options[:ssh][:path]} }
|
14
|
-
%x{ scp #{File.join(options[:backup_path], options[:backup_file])} #{options[:ssh][:user]}@#{options[:ssh][:ip]}:#{options[:ssh][:path]} }
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
data/lib/backup/encrypt.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
module Backup
|
2
|
-
class Encrypt
|
3
|
-
|
4
|
-
attr_accessor :options
|
5
|
-
|
6
|
-
def initialize(options = {})
|
7
|
-
self.options = options
|
8
|
-
end
|
9
|
-
|
10
|
-
# Encrypts the backup file
|
11
|
-
def run
|
12
|
-
unencrypted_file = File.join(options[:backup_path], options[:backup_file])
|
13
|
-
encrypted_file = File.join(options[:backup_path], options[:backup_file] + '.enc')
|
14
|
-
%x{ openssl enc -des-cbc -in #{unencrypted_file} -out #{encrypted_file} -k #{options[:encrypt]} }
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
data/lib/backup/transfer/base.rb
DELETED
data/lib/backup/transfer/s3.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'aws/s3'
|
2
|
-
|
3
|
-
module Backup
|
4
|
-
module Transfer
|
5
|
-
class S3 < Backup::Transfer::Base
|
6
|
-
|
7
|
-
def initialize(options)
|
8
|
-
super(default_options.merge(options))
|
9
|
-
|
10
|
-
# Creates a new instance of the Amazon S3 Wrapper Class/Object
|
11
|
-
# Passes in the options hash and lets the wrapper extract only the
|
12
|
-
# necessary information that is required to establish a link to Amazon S3.
|
13
|
-
s3 = Backup::Connection::S3.new(options)
|
14
|
-
|
15
|
-
# Connects to Amazon S3 using the credentials provided and
|
16
|
-
# stored in the options has by the user
|
17
|
-
s3.connect
|
18
|
-
|
19
|
-
# Initializes the file transfer to Amazon S3
|
20
|
-
s3.transfer
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
# Set default options
|
26
|
-
def default_options
|
27
|
-
{:s3 => {
|
28
|
-
:access_key_id => '',
|
29
|
-
:secret_access_key => '',
|
30
|
-
:bucket => ''
|
31
|
-
}}
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
data/lib/backup/transfer/ssh.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
module Backup
|
2
|
-
module Transfer
|
3
|
-
class SSH < Backup::Transfer::Base
|
4
|
-
|
5
|
-
def initialize(options)
|
6
|
-
super(default_options.merge(options))
|
7
|
-
|
8
|
-
# Creates a new instance of the SSH Wrapper Class/Object
|
9
|
-
# Passes in the options hash and lets the wrapper extract only the
|
10
|
-
# necessary information that is required to later transfer the specified file through SSH.
|
11
|
-
ssh = Backup::Connection::SSH.new(options)
|
12
|
-
|
13
|
-
# Initializes the file transfer to the specified server through SSH.
|
14
|
-
ssh.transfer
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
# Set default options
|
20
|
-
def default_options
|
21
|
-
{:ssh => {
|
22
|
-
:user => "",
|
23
|
-
:ip => "",
|
24
|
-
:path => "/var/backups/"
|
25
|
-
}}
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|