db_backup 1.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/bin/db_backup.rb +133 -0
- data/etc/db_backup.yaml +5 -0
- data/lib/dbbackup_version.rb +3 -0
- data/man/db_backup.1 +85 -0
- metadata +114 -0
data/bin/db_backup.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
#!/usr/bin/env ruby -w
|
2
|
+
|
3
|
+
# ********************************************************************************
|
4
|
+
#
|
5
|
+
# File: db_backup.rb
|
6
|
+
# Version: 1.0.0
|
7
|
+
# Author: Lee Choon Siong (lee@choonsiong.com)
|
8
|
+
#
|
9
|
+
# Backup one or more MySQL database(s).
|
10
|
+
#
|
11
|
+
# ********************************************************************************
|
12
|
+
|
13
|
+
$LOAD_PATH.push File.expand_path("../../lib", __FILE__)
|
14
|
+
|
15
|
+
# --------------------------------------------------------------------------------------------------------------------------------------------- modules
|
16
|
+
require 'optparse'
|
17
|
+
require 'English'
|
18
|
+
require 'open3'
|
19
|
+
require 'yaml'
|
20
|
+
require 'dbbackup_version'
|
21
|
+
|
22
|
+
# --------------------------------------------------------------------------------------------------------------------------------------------- global options
|
23
|
+
options = {
|
24
|
+
gzip: true,
|
25
|
+
force: false,
|
26
|
+
end_of_iteration: false,
|
27
|
+
user: nil,
|
28
|
+
password: nil,
|
29
|
+
}
|
30
|
+
|
31
|
+
#CONFIG_FILE = File.join(ENV['HOME'],'.db_backup.rc.yaml')
|
32
|
+
CONFIG_FILE = File.join('./etc/db_backup.yaml') # --------------------------------------------------------------------------------------------- configuration file
|
33
|
+
|
34
|
+
if File.exists? CONFIG_FILE
|
35
|
+
config_options = YAML.load_file(CONFIG_FILE)
|
36
|
+
options.merge!(config_options)
|
37
|
+
else
|
38
|
+
File.open(CONFIG_FILE, 'w') { |file| YAML::dump(options, file) }
|
39
|
+
STDERR.puts "Initialized configuration file in #{CONFIG_FILE}"
|
40
|
+
end
|
41
|
+
|
42
|
+
# --------------------------------------------------------------------------------------------------------------------------------------------- OptionParser start
|
43
|
+
option_parser = OptionParser.new do |opts|
|
44
|
+
executable_name = File.basename($PROGRAM_NAME)
|
45
|
+
opts.banner = "Backup one or more MySQL databases
|
46
|
+
|
47
|
+
Usage: #{executable_name} [options] database_name
|
48
|
+
|
49
|
+
Version: #{Dbbackup::VERSION}
|
50
|
+
|
51
|
+
Run man man/db_backup.1 for man page
|
52
|
+
"
|
53
|
+
|
54
|
+
# end of iteration switch
|
55
|
+
opts.on("-i", "--end-of-iteration", "Indicate that this backup is an \"iteration\" backup") do
|
56
|
+
options[:end_of_iteration] = true
|
57
|
+
end
|
58
|
+
|
59
|
+
# username flag
|
60
|
+
opts.on("-u USER", "--username", "Database username, in first.last format", /^[^.]+\.[^.]+$/) do |user|
|
61
|
+
options[:user] = user
|
62
|
+
end
|
63
|
+
|
64
|
+
# password flag
|
65
|
+
opts.on("-p PASSWORD", "--password", "Database password") do |password|
|
66
|
+
options[:password] = password
|
67
|
+
end
|
68
|
+
|
69
|
+
# no-gzip switch
|
70
|
+
opts.on("--no-gzip", "Do not compress the backup file") do
|
71
|
+
options[:gzip] = false
|
72
|
+
end
|
73
|
+
|
74
|
+
# force overwrite switch
|
75
|
+
opts.on("--[no-]force", "Overwrite existing files") do |force|
|
76
|
+
options[:force] = force
|
77
|
+
end
|
78
|
+
end
|
79
|
+
# --------------------------------------------------------------------------------------------------------------------------------------------- OptionParser end
|
80
|
+
|
81
|
+
exit_status = 0
|
82
|
+
|
83
|
+
begin
|
84
|
+
option_parser.parse!
|
85
|
+
if ARGV.empty?
|
86
|
+
STDERR.puts "Error: you must supply a database name"
|
87
|
+
puts
|
88
|
+
puts option_parser.help
|
89
|
+
exit_status |= 0b0010 # exit 2
|
90
|
+
end
|
91
|
+
rescue OptionParser::InvalidArgument => ex
|
92
|
+
STDERR.puts ex.message
|
93
|
+
STDERR.puts option_parser
|
94
|
+
exit_status |= 0b0001 # exit 1
|
95
|
+
end
|
96
|
+
|
97
|
+
exit exit_status unless exit_status == 0 # ---------------------------------------------------------------------------------------------------- Try exit
|
98
|
+
|
99
|
+
# --------------------------------------------------------------------------------------------------------------------------------------------- Main
|
100
|
+
auth = ""
|
101
|
+
auth += "-u#{options[:user]} " if options[:user]
|
102
|
+
auth += "-p#{options[:password]} " if options[:password]
|
103
|
+
|
104
|
+
database_name = ARGV[0]
|
105
|
+
output_file = "#{database_name}" + "_" + Time.now.strftime('%Y%m%d') + "_" + "#{$PROCESS_ID}" + ".sql"
|
106
|
+
|
107
|
+
command = "/usr/local/mysql/bin/mysqldump " + "#{auth}#{database_name} > ./out/#{output_file}" # ---------------------------------------------- MySQL command
|
108
|
+
|
109
|
+
if File.exists? output_file
|
110
|
+
if options[:force]
|
111
|
+
STDERR.puts "Warning: Overwriting #{output_file}"
|
112
|
+
else
|
113
|
+
STDERR.puts "Error: #{output_file} exists, use --force to overwrite"
|
114
|
+
exit 1 # ---------------------------------------------------------------------------------------------------------------------------------- Try exit
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Not sure whether this should put here or other place...
|
119
|
+
Signal.trap("SIGINT") do
|
120
|
+
FileUtils.rm output_file
|
121
|
+
end
|
122
|
+
|
123
|
+
unless ENV['NO_RUN']
|
124
|
+
puts "Running '#{command}'"
|
125
|
+
stdout_str, stderr_str, status = Open3.capture3(command)
|
126
|
+
|
127
|
+
unless status.success?
|
128
|
+
STDERR.puts "Error: There was a problem running '#{command}'"
|
129
|
+
STDERR.puts stderr_str.gsub(/^mysqldump: /, '')
|
130
|
+
exit 1 # ------------------------------------------------------------------------------------------------------------------------------------ Try exit
|
131
|
+
end
|
132
|
+
end
|
133
|
+
# --------------------------------------------------------------------------------------------------------------------------------------------- Program end
|
data/etc/db_backup.yaml
ADDED
data/man/db_backup.1
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
.\" generated with Ronn/v0.7.3
|
2
|
+
.\" http://github.com/rtomayko/ronn/tree/0.7.3
|
3
|
+
.
|
4
|
+
.TH "DB_BACKUP\.RB" "1" "April 2012" "" ""
|
5
|
+
.
|
6
|
+
.SH "NAME"
|
7
|
+
\fBdb_backup\.rb\fR \- backup one or more MySQL databases
|
8
|
+
.
|
9
|
+
.SH "SYNOPSIS"
|
10
|
+
\fBdb_backup\.rb\fR \fIdatabase_name\fR
|
11
|
+
.
|
12
|
+
.br
|
13
|
+
\fBdb_backup\.rb\fR \fB\-u username\fR \fB\-p password\fR \fIdatabase_name\fR
|
14
|
+
.
|
15
|
+
.br
|
16
|
+
\fBdb_backup\.rb\fR \fB\-i\fR|\fB\-\-end\-of\-iteration\fR \fIdatabase_name\fR
|
17
|
+
.
|
18
|
+
.SH "DESCRIPTION"
|
19
|
+
\fBdb_backup\.rb\fR is a simple command\-line tool for backing up a MySQL database\. It does so safely and quietly, using a sensible name for the backup files, so it\'s perfect for use with cron as a daily backup\.
|
20
|
+
.
|
21
|
+
.P
|
22
|
+
By default, \fBdb_backup\.rb\fR makes a daily backup and names the resulting backup file with the date\. \fBdb_backup\.rb\fR also understands our development process, so if you specify the \fB\-\-end\-of\-iteration\fR flag, the backup will be named differently than for a daily backup\. This will allow you to easily keep one backup per iteration, easily identifying it, and differentiate it from daily backups\.
|
23
|
+
.
|
24
|
+
.P
|
25
|
+
By default, \fBdb_backup\.rb\fR will use your database credentials in \fB~/\.my\.cnf\fR, however, you can override either the username or password (or both) via the \fB\-u\fR and \fB\-p\fR flags, respectively\.
|
26
|
+
.
|
27
|
+
.P
|
28
|
+
Finally, \fBdb_backup\.rb\fR will add a sanity check on your username, to make sure it fits with our corporate standard format of \fBfirst\.last\fR\.
|
29
|
+
.
|
30
|
+
.SH "FILES"
|
31
|
+
\fB~/\.my\.cnf\fR is used for authentication if \fB\-u\fR or \fB\-p\fR is omitted\.
|
32
|
+
.
|
33
|
+
.SH "OPTIONS"
|
34
|
+
.
|
35
|
+
.TP
|
36
|
+
\fB\-i\fR, \fB\-\-end\-of\-iteration\fR
|
37
|
+
Indicate that this backup is an "end of iteration" backup\.
|
38
|
+
.
|
39
|
+
.TP
|
40
|
+
\fB\-u USER\fR
|
41
|
+
Database username, in first\.last format \fB~/my\.cnf\fR is not correct
|
42
|
+
.
|
43
|
+
.TP
|
44
|
+
\fB\-p PASSWORD\fR
|
45
|
+
Database password
|
46
|
+
.
|
47
|
+
.SH "EXAMPLES"
|
48
|
+
Backup the database "big_client"
|
49
|
+
.
|
50
|
+
.IP "" 4
|
51
|
+
.
|
52
|
+
.nf
|
53
|
+
|
54
|
+
$ db_backup\.rb big_client
|
55
|
+
.
|
56
|
+
.fi
|
57
|
+
.
|
58
|
+
.IP "" 0
|
59
|
+
.
|
60
|
+
.P
|
61
|
+
Backup the database "small_client", for which different credentials are required:
|
62
|
+
.
|
63
|
+
.IP "" 4
|
64
|
+
.
|
65
|
+
.nf
|
66
|
+
|
67
|
+
$ db_backup\.rb \-u dave\.thomas \-p d4v3 small_client
|
68
|
+
.
|
69
|
+
.fi
|
70
|
+
.
|
71
|
+
.IP "" 0
|
72
|
+
.
|
73
|
+
.P
|
74
|
+
Make an iteration backup of the "big_client" database:
|
75
|
+
.
|
76
|
+
.IP "" 4
|
77
|
+
.
|
78
|
+
.nf
|
79
|
+
|
80
|
+
$ db_backup\.rb \-i big_client
|
81
|
+
.
|
82
|
+
.fi
|
83
|
+
.
|
84
|
+
.IP "" 0
|
85
|
+
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: db_backup
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lee Choon Siong
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: optparse
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: open3
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: yaml
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: English
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Dbbackup allows you to backup MySQL database on the command line
|
79
|
+
email:
|
80
|
+
- lee at choonsiong.com
|
81
|
+
executables:
|
82
|
+
- db_backup.rb
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- bin/db_backup.rb
|
87
|
+
- etc/db_backup.yaml
|
88
|
+
- lib/dbbackup_version.rb
|
89
|
+
- man/db_backup.1
|
90
|
+
homepage: http://lee.choonsiong.com
|
91
|
+
licenses: []
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project: db_backup
|
110
|
+
rubygems_version: 1.8.21
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: A lightweighted database backup application
|
114
|
+
test_files: []
|