backup 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 meskyanichi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,237 @@
1
+ = Backup
2
+
3
+ Backup is a gem/plugin that enables you to very easily create backups and transfer these to Amazon S3 or another server using SSH.
4
+ It currently supports MySQL, SQLite3 and basic Assets (documents, images, etc) inside a folder. The files will get tar'd / gzip'd and get a timestamp-prefix.
5
+ After the backup file has been created, it can be transferred to either Amazon S3 or any remote server through SSH.
6
+
7
+ == Installation
8
+
9
+ === Add Repository Source(s)
10
+
11
+ gem sources -a http://gemcutter.org
12
+ gem sources -a http://gems.github.com
13
+
14
+ === Gem
15
+
16
+ # Gem Cutter
17
+ sudo gem install backup
18
+
19
+ # GitHub
20
+ sudo gem install meskyanichi-backup
21
+
22
+ === Plugin
23
+
24
+ ./script/plugin install git://github.com/meskyanichi/backup.git
25
+
26
+
27
+ === Dependencies
28
+
29
+ # This will automatically install when installing the Backup gem.
30
+ # If you are using the Plugin, instead of the gem, be sure to install the aws-s3 gem.
31
+ sudo gem install aws-s3
32
+
33
+
34
+ == Getting started
35
+
36
+ Well, this is ridiculously easy to set up! So let's do this.
37
+ First install either the gem or plugin.
38
+
39
+ If you are using the gem version you "must" add the following line to your environment.rb file
40
+
41
+ ==== config/environment.rb
42
+
43
+ # For GemCutter Version
44
+ config.gem "backup", :lib => "backup", :version => "0.1.0", :source => "http://gemcutter.org"
45
+
46
+ # For GitHub Version
47
+ config.gem "meskyanichi-backup", :lib => "backup", :version => "0.1.0", :source => "http://gems.github.com"
48
+
49
+ Once that's done, run the following command from the "root" of your Rails App
50
+
51
+ ./script/generate backup_rake_tasks
52
+
53
+
54
+ This will generate two rake tasks and a README inside your "#{RAILS_ROOT}/lib/tasks/backup" folder.
55
+ - README.rdoc
56
+ - s3.rake
57
+ - ssh.rake
58
+
59
+ Open and read the README.rdoc if you want. It will explain everything very quickly, though, I doubt there is much to explain!
60
+ Then (or otherwise) open the "s3.rake" and "ssh.rake" rake task files. These include all the rake tasks/combinations that are (currently!) available.
61
+ Above each task inside these files is a description, explaining what you must do, again, "very" straight forward. Stupidly Easy to set up, thankfully!
62
+
63
+ After you've set up the tasks you wish to utilize (obviously you don't need to use all of them, you can simply just choose to use one of them),
64
+ they are all generated so you can basically just fill in the configuration values and be done with it. You obviously don't have to fill in the configuration
65
+ for any rake tasks you are not going to use.
66
+
67
+ That's it! So you have now done two things:
68
+ - Configured the Rake Files (just filled in the empty values)
69
+ - And at the same time, these are the executable rake tasks you will be using to create a backup
70
+
71
+ Just run one of the configured rake tasks to perform a backup!
72
+
73
+ == Example
74
+
75
+ So let me give an example of what one of these rake tasks will look like:
76
+
77
+ ==== Rake Task for Backing up a Sqlite3 file to S3
78
+
79
+ task :sqlite3 => :environment do
80
+ Backup::Sqlite3.new({
81
+ :file => 'production.sqlite3',
82
+
83
+ :use => :s3,
84
+ :s3 => {
85
+ :access_key_id => 'your-s3-id',
86
+ :secret_access_key => 'your-s3-password',
87
+ :bucket => 'your-bucket-to-backup-to'
88
+ }
89
+ }).run
90
+ end
91
+
92
+ So this is one of the rake that's that the generator provides. Simple, isn't it?
93
+ Just fill in the values and run:
94
+
95
+ rake backup:s3:sqlite3
96
+
97
+ ==== Rake Task for Backing up a Sqlite3 file to another server through SSH
98
+
99
+ task :sqlite3 => :environment do
100
+ Backup::Sqlite3.new({
101
+ :file => 'production.sqlite3',
102
+
103
+ :use => :ssh,
104
+ :ssh => {
105
+ :user => "root",
106
+ :ip => "123.45.678.90", # OR my-domain.com
107
+ :path => "/var/backups/etc"
108
+ }
109
+ }).run
110
+ end
111
+
112
+ Again, quick and easy. Now just execute this Backing/SSH transfer by running:
113
+
114
+ rake backing:ssh:sqlite3
115
+
116
+
117
+ See below what the requirements are when using S3 or SSH.
118
+
119
+ == Requirements
120
+
121
+ === Using Amazon S3
122
+
123
+ This obviously requires you to have access to an Amazon S3 account.
124
+ These accounts are free and you only get charged for what you actually "use".
125
+ So no transfers = no cost. And aside of that, S3 is EXTREMELY cheap!
126
+
127
+ You can get an account here: http://aws.amazon.com/s3
128
+
129
+ Once you have an account you must install the AWS S3 gem, like so:
130
+
131
+ sudo gem install aws-s3
132
+
133
+ Backup makes use of the "aws-s3" gem to connect to Amazon S3. This is a dependency and will be installed when installing the Backup gem.
134
+ If you are using the plugin, you will need to manually install it.
135
+
136
+ === Using SSH
137
+
138
+ If you're using SSH then there is one thing you must do. You must provide the machine that's going to "receive" your backups
139
+ your machine's (the senders) ssh-key. This is basically what you did with GitHub so you could push data to your GitHub repository
140
+ without getting prompted for a password.
141
+
142
+ === Setting Up A Key For SSH
143
+
144
+ Setting up SSH Keychains is quite simple.
145
+
146
+ SSH to the "production" server and run the following command:
147
+ ssh-keygen -t rsa
148
+
149
+ It will prompt you 3 times, first it will ask what you wish to call the filename.
150
+
151
+ Just hit enter every time, do "not" fill in a password.
152
+
153
+ This will generate two files in the ~/.ssh/ directory, namely:
154
+ - id_rsa
155
+ - id_rsa.pub
156
+
157
+ So, now that the files are generated, holding the authorization keys, you can use these on any server you wish to login to without using a password.
158
+ The procedure accomplishing this is easy.
159
+
160
+ First, we will ensure there is a .ssh directory on the "backup" server by running the following command:
161
+ ssh root@your_ip mkdir -p .ssh
162
+
163
+ Once that's in place, we will append our newly (or already existing) key to the backup servers' .ssh/authorized_keys file.
164
+ cat ~/.ssh/id_rsa.pub | ssh root@server.com 'cat >> .ssh/authorized_keys'
165
+
166
+ Done. Now the key, generated on your "production" server, has been inserted inside the ".ssh/authorized_keys" file on the Backup server.
167
+ You should now be set to run all SSH rake tasks that have been configured to work with that particular Backup server.
168
+
169
+ ==== Note: If the "authorized_keys" file does not yet exist, it will be automatically be created.
170
+
171
+
172
+ == Periodical Backups (using the "rake tasks" and a "cron")
173
+
174
+ Assuming you will want to run these backups (rake tasks) periodically.
175
+ What I currently use to run them is the "javan-whenever" gem. This is a very simple, easy to use gem
176
+ that makes it EXTREMELY simple to manage cron, using Ruby syntax. To understand what I mean, see the example below!
177
+
178
+
179
+ === Javan's Whenever Gem Example
180
+
181
+ With this gem you can basically get periodic backup execution as easy as this:
182
+
183
+ every 2.hours do
184
+ rake "backup:s3:sqlite3"
185
+ end
186
+
187
+ Obviously this will update the crontab to make SQLite3 backups and store them on Amazon S3 every 2 hours.
188
+ If you want to do multiple backups, like perhaps backup your MySQL database, along with your assets:
189
+
190
+ every 2.hours do
191
+ rake "backup:s3:mysql"
192
+ rake "backup:s3:assets"
193
+ end
194
+
195
+ So yes, I highly recommend using the javan-whenever gem for this. It's very easy to write and maintain the crontab this way.
196
+ For more information on this gem and on how to use it: http://github.com/javan/whenever
197
+ Also, Ryan Bates has created a screencast for this gem, see it here: http://railscasts.com/episodes/164-cron-in-ruby
198
+ It's awesome, go check it out!
199
+
200
+
201
+ == Resources
202
+
203
+ So let me sum up the resources
204
+
205
+ ==== My Backup Gem
206
+ sudo gem install backup
207
+ or
208
+ sudo gem install meskyanichi-backup
209
+
210
+ ==== AWS-S3 Gem
211
+ sudo gem install aws-s3
212
+
213
+ ==== Whenever Gem
214
+ sudo gem install javan-whenever
215
+ http://railscasts.com/episodes/164-cron-in-ruby
216
+
217
+ ==== Amazon S3 Account Creation
218
+ http://aws.amazon.com/s3
219
+
220
+
221
+ ==== Notes:
222
+
223
+ If you are going to use Amazon S3 to store your backups, be sure to install "AWS-S3".
224
+
225
+ If you want a nice and super easy way of managing the crontab and do periodical backups, install "Whenever".
226
+
227
+ Watch the Whenever Gem Screencast by Ryan Bates: http://railscasts.com/episodes/164-cron-in-ruby
228
+
229
+
230
+ == Requests
231
+
232
+ If anyone wishes to see support for PostgreSQL or any other database format, please send me a message!
233
+
234
+
235
+ == Copyright
236
+
237
+ Copyright (c) 2009 Michael van Rooijen | Final Creation. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "backup"
8
+ gem.summary = %Q{Backup is a gem/plugin that enables you to very easily create backups and transfer these to Amazon S3 or another server with SSH.}
9
+ gem.description = %Q{Backup is a gem/plugin that enables you to very easily create backups and transfer these to Amazon S3 or another server with SSH.
10
+ It currently supports MySQL, SQLite3 and basic Assets (documents, images, etc). The files will get tar'd / gzip'd and get a timestamp.
11
+ After creation, these files can be transferred to either Amazon S3 or any remote server through SSH.}
12
+ gem.email = "meskyan@gmail.com"
13
+ gem.homepage = "http://github.com/meskyanichi/backup"
14
+ gem.authors = ["meskyanichi"]
15
+ gem.add_dependency "aws-s3"
16
+ # gem.files.include 'generators/**/*'
17
+ # gem.files.include 'lib/**/*'
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/*_test.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/*_test.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ if File.exist?('VERSION')
50
+ version = File.read('VERSION')
51
+ else
52
+ version = ""
53
+ end
54
+
55
+ rdoc.rdoc_dir = 'rdoc'
56
+ rdoc.title = "backup #{version}"
57
+ rdoc.rdoc_files.include('README*')
58
+ rdoc.rdoc_files.include('lib/**/*.rb')
59
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/backup.gemspec ADDED
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{backup}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["meskyanichi"]
12
+ s.date = %q{2009-10-03}
13
+ s.description = %q{Backup is a gem/plugin that enables you to very easily create backups and transfer these to Amazon S3 or another server with SSH.
14
+ It currently supports MySQL, SQLite3 and basic Assets (documents, images, etc). The files will get tar'd / gzip'd and get a timestamp.
15
+ After creation, these files can be transferred to either Amazon S3 or any remote server through SSH.}
16
+ s.email = %q{meskyan@gmail.com}
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "backup.gemspec",
29
+ "generators/backup_rake_tasks/backup_rake_tasks_generator.rb",
30
+ "generators/backup_rake_tasks/templates/README.rdoc",
31
+ "generators/backup_rake_tasks/templates/s3.rake",
32
+ "generators/backup_rake_tasks/templates/ssh.rake",
33
+ "lib/backup.rb",
34
+ "lib/backup/assets.rb",
35
+ "lib/backup/base.rb",
36
+ "lib/backup/connection/base.rb",
37
+ "lib/backup/connection/s3.rb",
38
+ "lib/backup/connection/ssh.rb",
39
+ "lib/backup/custom.rb",
40
+ "lib/backup/mysql.rb",
41
+ "lib/backup/sqlite3.rb",
42
+ "lib/backup/transfer/base.rb",
43
+ "lib/backup/transfer/s3.rb",
44
+ "lib/backup/transfer/ssh.rb"
45
+ ]
46
+ s.homepage = %q{http://github.com/meskyanichi/backup}
47
+ s.rdoc_options = ["--charset=UTF-8"]
48
+ s.require_paths = ["lib"]
49
+ s.rubygems_version = %q{1.3.5}
50
+ s.summary = %q{Backup is a gem/plugin that enables you to very easily create backups and transfer these to Amazon S3 or another server with SSH.}
51
+
52
+ if s.respond_to? :specification_version then
53
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
54
+ s.specification_version = 3
55
+
56
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
57
+ s.add_runtime_dependency(%q<aws-s3>, [">= 0"])
58
+ else
59
+ s.add_dependency(%q<aws-s3>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<aws-s3>, [">= 0"])
63
+ end
64
+ end
@@ -0,0 +1,56 @@
1
+ class BackupRakeTasksGenerator < Rails::Generator::Base
2
+
3
+ # This method gets initialized when the generator gets run.
4
+ # It will receive an array of arguments inside @args
5
+ def initialize(runtime_args, runtime_options = {})
6
+ super
7
+ extract_args
8
+ set_defaults
9
+ confirm_input
10
+ end
11
+
12
+ # Processes the file generation/templating
13
+ # This will automatically be run after the initialize method
14
+ def manifest
15
+ record do |m|
16
+ m.directory "lib/tasks/"
17
+ m.directory "lib/tasks/backup"
18
+ m.file "README.rdoc", "lib/tasks/backup/README.rdoc"
19
+ m.file "s3.rake", "lib/tasks/backup/s3.rake"
20
+ m.file "ssh.rake", "lib/tasks/backup/ssh.rake"
21
+ end
22
+ end
23
+
24
+ # Creates a new Hash Object containing the user input
25
+ # The user input will be available through @input and input
26
+ def extract_args
27
+ @input = Hash.new
28
+ @args.each do |arg|
29
+ if arg.include?(":") then
30
+ @input[:"#{arg.slice(0, arg.index(":"))}"] = arg.slice((arg.index(":") + 1)..-1)
31
+ end
32
+ end
33
+ end
34
+
35
+ # Input Method that's available inside the generated templates
36
+ # because instance variable are not available, so we access them through methods
37
+ def input
38
+ @input
39
+ end
40
+
41
+ # Sets defaults for user input when left blank by the user
42
+ # for each parameter
43
+ def set_defaults
44
+ end
45
+
46
+ # Confirms whether the model and attachment arguments were passed in
47
+ # Raises an error if not
48
+ def confirm_input
49
+ end
50
+
51
+ private
52
+
53
+ def input_error
54
+ end
55
+
56
+ end
@@ -0,0 +1,25 @@
1
+ * Backup's Generated Rake Tasks
2
+ http://github.com/meskyanichi/backup
3
+
4
+ Quite simple, the generator has now generated all (currently!) possible backup options in the form of rake tasks!
5
+ Just open the desired backup-method's file (S3 or SHH or BOTH!). The basic structure of every task is already setup correctly.
6
+ Above each task you will find a description about the settings of each task. Please be sure to read this first before asking questions.
7
+
8
+ All in all this is extremely straight forward, there is no need to setup anything else inside your Rails Application other than installing the plugin,
9
+ running (this) generator and changing the values inside the rake task files.
10
+
11
+ Here is a list of the current rake tasks that are available:
12
+
13
+ backup:s3:mysql
14
+ backup:s3:sqlite3
15
+ backup:s3:assets
16
+ backup:s3:custom
17
+ backup:ssh:mysql
18
+ backup:ssh:sqlite3
19
+ backup:ssh:assets
20
+ backup:ssh:custom
21
+
22
+
23
+ So now you have rake tasks for all your essential backups.
24
+ What I like to do is use a cronjob manager gem to manage my cronjobs to trigger these rake tasks.
25
+ javan-whenever is a good gem for doing this! Try it, or any other method you can think of to trigger these rake tasks periodically.
@@ -0,0 +1,119 @@
1
+ namespace :backup do
2
+ namespace :s3 do
3
+
4
+ # => rake backup:s3:mysql
5
+ # Fill in your mysql credentials to allow Backup to create a mysql dump, and which database to make a dump from.
6
+ # Specify that you want to use :s3
7
+ # Fill in your Amazon S3 Account's Credentials (access_key_id, secret_access_key)
8
+ # Specify which bucket you wish to store your files to.
9
+ # If you wish to put specific files in subfolders inside the bucket, you may do so by doing something like this:
10
+ # :bucket => "my_bucket/subfolder1/subfolder2" etc.
11
+ task :mysql => :environment do
12
+ Backup::Mysql.new({
13
+ :mysql => {
14
+ :user => "",
15
+ :password => "",
16
+ :database => ""
17
+ },
18
+
19
+ :use => :s3,
20
+ :s3 => {
21
+ :access_key_id => '',
22
+ :secret_access_key => '',
23
+ :bucket => 'mybucket/backups/etc'
24
+ }
25
+ }).run
26
+ end
27
+
28
+ # => rake backup:s3:sqlite3
29
+ # Specify which sqlite3 file you wish to back up. This will generally be "production.sqlite3". (and this is the default, so you can remove the :file attribute if it is)
30
+ # Specify that you want to use :s3
31
+ # Fill in your Amazon S3 Account's Credentials (access_key_id, secret_access_key)
32
+ # Specify which bucket you wish to store your files to.
33
+ # If you wish to put specific files in subfolders inside the bucket, you may do so by doing something like this:
34
+ # :bucket => "my_bucket/subfolder1/subfolder2" etc.
35
+ task :sqlite3 => :environment do
36
+ Backup::Sqlite3.new({
37
+ :file => 'production.sqlite3', # "production.sqlite3" is default, can remove the whole :file attribute or change it's value
38
+
39
+ :use => :s3,
40
+ :s3 => {
41
+ :access_key_id => '',
42
+ :secret_access_key => '',
43
+ :bucket => 'mybucket/backups/etc'
44
+ }
45
+ }).run
46
+ end
47
+
48
+ # => rake backup:s3:assets
49
+ # Specify which directory (:path) (and all it's underlaying files and folders) you wish to backup.
50
+ # Specify that you want to use :s3
51
+ # Fill in your Amazon S3 Account's Credentials (access_key_id, secret_access_key)
52
+ # Specify which bucket you wish to store your files to.
53
+ # If you wish to put specific files in subfolders inside the bucket, you may do so by doing something like this:
54
+ # :bucket => "my_bucket/subfolder1/subfolder2" etc.
55
+ task :assets => :environment do
56
+ Backup::Assets.new({
57
+ :path => "#{RAILS_ROOT}/public/assets",
58
+
59
+ :use => :s3,
60
+ :s3 => {
61
+ :access_key_id => '',
62
+ :secret_access_key => '',
63
+ :bucket => 'mybucket/backups/etc'
64
+ }
65
+ }).run
66
+ end
67
+
68
+ # => rake backup:s3:custom
69
+ # This is a more complex implementation of the Backup gem.
70
+ # Might you be using a database type that is currently not supported, then you can manually create an SQL dump
71
+ # using the :command attribute. This will take either a single string, or an array of strings, depending on how many
72
+ # commands you wish to execute.
73
+ #
74
+ # Single Command
75
+ # :command => "my command"
76
+ # Multiple Commands
77
+ # :command => ["my command 1", "my command 2", "my command 3"] etc.
78
+ #
79
+ # This means you have full control over where the sql dump should be placed. But, depending on your decision, you must
80
+ # set the correct path to the file(s) (sql dumps) that have been generated.
81
+ #
82
+ # Path To File(s) Directory
83
+ # :path => "#{RAILS_ROOT}/db"
84
+ #
85
+ # Finally, you must specify which file(s) should be backed up.
86
+ # The :file attribute can take either a single string, or an array of strings to add multiple files.
87
+ #
88
+ # Select a single file to backup from the :path directory you specified
89
+ # :file => "foobar1.sql"
90
+ # Select multiple files to backup from the :path directory you specified
91
+ # :file => ["foobar1.sql", "foobar2.sql"] etc
92
+ #
93
+ # When you specify you would like to backup multiple files, it will automatically archive these as a "tar" for you and then compress it.
94
+ #
95
+ # By default, after the backup has been pushed to S3, it will remove the original files (created from your :command attribute)
96
+ # If you wish to keep these files, then add the following line:
97
+ # :keep_original_files => true
98
+ # This is set to 'false' by default, as you most likely don't want to keep these files on your production server.
99
+ #
100
+ # Just use the ":use => :s3" as usual to tell it you would like to back up these files using S3.
101
+ # And then, like in the example below, provide the S3 credentials/details to be able to connect to the server you wish to back these files up to.
102
+ task :custom => :environment do
103
+ Backup::Custom.new({
104
+ :command => ["mysqldump --quick -u root --password='' foobar > #{RAILS_ROOT}/db/foobar1.sql",
105
+ "mysqldump --quick -u root --password='' foobar > #{RAILS_ROOT}/db/foobar2.sql"],
106
+
107
+ :path => "#{RAILS_ROOT}/db",
108
+ :file => ["foobar1.sql","foobar2.sql"],
109
+
110
+ :use => :s3,
111
+ :s3 => {
112
+ :access_key_id => '',
113
+ :secret_access_key => '',
114
+ :bucket => 'mybucket/backups/etc'
115
+ }
116
+ }).run
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,114 @@
1
+ namespace :backup do
2
+ namespace :ssh do
3
+
4
+ # => rake backup:ssh:mysql
5
+ # Fill in your mysql credentials to allow Backup to create a mysql dump, and which database to make a dump from.
6
+ # Specify that you want to use :ssh
7
+ # Specify what user should connect through SSH, to what address (be it IP or an URL) and the absolute path on the backup-server
8
+ # to where Backup should store the backups.
9
+ task :mysql => :environment do
10
+ Backup::Mysql.new({
11
+ :mysql => {
12
+ :user => "",
13
+ :password => "",
14
+ :database => ""
15
+ },
16
+
17
+ :use => :ssh,
18
+ :ssh => {
19
+ :user => "root",
20
+ :ip => "mydomain.com", # or: 123.45.678.90
21
+ :path => "/var/backups/etc"
22
+ }
23
+ }).run
24
+ end
25
+
26
+ # => rake backup:ssh:sqlite3
27
+ # Specify which sqlite3 file you wish to back up. This will generally be "production.sqlite3". (and this is the default, so you can remove the :file attribute if it is)
28
+ # If your sqlite3 file is not located inside the #{RAILS_ROOT}/db folder, then add a :path => "#{RAILS_ROOT}/path/to/db/folder"
29
+ # Specify that you want to use :ssh
30
+ # Specify what user should connect through SSH, to what address (be it IP or an URL) and the absolute path on the backup-server
31
+ # to where Backup should store the backups.
32
+ task :sqlite3 => :environment do
33
+ Backup::Sqlite3.new({
34
+ :file => 'production.sqlite3', # "production.sqlite3" is default, can remove the whole :file attribute or change it's value
35
+
36
+ :use => :ssh,
37
+ :ssh => {
38
+ :user => "root",
39
+ :ip => "mydomain.com", # or: 123.45.678.90
40
+ :path => "/var/backups/etc"
41
+ }
42
+ }).run
43
+ end
44
+
45
+ # => rake backup:ssh:assets
46
+ # Specify which directory (:path) (and all it's underlaying files and folders) you wish to backup.
47
+ # Specify that you want to use :ssh
48
+ # Specify what user should connect through SSH, to what address (be it IP or an URL) and the absolute path on the backup-server
49
+ # to where Backup should store the backups.
50
+ task :assets => :environment do
51
+ Backup::Assets.new({
52
+ :path => "#{RAILS_ROOT}/public/assets",
53
+
54
+ :use => :ssh,
55
+ :ssh => {
56
+ :user => "root",
57
+ :ip => "mydomain.com", # or: 123.45.678.90
58
+ :path => "/var/backups/etc"
59
+ }
60
+ }).run
61
+ end
62
+
63
+ # => rake backup:ssh:custom
64
+ # This is a more complex implementation of the Backup gem.
65
+ # Might you be using a database type that is currently not supported, then you can manually create an SQL dump
66
+ # using the :command attribute. This will take either a single string, or an array of strings, depending on how many
67
+ # commands you wish to execute.
68
+ #
69
+ # Single Command
70
+ # :command => "my command"
71
+ # Multiple Commands
72
+ # :command => ["my command 1", "my command 2", "my command 3"] etc.
73
+ #
74
+ # This means you have full control over where the sql dump should be placed. But, depending on your decision, you must
75
+ # set the correct path to the file(s) (sql dumps) that have been generated.
76
+ #
77
+ # Path To File(s) Directory
78
+ # :path => "#{RAILS_ROOT}/db"
79
+ #
80
+ # Finally, you must specify which file(s) should be backed up.
81
+ # The :file attribute can take either a single string, or an array of strings to add multiple files.
82
+ #
83
+ # Select a single file to backup from the :path directory you specified
84
+ # :file => "foobar1.sql"
85
+ # Select multiple files to backup from the :path directory you specified
86
+ # :file => ["foobar1.sql", "foobar2.sql"] etc
87
+ #
88
+ # When you specify you would like to backup multiple files, it will automatically archive these as a "tar" for you and then compress it.
89
+ #
90
+ # By default, after the backup has been pushed to your backup server using SSH, it will remove the original files (created from your :command attribute)
91
+ # If you wish to keep these files, then add the following line:
92
+ # :keep_original_files => true
93
+ # This is set to 'false' by default, as you most likely don't want to keep these files on your production server.
94
+ #
95
+ # Just use the ":use => :ssh" as usual to tell it you would like to back up these files using SSH.
96
+ # And then, like in the example below, provide the SSH details to be able to connect to the server you wish to back these files up to.
97
+ task :custom => :environment do
98
+ Backup::Custom.new({
99
+ :command => ["mysqldump --quick -u root --password='' foobar > #{RAILS_ROOT}/db/foobar1.sql",
100
+ "mysqldump --quick -u root --password='' foobar > #{RAILS_ROOT}/db/foobar2.sql"],
101
+
102
+ :path => "#{RAILS_ROOT}/db",
103
+ :file => ["foobar1.sql", "foobar2.sql"],
104
+
105
+ :use => :ssh,
106
+ :ssh => {
107
+ :user => "root",
108
+ :ip => "mydomain.com", # or: 123.45.678.90
109
+ :path => "/var/backups/etc"
110
+ }
111
+ }).run
112
+ end
113
+ end
114
+ end
data/lib/backup.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'backup/base'
2
+ require 'backup/sqlite3'
3
+ require 'backup/mysql'
4
+ require 'backup/assets'
5
+ require 'backup/custom'
6
+ require 'backup/transfer/base'
7
+ require 'backup/transfer/s3'
8
+ require 'backup/transfer/ssh'
9
+ require 'backup/connection/base'
10
+ require 'backup/connection/s3'
11
+ require 'backup/connection/ssh'
12
+
13
+ module Backup
14
+ end
@@ -0,0 +1,31 @@
1
+ module Backup
2
+ class Assets < Backup::Base
3
+
4
+ def initialize(options = {})
5
+ super(default_options.merge(options))
6
+ setup_paths("assets/#{self.class.name.downcase.gsub('::','-')}", 'tar.gz')
7
+ end
8
+
9
+ def run
10
+ archive
11
+ compress
12
+ transfer
13
+ remove_temp_files
14
+ end
15
+
16
+ private
17
+
18
+ def archive
19
+ %x{ tar -cf #{File.join(options[:backup_path], options[:backup_file])} #{options[:path]} }
20
+ end
21
+
22
+ def compress
23
+ %x{ gzip --best #{File.join(options[:backup_path], options[:backup_file])} }
24
+ end
25
+
26
+ def default_options
27
+ { :path => "#{RAILS_ROOT}/public/assets", :file => "assets" }
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,46 @@
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
+ def setup_paths(path, type = nil)
14
+ %x{ mkdir -p #{RAILS_ROOT}/tmp/backups/#{path} }
15
+ options[:backup_path] = "#{RAILS_ROOT}/tmp/backups/#{path}"
16
+
17
+ if options[:file].is_a?(Array)
18
+ options[:backup_file] = "#{backup_time}-#{options[:file].first}.#{type}"
19
+ else
20
+ options[:backup_file] = "#{backup_time}-#{options[:file]}.#{type}"
21
+ end
22
+ end
23
+
24
+ def transfer
25
+ case options[:use]
26
+ when :s3 then Backup::Transfer::S3.new(options)
27
+ when :ssh then Backup::Transfer::SSH.new(options)
28
+ end
29
+ end
30
+
31
+ def remove_temp_files
32
+ %x{ rm #{File.join(options[:backup_path], "*")} }
33
+ end
34
+
35
+ def remove_original_file
36
+ if options[:file].is_a?(Array)
37
+ options[:file].each do |file|
38
+ %x{ rm #{File.join(options[:path], file)} }
39
+ end
40
+ else
41
+ %x{ rm #{File.join(options[:path], options[:file])} }
42
+ end
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,13 @@
1
+ module Backup
2
+ module Connection
3
+ class Base
4
+
5
+ attr_accessor :options
6
+
7
+ def initialize(options)
8
+ self.options = options
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ module Backup
2
+ module Connection
3
+ class S3 < Backup::Connection::Base
4
+
5
+ def initialize(options = {})
6
+ super(options)
7
+ end
8
+
9
+ def connect
10
+ AWS::S3::Base.establish_connection!(
11
+ :access_key_id => options[:s3][:access_key_id],
12
+ :secret_access_key => options[:s3][:secret_access_key]
13
+ )
14
+ end
15
+
16
+ def service
17
+ AWS::S3::Service
18
+ end
19
+
20
+ def bucket
21
+ AWS::S3::Bucket
22
+ end
23
+
24
+ def object
25
+ AWS::S3::S3Object
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ module Backup
2
+ module Connection
3
+ class SSH < Backup::Connection::Base
4
+
5
+ def initialize(options = {})
6
+ super(options)
7
+ end
8
+
9
+ def store
10
+ %x{ ssh #{options[:ssh][:user]}@#{options[:ssh][:ip]} mkdir -p #{options[:ssh][:path]} }
11
+ %x{ scp #{File.join(options[:backup_path], options[:backup_file])} #{options[:ssh][:user]}@#{options[:ssh][:ip]}:#{options[:ssh][:path]} }
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,55 @@
1
+ module Backup
2
+ class Custom < Backup::Base
3
+
4
+ def initialize(options = {})
5
+ super(default_options.merge(options))
6
+ setup_paths("db/#{self.class.name.downcase.gsub('::','-')}", options[:file].is_a?(Array) ? 'tar.gz' : 'gz')
7
+ end
8
+
9
+ def run
10
+ command
11
+ archive
12
+ compress
13
+ transfer
14
+ remove_temp_files
15
+ remove_original_file unless options[:keep_original_files].eql?(true)
16
+ end
17
+
18
+ private
19
+
20
+ def command
21
+ if options[:command].is_a?(Array)
22
+ options[:command].each do |command|
23
+ %x{ #{command} }
24
+ end
25
+ else
26
+ %x{ #{options[:command]} }
27
+ end
28
+ end
29
+
30
+ def archive
31
+ if options[:file].is_a?(Array)
32
+ files = options[:file].map {|file| File.join(options[:path], file)}
33
+ %x{ tar -cf #{File.join(options[:backup_path], options[:backup_file])} #{files.join(' ')} }
34
+ else
35
+ %x{ tar -cf #{File.join(options[:backup_path], options[:backup_file])} #{File.join(options[:path], options[:file])} }
36
+ end
37
+ end
38
+
39
+ def compress
40
+ if options[:file].is_a?(Array)
41
+ %x{ gzip --best #{File.join(options[:backup_path], options[:backup_file])} }
42
+ else
43
+ %x{ gzip -cv #{File.join(options[:path], options[:file])} --best > #{File.join(options[:backup_path], options[:backup_file])} }
44
+ end
45
+ end
46
+
47
+ def default_options
48
+ { :path => "",
49
+ :file => "",
50
+ :command => "",
51
+ :keep_original_files => false }
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,38 @@
1
+ module Backup
2
+ class Mysql < Backup::Base
3
+
4
+ def initialize(options = {})
5
+ super(default_options.merge(options))
6
+ setup_paths("db/#{self.class.name.downcase.gsub('::','-')}", :gz)
7
+ end
8
+
9
+ def run
10
+ make_mysql_dump
11
+ compress
12
+ transfer
13
+ remove_temp_files
14
+ end
15
+
16
+ private
17
+
18
+ def compress
19
+ %x{ gzip -cv #{File.join(options[:path], options[:file])} --best > #{File.join(options[:backup_path], options[:backup_file])} }
20
+ end
21
+
22
+ def make_mysql_dump
23
+ # => /usr/local/mysql/bin/mysqldump on Mac OS X 10.6
24
+ %x{ mysqldump --quick -u #{options[:mysql][:user]} --password='#{options[:mysql][:password]}' #{options[:mysql][:database]} > #{File.join(options[:path], options[:file])} }
25
+ end
26
+
27
+ def default_options
28
+ {:path => "#{RAILS_ROOT}/tmp/backups/db/#{self.class.name.downcase.gsub('::','-')}",
29
+ :file => "production.sql",
30
+ :mysql => {
31
+ :user => "",
32
+ :password => "",
33
+ :database => ""
34
+ }}
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,27 @@
1
+ module Backup
2
+ class Sqlite3 < Backup::Base
3
+
4
+ def initialize(options = {})
5
+ super(default_options.merge(options))
6
+ setup_paths("db/#{self.class.name.downcase.gsub('::','-')}", :gz)
7
+ end
8
+
9
+ def run
10
+ compress
11
+ transfer
12
+ remove_temp_files
13
+ end
14
+
15
+ private
16
+
17
+ def compress
18
+ %x{ gzip -cv #{File.join(options[:path], options[:file])} --best > #{File.join(options[:backup_path], options[:backup_file])} }
19
+ end
20
+
21
+ def default_options
22
+ { :path => "#{RAILS_ROOT}/db",
23
+ :file => "production.sqlite3" }
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ module Backup
2
+ module Transfer
3
+ class Base
4
+
5
+ attr_accessor :options
6
+
7
+ def initialize(options)
8
+ self.options = options
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,30 @@
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
+ s3 = Backup::Connection::S3.new(options)
11
+ s3.connect
12
+ s3.object.store(
13
+ options[:backup_file],
14
+ open(File.join(options[:backup_path], options[:backup_file])),
15
+ options[:s3][:bucket] )
16
+ end
17
+
18
+ private
19
+
20
+ def default_options
21
+ {:s3 => {
22
+ :access_key_id => '',
23
+ :secret_access_key => '',
24
+ :bucket => ''
25
+ }}
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,24 @@
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
+ ssh = Backup::Connection::SSH.new(options)
9
+ ssh.store
10
+ end
11
+
12
+ private
13
+
14
+ def default_options
15
+ {:ssh => {
16
+ :user => "",
17
+ :ip => "",
18
+ :path => "/var/backups/"
19
+ }}
20
+ end
21
+
22
+ end
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - meskyanichi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-03 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: aws-s3
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: |-
26
+ Backup is a gem/plugin that enables you to very easily create backups and transfer these to Amazon S3 or another server with SSH.
27
+ It currently supports MySQL, SQLite3 and basic Assets (documents, images, etc). The files will get tar'd / gzip'd and get a timestamp.
28
+ After creation, these files can be transferred to either Amazon S3 or any remote server through SSH.
29
+ email: meskyan@gmail.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files:
35
+ - LICENSE
36
+ - README.rdoc
37
+ files:
38
+ - .document
39
+ - .gitignore
40
+ - LICENSE
41
+ - README.rdoc
42
+ - Rakefile
43
+ - VERSION
44
+ - backup.gemspec
45
+ - generators/backup_rake_tasks/backup_rake_tasks_generator.rb
46
+ - generators/backup_rake_tasks/templates/README.rdoc
47
+ - generators/backup_rake_tasks/templates/s3.rake
48
+ - generators/backup_rake_tasks/templates/ssh.rake
49
+ - lib/backup.rb
50
+ - lib/backup/assets.rb
51
+ - lib/backup/base.rb
52
+ - lib/backup/connection/base.rb
53
+ - lib/backup/connection/s3.rb
54
+ - lib/backup/connection/ssh.rb
55
+ - lib/backup/custom.rb
56
+ - lib/backup/mysql.rb
57
+ - lib/backup/sqlite3.rb
58
+ - lib/backup/transfer/base.rb
59
+ - lib/backup/transfer/s3.rb
60
+ - lib/backup/transfer/ssh.rb
61
+ has_rdoc: true
62
+ homepage: http://github.com/meskyanichi/backup
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --charset=UTF-8
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ requirements: []
83
+
84
+ rubyforge_project:
85
+ rubygems_version: 1.3.5
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Backup is a gem/plugin that enables you to very easily create backups and transfer these to Amazon S3 or another server with SSH.
89
+ test_files: []
90
+