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,119 +0,0 @@
|
|
1
|
-
# Backup Configuration file for "SSH (SCP) Backups"
|
2
|
-
# This file was generated with the Backup Generator
|
3
|
-
#
|
4
|
-
# FOR MORE INFORMATION, VIEW THE WIKI ON GITHUB:
|
5
|
-
# http://wiki.github.com/meskyanichi/backup
|
6
|
-
#
|
7
|
-
# If you wish to make use of SSH (SCP) to store your
|
8
|
-
# backups to another server, use this configuration file.
|
9
|
-
#
|
10
|
-
# Step by Step
|
11
|
-
#
|
12
|
-
# Step 1
|
13
|
-
# Choose the adapter you would like to use to back up your files
|
14
|
-
# (MySQL, SQLite3, Assets or something Custom)
|
15
|
-
#
|
16
|
-
# Step 2
|
17
|
-
# Fill in the values for the adapter you choose.
|
18
|
-
# You do not need to fill in the values for every adapter,
|
19
|
-
# only the ones you are going to use. The rest you can leave untouched or remove.
|
20
|
-
#
|
21
|
-
# Step 3
|
22
|
-
# Once you've filled in all values, you are ready to execute the rake task that
|
23
|
-
# starts that backup process. Above each Adapter you will see what rake task should
|
24
|
-
# be run to start the backup process for that specific Adapter.
|
25
|
-
#
|
26
|
-
# Step 4
|
27
|
-
# Once set up, you could set these rake tasks up in a cronjob to execute it periodically.
|
28
|
-
# For handling this, I recommend using the javan-whenever gem
|
29
|
-
#
|
30
|
-
# See here: http://github.com/javan/whenever
|
31
|
-
|
32
|
-
|
33
|
-
# Global Options
|
34
|
-
#
|
35
|
-
# - encrypt (optional!)
|
36
|
-
#
|
37
|
-
# What "encrypt" does is encrypt your backup with the given password
|
38
|
-
# Example:
|
39
|
-
# encrypt: this_is_my_password
|
40
|
-
#
|
41
|
-
# This will encrypt the backup file with the password: this_is_my_password
|
42
|
-
# NOTE: To "decrypt" an encrypted file later on, use the following command:
|
43
|
-
# openssl enc -des-cbc -d -in encrypted_file -out decrypted_file
|
44
|
-
#
|
45
|
-
#
|
46
|
-
# - keep_backups (optional!)
|
47
|
-
#
|
48
|
-
# what "keep_backups" does is it ensures that there won't be more than the specified
|
49
|
-
# amount of backups present on the storage location (S3 or any remotely accessible server)
|
50
|
-
# Example:
|
51
|
-
# keep_backups: 10
|
52
|
-
#
|
53
|
-
# This will make sure there won't be more than 10 backups on the server at a time.
|
54
|
-
# When the 11th backup is pushed to for example Amazon S3, it will delete the oldest
|
55
|
-
# backup. Why would you want this? To avoid S3 from being expensive and otherwise
|
56
|
-
# to not overload your remote server's harddisk and practically fload it.
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
# MySQL Adapter
|
62
|
-
# To run this adapter, execute the following rake task:
|
63
|
-
# rake backup:ssh:mysql
|
64
|
-
mysql:
|
65
|
-
mysql_config:
|
66
|
-
user: root
|
67
|
-
password: ''
|
68
|
-
database: foobar
|
69
|
-
ssh:
|
70
|
-
user: root
|
71
|
-
ip: my-domain.com
|
72
|
-
path: '/var/backups'
|
73
|
-
#encrypt: my_secret_password # Uncomment me if you want this functionality
|
74
|
-
#keep_backups: 25 # Uncomment me if you want this functionality
|
75
|
-
|
76
|
-
|
77
|
-
# SQLite3 Adapter
|
78
|
-
# To run this adapter, execute the following rake task:
|
79
|
-
# rake backup:ssh:sqlite3
|
80
|
-
sqlite3:
|
81
|
-
file: production.sqlite3
|
82
|
-
path: ':rails_root/db'
|
83
|
-
ssh:
|
84
|
-
user: root
|
85
|
-
ip: my-domain.com
|
86
|
-
path: '/var/backups'
|
87
|
-
#encrypt: my_secret_password # Uncomment me if you want this functionality
|
88
|
-
#keep_backups: 25 # Uncomment me if you want this functionality
|
89
|
-
|
90
|
-
# Asset Adapter
|
91
|
-
# To run this adapter, execute the following rake task:
|
92
|
-
# rake backup:ssh:assets
|
93
|
-
assets:
|
94
|
-
path: ':rails_root/public/assets'
|
95
|
-
ssh:
|
96
|
-
user: root
|
97
|
-
ip: my-domain.com
|
98
|
-
path: '/var/backups'
|
99
|
-
#encrypt: my_secret_password # Uncomment me if you want this functionality
|
100
|
-
#keep_backups: 25 # Uncomment me if you want this functionality
|
101
|
-
|
102
|
-
|
103
|
-
# Custom Adapter
|
104
|
-
# To run this adapter, execute the following rake task:
|
105
|
-
# rake backup:ssh:custom
|
106
|
-
custom:
|
107
|
-
path: ':rails_root/db'
|
108
|
-
file:
|
109
|
-
- 'foobar1.sql'
|
110
|
-
- 'foobar2.sql'
|
111
|
-
command:
|
112
|
-
- 'mysqldump --quick -u root --password="" foobar > :rails_root/db/foobar1.sql'
|
113
|
-
- 'mysqldump --quick -u root --password="" foobar > :rails_root/db/foobar2.sql'
|
114
|
-
ssh:
|
115
|
-
user: root
|
116
|
-
ip: my-domain.com
|
117
|
-
path: '/var/backups'
|
118
|
-
#encrypt: my_secret_password # Uncomment me if you want this functionality
|
119
|
-
#keep_backups: 25 # Uncomment me if you want this functionality
|
@@ -1,57 +0,0 @@
|
|
1
|
-
module Backup
|
2
|
-
module Adapter
|
3
|
-
class Assets < Backup::Base
|
4
|
-
|
5
|
-
def initialize(options = {})
|
6
|
-
super(default_options.merge(options))
|
7
|
-
setup_paths("assets/#{self.class.name.downcase.gsub('::','-')}", 'tar.gz')
|
8
|
-
end
|
9
|
-
|
10
|
-
# Initialize the process
|
11
|
-
# Executing multiple processes
|
12
|
-
#
|
13
|
-
# - Archive
|
14
|
-
# Archives the specified folder to a .tar
|
15
|
-
# - Compress
|
16
|
-
# Compresses the .tar file using Gzip
|
17
|
-
# - Encrypt
|
18
|
-
# Encrypts the backup file
|
19
|
-
# - Transfer
|
20
|
-
# Initializes the transfer to either S3 or using SSH
|
21
|
-
# - Record
|
22
|
-
# Records the Backup Data to the Backup SQLite3 database
|
23
|
-
# - Remove Temp Files
|
24
|
-
# Removes temporary files after the process is complete
|
25
|
-
def run
|
26
|
-
begin
|
27
|
-
archive
|
28
|
-
compress
|
29
|
-
encrypt
|
30
|
-
transfer
|
31
|
-
record
|
32
|
-
ensure
|
33
|
-
remove_temp_files
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
# Archives the assets into a .tar file and stores it
|
40
|
-
# inside the "Backup Path"
|
41
|
-
def archive
|
42
|
-
%x{ tar -cf #{File.join(options[:backup_path], options[:backup_file])} #{options[:path]} }
|
43
|
-
end
|
44
|
-
|
45
|
-
# Compresses the .tar file to .tar.gz and removes the old .tar file
|
46
|
-
def compress
|
47
|
-
%x{ gzip --best #{File.join(options[:backup_path], options[:backup_file])} }
|
48
|
-
end
|
49
|
-
|
50
|
-
# Set default options
|
51
|
-
def default_options
|
52
|
-
{ :path => "#{RAILS_ROOT}/public/assets", :file => "assets" }
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
@@ -1,91 +0,0 @@
|
|
1
|
-
module Backup
|
2
|
-
module Adapter
|
3
|
-
class Custom < Backup::Base
|
4
|
-
|
5
|
-
def initialize(options = {})
|
6
|
-
super(default_options.merge(options))
|
7
|
-
setup_paths("db/#{self.class.name.downcase.gsub('::','-')}", options[:file].is_a?(Array) ? 'tar.gz' : 'gz')
|
8
|
-
end
|
9
|
-
|
10
|
-
# Initialize the process
|
11
|
-
# Executing multiple processes
|
12
|
-
#
|
13
|
-
# - Command
|
14
|
-
# Executes a command from a user to generate a SQL dump
|
15
|
-
# - Archive
|
16
|
-
# Archives the specified folder to a .tar
|
17
|
-
# - Compress
|
18
|
-
# Compresses the .tar file using Gzip
|
19
|
-
# - Encrypt
|
20
|
-
# Encrypts the backup file
|
21
|
-
# - Transfer
|
22
|
-
# Initializes the transfer to either S3 or using SSH
|
23
|
-
# - Record
|
24
|
-
# Records the Backup Data to the Backup SQLite3 database
|
25
|
-
# - Remove Temp Files
|
26
|
-
# Removes temporary files after the process is complete
|
27
|
-
# - Remove Original File
|
28
|
-
# Removes the user generated sql files (unless the user specifies he wants to keep them)
|
29
|
-
def run
|
30
|
-
begin
|
31
|
-
command
|
32
|
-
archive
|
33
|
-
compress
|
34
|
-
encrypt
|
35
|
-
transfer
|
36
|
-
record
|
37
|
-
ensure
|
38
|
-
remove_temp_files
|
39
|
-
remove_original_file
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
private
|
44
|
-
|
45
|
-
# Allows a user to insert one or more commands to be executed
|
46
|
-
# before the actual archive, compress and transferring processes.
|
47
|
-
# The command takes either a String for a single command, and an Array for multiple commands.
|
48
|
-
def command
|
49
|
-
if options[:command].is_a?(Array)
|
50
|
-
options[:command].each do |command|
|
51
|
-
%x{ #{command} }
|
52
|
-
end
|
53
|
-
else
|
54
|
-
%x{ #{options[:command]} }
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
# Archives the assets into a .tar file and stores it
|
59
|
-
# inside the "Backup Path"
|
60
|
-
def archive
|
61
|
-
if options[:file].is_a?(Array)
|
62
|
-
files = options[:file].map {|file| File.join(options[:path], file)}
|
63
|
-
%x{ tar -cf #{File.join(options[:backup_path], options[:backup_file])} #{files.join(' ')} }
|
64
|
-
else
|
65
|
-
%x{ tar -cf #{File.join(options[:backup_path], options[:backup_file])} #{File.join(options[:path], options[:file])} }
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
# If the user has bundled a couple of files to a .tar (by using an Array for the :file attribute)
|
70
|
-
# then it compresses the .tar file to .tar.gz and removes the old .tar file
|
71
|
-
# If the user has only a single file, it will be read out and a new file will be generated
|
72
|
-
# The old (single) file will remain until the process is complete, unless the user specifies otherwise.
|
73
|
-
def compress
|
74
|
-
if options[:file].is_a?(Array)
|
75
|
-
%x{ gzip --best #{File.join(options[:backup_path], options[:backup_file])} }
|
76
|
-
else
|
77
|
-
%x{ gzip -cv #{File.join(options[:path], options[:file])} --best > #{File.join(options[:backup_path], options[:backup_file])} }
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
# Set default options
|
82
|
-
def default_options
|
83
|
-
{ :path => "",
|
84
|
-
:file => "",
|
85
|
-
:command => "",
|
86
|
-
:keep_original_files => false }
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
data/lib/backup/adapter/mysql.rb
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
module Backup
|
2
|
-
module Adapter
|
3
|
-
class Mysql < Backup::Base
|
4
|
-
|
5
|
-
def initialize(options = {})
|
6
|
-
super(default_options.merge(options))
|
7
|
-
setup_paths("db/#{self.class.name.downcase.gsub('::','-')}", :gz)
|
8
|
-
end
|
9
|
-
|
10
|
-
# Initialize the process
|
11
|
-
# Executing multiple processes
|
12
|
-
#
|
13
|
-
# - Make MySQL Dump
|
14
|
-
# Creates a MySQL dump based on the parameters provided by the user
|
15
|
-
# - Compress
|
16
|
-
# Compresses the .tar file using Gzip
|
17
|
-
# - Encrypt
|
18
|
-
# Encrypts the backup file
|
19
|
-
# - Transfer
|
20
|
-
# Initializes the transfer to either S3 or using SSH
|
21
|
-
# - Record
|
22
|
-
# Records the Backup Data to the Backup SQLite3 database
|
23
|
-
# - Remove Temp Files
|
24
|
-
# Removes temporary files after the process is complete
|
25
|
-
def run
|
26
|
-
begin
|
27
|
-
make_mysql_dump
|
28
|
-
compress
|
29
|
-
encrypt
|
30
|
-
transfer
|
31
|
-
record
|
32
|
-
ensure
|
33
|
-
remove_temp_files
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
# Compresses the MySQL dump file and stores the compressed version inside the tmp/backups folder.
|
40
|
-
def compress
|
41
|
-
%x{ gzip -cv #{File.join(options[:path], options[:file])} --best > #{File.join(options[:backup_path], options[:backup_file])} }
|
42
|
-
end
|
43
|
-
|
44
|
-
# This will generate a MySQL dump based on the options the user passed in.
|
45
|
-
# The MySQL dump will be placed (by default) in the config/db directory so it can be found
|
46
|
-
# by the compressor.
|
47
|
-
def make_mysql_dump
|
48
|
-
# => /usr/local/mysql/bin/mysqldump on Mac OS X 10.6
|
49
|
-
%x{ mysqldump --quick -u #{options[:mysql][:user]} --password='#{options[:mysql][:password]}' #{options[:mysql][:database]} > #{File.join(options[:path], options[:file])} }
|
50
|
-
end
|
51
|
-
|
52
|
-
# Set default options
|
53
|
-
def default_options
|
54
|
-
{:path => "#{RAILS_ROOT}/tmp/backups/db/#{self.class.name.downcase.gsub('::','-')}",
|
55
|
-
:file => "production.sql",
|
56
|
-
:mysql => {
|
57
|
-
:user => "",
|
58
|
-
:password => "",
|
59
|
-
:database => ""
|
60
|
-
}}
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
module Backup
|
2
|
-
module Adapter
|
3
|
-
class Sqlite3 < Backup::Base
|
4
|
-
|
5
|
-
def initialize(options = {})
|
6
|
-
super(default_options.merge(options))
|
7
|
-
setup_paths("db/#{self.class.name.downcase.gsub('::','-')}", :gz)
|
8
|
-
end
|
9
|
-
|
10
|
-
# Initialize the process
|
11
|
-
# Executing multiple processes
|
12
|
-
#
|
13
|
-
# - Compress
|
14
|
-
# Compresses the .tar file using Gzip
|
15
|
-
# - Encrypt
|
16
|
-
# Encrypts the backup file
|
17
|
-
# - Transfer
|
18
|
-
# Initializes the transfer to either S3 or using SSH
|
19
|
-
# - Record
|
20
|
-
# Records the Backup Data to the Backup SQLite3 database
|
21
|
-
# - Remove Temp Files
|
22
|
-
# Removes temporary files after the process is complete
|
23
|
-
def run
|
24
|
-
begin
|
25
|
-
compress
|
26
|
-
encrypt
|
27
|
-
transfer
|
28
|
-
record
|
29
|
-
ensure
|
30
|
-
remove_temp_files
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
# Compresses the SQLite3file and stores the compressed version inside the tmp/backups folder.
|
37
|
-
def compress
|
38
|
-
%x{ gzip -cv #{File.join(options[:path], options[:file])} --best > #{File.join(options[:backup_path], options[:backup_file])} }
|
39
|
-
end
|
40
|
-
|
41
|
-
# Set default options
|
42
|
-
def default_options
|
43
|
-
{ :path => "#{RAILS_ROOT}/db",
|
44
|
-
:file => "production.sqlite3" }
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,90 +0,0 @@
|
|
1
|
-
require 'aws/s3'
|
2
|
-
|
3
|
-
module Backup
|
4
|
-
module BackupRecord
|
5
|
-
class S3 < 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
|
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.bucket = options[:s3][:bucket]
|
31
|
-
self.keep_backups = options[:keep_backups]
|
32
|
-
self.adapter = options[:adapter]
|
33
|
-
self.index = options[:index]
|
34
|
-
end
|
35
|
-
|
36
|
-
# This will only be triggered by the rake task
|
37
|
-
# rake backup:db:destroy:s3
|
38
|
-
#
|
39
|
-
# This will loop through all the configured adapters
|
40
|
-
# and destroy all "Backup" database records for the
|
41
|
-
# S3 table and delete all backed up files from the
|
42
|
-
# Amazon S3 server.
|
43
|
-
def self.destroy_all_backups(adapter, options, index)
|
44
|
-
backups = Backup::BackupRecord::S3.all(:conditions => {:adapter => adapter, :index => index})
|
45
|
-
unless backups.empty?
|
46
|
-
s3 = Backup::Connection::S3.new(options)
|
47
|
-
s3.connect
|
48
|
-
backups.each do |backup|
|
49
|
-
puts "Destroying backup: #{backup.backup_file}.."
|
50
|
-
s3.destroy(backup.backup_file, backup.bucket)
|
51
|
-
backup.destroy
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
private
|
57
|
-
|
58
|
-
# Destroys backups when the backup limit has been reached
|
59
|
-
# This is determined by the "keep_backups:" parameter
|
60
|
-
# First all backups will be fetched.
|
61
|
-
def destroy_old_backups
|
62
|
-
if keep_backups.is_a?(Integer)
|
63
|
-
backups = Backup::BackupRecord::S3.all(:conditions => {:adapter => adapter, :index => index})
|
64
|
-
backups_to_destroy = Array.new
|
65
|
-
backups.each_with_index do |backup, index|
|
66
|
-
if index >= keep_backups then
|
67
|
-
backups_to_destroy << backup
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
if backups_to_destroy
|
72
|
-
# Create a new Amazon S3 Object
|
73
|
-
s3 = Backup::Connection::S3.new(options)
|
74
|
-
|
75
|
-
# Connect to Amazon S3 with provided credentials
|
76
|
-
s3.connect
|
77
|
-
|
78
|
-
# Loop through all backups that should be destroyed and remove them from S3.
|
79
|
-
backups_to_destroy.each do |backup|
|
80
|
-
puts "Destroying old backup: #{backup.backup_file}.."
|
81
|
-
s3.destroy(backup.backup_file, backup.bucket)
|
82
|
-
backup.destroy
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|