aurels-backuper 0.2

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.
Files changed (3) hide show
  1. data/README +14 -0
  2. data/lib/backuper.rb +109 -0
  3. metadata +54 -0
data/README ADDED
@@ -0,0 +1,14 @@
1
+ A simple server backup tool written in Ruby.
2
+
3
+ To install:
4
+
5
+ sudo gem list -a http://gems.github.com
6
+ sudo gem install aurels-backuper
7
+
8
+ To use:
9
+
10
+ - Create a script file which requires 'backuper' and create a new Backuper instance
11
+ - Configure cron to run the script as root when you want
12
+
13
+ Example of backup script in examples/backup.rb (use it as a base template)
14
+
data/lib/backuper.rb ADDED
@@ -0,0 +1,109 @@
1
+ class Backuper
2
+
3
+ TMP_DIR = "/backuper"
4
+
5
+ attr_writer :mysql_params
6
+ attr_writer :ftp_params
7
+
8
+ def initialize
9
+ @config_files = []
10
+ @config_dirs = []
11
+ @sqlite_databases = []
12
+ @mysql_params = {}
13
+ @mysql_databases = []
14
+ @data_dirs = []
15
+ @ftp_params = {}
16
+ @archive_filename = "backup-#{Time.now.strftime('%Y-%m-%d')}-#{Time.now.to_i}.tar.gz"
17
+ end
18
+
19
+ def config_file(path)
20
+ @config_files << path
21
+ end
22
+
23
+ def config_dir(path)
24
+ @config_dirs << path
25
+ end
26
+
27
+ def sqlite_database(path)
28
+ @sqlite_databases << path
29
+ end
30
+
31
+ def mysql_database(name)
32
+ @mysql_databases << name
33
+ end
34
+
35
+ def data_dir(path)
36
+ @data_dirs << path
37
+ end
38
+
39
+ def perform!
40
+ self.prepare
41
+ self.backup_config_files_and_dirs
42
+ self.backup_sqlite_databases
43
+ self.backup_mysql_databases
44
+ self.create_archive
45
+ self.upload_archive
46
+ self.cleanup
47
+ end
48
+
49
+ protected
50
+
51
+ def prepare
52
+ run "mkdir #{TMP_DIR}"
53
+ run "mkdir #{File.join(TMP_DIR, 'config_files')}"
54
+ run "mkdir #{File.join(TMP_DIR, 'databases')}"
55
+ run "mkdir #{File.join(TMP_DIR, 'databases', 'sqlite')}"
56
+ run "mkdir #{File.join(TMP_DIR, 'databases', 'mysql')}"
57
+ end
58
+
59
+ def backup_config_files_and_dirs
60
+ @config_files.each do |config_file|
61
+ run "cp #{config_file} #{File.join(TMP_DIR, 'config_files')}"
62
+ end
63
+ @config_dirs.each do |config_dir|
64
+ run "cp -R #{config_dir} #{File.join(TMP_DIR, 'config_files')}"
65
+ end
66
+ end
67
+
68
+ def backup_sqlite_databases
69
+ @sqlite_databases.each do |db|
70
+ run "cp #{db} #{File.join(TMP_DIR, 'databases', 'sqlite')}"
71
+ end
72
+ end
73
+
74
+ def backup_mysql_databases
75
+ unless @mysql_params.empty?
76
+ @mysql_databases.each do |db|
77
+ run "mysqldump #{db} --user=#{@mysql_params[:user]} --password=#{@mysql_params[:password]} > #{File.join(TMP_DIR, 'databases', 'mysql', "#{db}.sql")}"
78
+ end
79
+ else
80
+ say "MySQL databases backup not done because MySQL params are unset"
81
+ end
82
+ end
83
+
84
+ def create_archive
85
+ run "tar czf /tmp/#{@archive_filename} #{TMP_DIR}"
86
+ end
87
+
88
+ def upload_archive
89
+ unless @ftp_params.empty?
90
+ run "ncftpput -u #{@ftp_params[:user]} -p #{@ftp_params[:password]} #{@ftp_params[:host]} #{@ftp_params[:path]} /tmp/#{@archive_filename}"
91
+ else
92
+ say "Upload to FTP server not done because FTP params are unset"
93
+ end
94
+ end
95
+
96
+ def cleanup
97
+ run "rm -rf #{TMP_DIR}"
98
+ run "rm -f /tmp/#{@archive_filename}"
99
+ end
100
+
101
+ def say(str)
102
+ puts "#{str}"
103
+ end
104
+
105
+ def run(cmd)
106
+ puts "#{cmd}"
107
+ system("#{cmd}")
108
+ end
109
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aurels-backuper
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.2"
5
+ platform: ruby
6
+ authors:
7
+ - "Aur\xC3\xA9lien Malisart"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simple unix server backup tool
17
+ email: aurelien.malisart@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - lib/backuper.rb
27
+ has_rdoc: false
28
+ homepage: http://github.com/aurels/backuper
29
+ post_install_message:
30
+ rdoc_options: []
31
+
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: "0"
39
+ version:
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ requirements: []
47
+
48
+ rubyforge_project:
49
+ rubygems_version: 1.2.0
50
+ signing_key:
51
+ specification_version: 2
52
+ summary: Simple unix server backup tool
53
+ test_files: []
54
+