backup_zh 4.0.3.1

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 (127) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +24 -0
  3. data/README.md +21 -0
  4. data/bin/backup_zh +5 -0
  5. data/lib/backup.rb +137 -0
  6. data/lib/backup/archive.rb +170 -0
  7. data/lib/backup/binder.rb +22 -0
  8. data/lib/backup/cleaner.rb +116 -0
  9. data/lib/backup/cli.rb +364 -0
  10. data/lib/backup/cloud_io/base.rb +41 -0
  11. data/lib/backup/cloud_io/cloud_files.rb +298 -0
  12. data/lib/backup/cloud_io/qi_niu.rb +93 -0
  13. data/lib/backup/cloud_io/s3.rb +260 -0
  14. data/lib/backup/compressor/base.rb +35 -0
  15. data/lib/backup/compressor/bzip2.rb +39 -0
  16. data/lib/backup/compressor/custom.rb +53 -0
  17. data/lib/backup/compressor/gzip.rb +74 -0
  18. data/lib/backup/config.rb +119 -0
  19. data/lib/backup/config/dsl.rb +103 -0
  20. data/lib/backup/config/helpers.rb +143 -0
  21. data/lib/backup/database/base.rb +85 -0
  22. data/lib/backup/database/mongodb.rb +186 -0
  23. data/lib/backup/database/mysql.rb +181 -0
  24. data/lib/backup/database/openldap.rb +95 -0
  25. data/lib/backup/database/postgresql.rb +133 -0
  26. data/lib/backup/database/redis.rb +179 -0
  27. data/lib/backup/database/riak.rb +82 -0
  28. data/lib/backup/encryptor/base.rb +29 -0
  29. data/lib/backup/encryptor/gpg.rb +747 -0
  30. data/lib/backup/encryptor/open_ssl.rb +72 -0
  31. data/lib/backup/errors.rb +58 -0
  32. data/lib/backup/logger.rb +199 -0
  33. data/lib/backup/logger/console.rb +51 -0
  34. data/lib/backup/logger/fog_adapter.rb +29 -0
  35. data/lib/backup/logger/logfile.rb +133 -0
  36. data/lib/backup/logger/syslog.rb +116 -0
  37. data/lib/backup/model.rb +454 -0
  38. data/lib/backup/notifier/base.rb +98 -0
  39. data/lib/backup/notifier/campfire.rb +69 -0
  40. data/lib/backup/notifier/flowdock.rb +102 -0
  41. data/lib/backup/notifier/hipchat.rb +93 -0
  42. data/lib/backup/notifier/http_post.rb +122 -0
  43. data/lib/backup/notifier/mail.rb +238 -0
  44. data/lib/backup/notifier/nagios.rb +74 -0
  45. data/lib/backup/notifier/prowl.rb +69 -0
  46. data/lib/backup/notifier/pushover.rb +80 -0
  47. data/lib/backup/notifier/slack.rb +158 -0
  48. data/lib/backup/notifier/twitter.rb +64 -0
  49. data/lib/backup/notifier/zabbix.rb +68 -0
  50. data/lib/backup/package.rb +51 -0
  51. data/lib/backup/packager.rb +101 -0
  52. data/lib/backup/pipeline.rb +124 -0
  53. data/lib/backup/splitter.rb +76 -0
  54. data/lib/backup/storage/base.rb +57 -0
  55. data/lib/backup/storage/cloud_files.rb +158 -0
  56. data/lib/backup/storage/cycler.rb +65 -0
  57. data/lib/backup/storage/dropbox.rb +236 -0
  58. data/lib/backup/storage/ftp.rb +98 -0
  59. data/lib/backup/storage/local.rb +64 -0
  60. data/lib/backup/storage/ninefold.rb +74 -0
  61. data/lib/backup/storage/qi_niu.rb +70 -0
  62. data/lib/backup/storage/rsync.rb +248 -0
  63. data/lib/backup/storage/s3.rb +154 -0
  64. data/lib/backup/storage/scp.rb +67 -0
  65. data/lib/backup/storage/sftp.rb +82 -0
  66. data/lib/backup/syncer/base.rb +70 -0
  67. data/lib/backup/syncer/cloud/base.rb +179 -0
  68. data/lib/backup/syncer/cloud/cloud_files.rb +83 -0
  69. data/lib/backup/syncer/cloud/local_file.rb +100 -0
  70. data/lib/backup/syncer/cloud/s3.rb +110 -0
  71. data/lib/backup/syncer/rsync/base.rb +48 -0
  72. data/lib/backup/syncer/rsync/local.rb +31 -0
  73. data/lib/backup/syncer/rsync/pull.rb +51 -0
  74. data/lib/backup/syncer/rsync/push.rb +205 -0
  75. data/lib/backup/template.rb +46 -0
  76. data/lib/backup/utilities.rb +224 -0
  77. data/lib/backup/version.rb +5 -0
  78. data/templates/cli/archive +28 -0
  79. data/templates/cli/compressor/bzip2 +4 -0
  80. data/templates/cli/compressor/custom +7 -0
  81. data/templates/cli/compressor/gzip +4 -0
  82. data/templates/cli/config +123 -0
  83. data/templates/cli/databases/mongodb +15 -0
  84. data/templates/cli/databases/mysql +18 -0
  85. data/templates/cli/databases/openldap +24 -0
  86. data/templates/cli/databases/postgresql +16 -0
  87. data/templates/cli/databases/redis +16 -0
  88. data/templates/cli/databases/riak +17 -0
  89. data/templates/cli/encryptor/gpg +27 -0
  90. data/templates/cli/encryptor/openssl +9 -0
  91. data/templates/cli/model +26 -0
  92. data/templates/cli/notifier/zabbix +15 -0
  93. data/templates/cli/notifiers/campfire +12 -0
  94. data/templates/cli/notifiers/flowdock +16 -0
  95. data/templates/cli/notifiers/hipchat +15 -0
  96. data/templates/cli/notifiers/http_post +32 -0
  97. data/templates/cli/notifiers/mail +21 -0
  98. data/templates/cli/notifiers/nagios +13 -0
  99. data/templates/cli/notifiers/prowl +11 -0
  100. data/templates/cli/notifiers/pushover +11 -0
  101. data/templates/cli/notifiers/slack +23 -0
  102. data/templates/cli/notifiers/twitter +13 -0
  103. data/templates/cli/splitter +7 -0
  104. data/templates/cli/storages/cloud_files +11 -0
  105. data/templates/cli/storages/dropbox +19 -0
  106. data/templates/cli/storages/ftp +12 -0
  107. data/templates/cli/storages/local +7 -0
  108. data/templates/cli/storages/ninefold +9 -0
  109. data/templates/cli/storages/qi_niu +9 -0
  110. data/templates/cli/storages/rsync +17 -0
  111. data/templates/cli/storages/s3 +14 -0
  112. data/templates/cli/storages/scp +14 -0
  113. data/templates/cli/storages/sftp +14 -0
  114. data/templates/cli/syncers/cloud_files +22 -0
  115. data/templates/cli/syncers/rsync_local +20 -0
  116. data/templates/cli/syncers/rsync_pull +28 -0
  117. data/templates/cli/syncers/rsync_push +28 -0
  118. data/templates/cli/syncers/s3 +27 -0
  119. data/templates/general/links +3 -0
  120. data/templates/general/version.erb +2 -0
  121. data/templates/notifier/mail/failure.erb +16 -0
  122. data/templates/notifier/mail/success.erb +16 -0
  123. data/templates/notifier/mail/warning.erb +16 -0
  124. data/templates/storage/dropbox/authorization_url.erb +6 -0
  125. data/templates/storage/dropbox/authorized.erb +4 -0
  126. data/templates/storage/dropbox/cache_file_written.erb +10 -0
  127. metadata +1124 -0
@@ -0,0 +1,186 @@
1
+ # encoding: utf-8
2
+
3
+ module Backup
4
+ module Database
5
+ class MongoDB < Base
6
+ class Error < Backup::Error; end
7
+
8
+ ##
9
+ # Name of the database that needs to get dumped
10
+ attr_accessor :name
11
+
12
+ ##
13
+ # Credentials for the specified database
14
+ attr_accessor :username, :password
15
+
16
+ ##
17
+ # Connectivity options
18
+ attr_accessor :host, :port
19
+
20
+ ##
21
+ # IPv6 support (disabled by default)
22
+ attr_accessor :ipv6
23
+
24
+ ##
25
+ # Collections to dump, collections that aren't specified won't get dumped
26
+ attr_accessor :only_collections
27
+
28
+ ##
29
+ # Additional "mongodump" options
30
+ attr_accessor :additional_options
31
+
32
+ ##
33
+ # Forces mongod to flush all pending write operations to the disk and
34
+ # locks the entire mongod instance to prevent additional writes until the
35
+ # dump is complete.
36
+ #
37
+ # Note that if Profiling is enabled, this will disable it and will not
38
+ # re-enable it after the dump is complete.
39
+ attr_accessor :lock
40
+
41
+ ##
42
+ # Creates a dump of the database that includes an oplog, to create a
43
+ # point-in-time snapshot of the state of a mongod instance.
44
+ #
45
+ # If this option is used, you would not use the `lock` option.
46
+ #
47
+ # This will only work against nodes that maintain a oplog.
48
+ # This includes all members of a replica set, as well as master nodes in
49
+ # master/slave replication deployments.
50
+ attr_accessor :oplog
51
+
52
+ def initialize(model, database_id = nil, &block)
53
+ super
54
+ instance_eval(&block) if block_given?
55
+ end
56
+
57
+ def perform!
58
+ super
59
+
60
+ lock_database if @lock
61
+ dump!
62
+ package!
63
+
64
+ ensure
65
+ unlock_database if @lock
66
+ end
67
+
68
+ private
69
+
70
+ ##
71
+ # Performs all required mongodump commands, dumping the output files
72
+ # into the +dump_packaging_path+ directory for packaging.
73
+ def dump!
74
+ FileUtils.mkdir_p dump_packaging_path
75
+
76
+ collections = Array(only_collections)
77
+ if collections.empty?
78
+ run(mongodump)
79
+ else
80
+ collections.each do |collection|
81
+ run("#{ mongodump } --collection='#{ collection }'")
82
+ end
83
+ end
84
+ end
85
+
86
+ ##
87
+ # Creates a tar archive of the +dump_packaging_path+ directory
88
+ # and stores it in the +dump_path+ using +dump_filename+.
89
+ #
90
+ # <trigger>/databases/MongoDB[-<database_id>].tar[.gz]
91
+ #
92
+ # If successful, +dump_packaging_path+ is removed.
93
+ def package!
94
+ pipeline = Pipeline.new
95
+ dump_ext = 'tar'
96
+
97
+ pipeline << "#{ utility(:tar) } -cf - " +
98
+ "-C '#{ dump_path }' '#{ dump_filename }'"
99
+
100
+ model.compressor.compress_with do |command, ext|
101
+ pipeline << command
102
+ dump_ext << ext
103
+ end if model.compressor
104
+
105
+ pipeline << "#{ utility(:cat) } > " +
106
+ "'#{ File.join(dump_path, dump_filename) }.#{ dump_ext }'"
107
+
108
+ pipeline.run
109
+ if pipeline.success?
110
+ FileUtils.rm_rf dump_packaging_path
111
+ log!(:finished)
112
+ else
113
+ raise Error, "Dump Failed!\n" + pipeline.error_messages
114
+ end
115
+ end
116
+
117
+ def dump_packaging_path
118
+ File.join(dump_path, dump_filename)
119
+ end
120
+
121
+ def mongodump
122
+ "#{ utility(:mongodump) } #{ name_option } #{ credential_options } " +
123
+ "#{ connectivity_options } #{ ipv6_option } #{ oplog_option } " +
124
+ "#{ user_options } --out='#{ dump_packaging_path }'"
125
+ end
126
+
127
+ def name_option
128
+ "--db='#{ name }'" if name
129
+ end
130
+
131
+ def credential_options
132
+ opts = []
133
+ opts << "--username='#{ username }'" if username
134
+ opts << "--password='#{ password }'" if password
135
+ opts.join(' ')
136
+ end
137
+
138
+ def connectivity_options
139
+ opts = []
140
+ opts << "--host='#{ host }'" if host
141
+ opts << "--port='#{ port }'" if port
142
+ opts.join(' ')
143
+ end
144
+
145
+ def ipv6_option
146
+ '--ipv6' if ipv6
147
+ end
148
+
149
+ def oplog_option
150
+ '--oplog' if oplog
151
+ end
152
+
153
+ def user_options
154
+ Array(additional_options).join(' ')
155
+ end
156
+
157
+ def lock_database
158
+ lock_command = <<-EOS.gsub(/^ +/, '')
159
+ echo 'use admin
160
+ db.setProfilingLevel(0)
161
+ db.fsyncLock()' | #{ mongo_shell }
162
+ EOS
163
+
164
+ run(lock_command)
165
+ end
166
+
167
+ def unlock_database
168
+ unlock_command = <<-EOS.gsub(/^ +/, '')
169
+ echo 'use admin
170
+ db.fsyncUnlock()' | #{ mongo_shell }
171
+ EOS
172
+
173
+ run(unlock_command)
174
+ end
175
+
176
+ def mongo_shell
177
+ cmd = "#{ utility(:mongo) } #{ connectivity_options }".rstrip
178
+ cmd << " #{ credential_options }".rstrip
179
+ cmd << " #{ ipv6_option }".rstrip
180
+ cmd << " '#{ name }'" if name
181
+ cmd
182
+ end
183
+
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,181 @@
1
+ # encoding: utf-8
2
+ require 'shellwords'
3
+
4
+ module Backup
5
+ module Database
6
+ class MySQL < Base
7
+ class Error < Backup::Error; end
8
+
9
+ ##
10
+ # Name of the database that needs to get dumped
11
+ # To dump all databases, set this to `:all` or leave blank.
12
+ attr_accessor :name
13
+
14
+ ##
15
+ # Credentials for the specified database
16
+ attr_accessor :username, :password
17
+
18
+ ##
19
+ # Connectivity options
20
+ attr_accessor :host, :port, :socket
21
+
22
+ ##
23
+ # Tables to skip while dumping the database
24
+ #
25
+ # If `name` is set to :all (or not specified), these must include
26
+ # a database name. e.g. 'name.table'.
27
+ # If `name` is given, these may simply be table names.
28
+ attr_accessor :skip_tables
29
+
30
+ ##
31
+ # Tables to dump. This in only valid if `name` is specified.
32
+ # If none are given, the entire database will be dumped.
33
+ attr_accessor :only_tables
34
+
35
+ ##
36
+ # Additional "mysqldump" or "innobackupex (backup creation)" options
37
+ attr_accessor :additional_options
38
+
39
+ ##
40
+ # Additional innobackupex log preparation phase ("apply-logs") options
41
+ attr_accessor :prepare_options
42
+
43
+ ##
44
+ # Default is :mysqldump (which is built in MySQL and generates
45
+ # a textual SQL file), but can be changed to :innobackupex, which
46
+ # has more feasible restore times for large databases.
47
+ # See: http://www.percona.com/doc/percona-xtrabackup/
48
+ attr_accessor :backup_engine
49
+
50
+ ##
51
+ # If set the backup engine command block is executed as the given user
52
+ attr_accessor :sudo_user
53
+
54
+ ##
55
+ # If set, do not suppress innobackupdb output (useful for debugging)
56
+ attr_accessor :verbose
57
+
58
+ def initialize(model, database_id = nil, &block)
59
+ super
60
+ instance_eval(&block) if block_given?
61
+
62
+ @name ||= :all
63
+ @backup_engine ||= :mysqldump
64
+ end
65
+
66
+ ##
67
+ # Performs the mysqldump or innobackupex command and outputs
68
+ # the dump file in the +dump_path+ using +dump_filename+.
69
+ #
70
+ # <trigger>/databases/MySQL[-<database_id>].[sql|tar][.gz]
71
+ def perform!
72
+ super
73
+
74
+ pipeline = Pipeline.new
75
+ dump_ext = sql_backup? ? 'sql' : 'tar'
76
+
77
+ pipeline << sudo_option(sql_backup? ? mysqldump : innobackupex)
78
+
79
+ model.compressor.compress_with do |command, ext|
80
+ pipeline << command
81
+ dump_ext << ext
82
+ end if model.compressor
83
+
84
+ pipeline << "#{ utility(:cat) } > " +
85
+ "'#{ File.join(dump_path, dump_filename) }.#{ dump_ext }'"
86
+
87
+ pipeline.run
88
+ if pipeline.success?
89
+ log!(:finished)
90
+ else
91
+ raise Error, "Dump Failed!\n" + pipeline.error_messages
92
+ end
93
+ end
94
+
95
+ private
96
+
97
+ def mysqldump
98
+ "#{ utility(:mysqldump) } #{ credential_options } " +
99
+ "#{ connectivity_options } #{ user_options } #{ name_option } " +
100
+ "#{ tables_to_dump } #{ tables_to_skip }"
101
+ end
102
+
103
+ def credential_options
104
+ opts = []
105
+ opts << "--user=#{ Shellwords.escape(username) }" if username
106
+ opts << "--password=#{ Shellwords.escape(password) }" if password
107
+ opts.join(' ')
108
+ end
109
+
110
+ def connectivity_options
111
+ return "--socket='#{ socket }'" if socket
112
+
113
+ opts = []
114
+ opts << "--host='#{ host }'" if host
115
+ opts << "--port='#{ port }'" if port
116
+ opts.join(' ')
117
+ end
118
+
119
+ def user_options
120
+ Array(additional_options).join(' ')
121
+ end
122
+
123
+ def user_prepare_options
124
+ Array(prepare_options).join(' ')
125
+ end
126
+
127
+ def name_option
128
+ dump_all? ? '--all-databases' : name
129
+ end
130
+
131
+ def tables_to_dump
132
+ Array(only_tables).join(' ') unless dump_all?
133
+ end
134
+
135
+ def tables_to_skip
136
+ Array(skip_tables).map do |table|
137
+ table = (dump_all? || table['.']) ? table : "#{ name }.#{ table }"
138
+ "--ignore-table='#{ table }'"
139
+ end.join(' ')
140
+ end
141
+
142
+ def dump_all?
143
+ name == :all
144
+ end
145
+
146
+ def sql_backup?
147
+ backup_engine.to_sym == :mysqldump
148
+ end
149
+
150
+ def innobackupex
151
+ # Creation phase
152
+ "#{ utility(:innobackupex) } #{ credential_options } " +
153
+ "#{ connectivity_options } #{ user_options } " +
154
+ "--no-timestamp #{ temp_dir } #{ quiet_option } && " +
155
+ # Log applying phase (prepare for restore)
156
+ "#{ utility(:innobackupex) } --apply-log #{ temp_dir } " +
157
+ "#{ user_prepare_options } #{ quiet_option } && " +
158
+ # Move files to tar-ed stream on stdout
159
+ "#{ utility(:tar) } --remove-files -cf - " +
160
+ "-C #{ File.dirname(temp_dir) } #{ File.basename(temp_dir) }"
161
+ end
162
+
163
+ def sudo_option(command_block)
164
+ return command_block unless sudo_user
165
+
166
+ "sudo -s -u #{ sudo_user } -- <<END_OF_SUDO\n" +
167
+ "#{command_block}\n" +
168
+ "END_OF_SUDO\n"
169
+ end
170
+
171
+ def quiet_option
172
+ verbose ? "" : " 2> /dev/null "
173
+ end
174
+
175
+ def temp_dir
176
+ File.join(dump_path, dump_filename + ".bkpdir")
177
+ end
178
+
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,95 @@
1
+ # encoding: utf-8
2
+
3
+ module Backup
4
+ module Database
5
+ class OpenLDAP < Base
6
+ class Error < Backup::Error; end
7
+
8
+ ##
9
+ # Name of the ldap backup
10
+ attr_accessor :name
11
+
12
+ ##
13
+ # run slapcat under sudo if needed
14
+ # make sure to set SUID on a file, to let you run the file with permissions of file owner
15
+ # eg. sudo chmod u+s /usr/sbin/slapcat
16
+ attr_accessor :use_sudo
17
+
18
+ ##
19
+ # Stores the location of the slapd.conf or slapcat confdir
20
+ attr_accessor :slapcat_conf
21
+
22
+ ##
23
+ # Additional slapcat options
24
+ attr_accessor :slapcat_args
25
+
26
+ ##
27
+ # Path to slapcat utility (optional)
28
+ attr_accessor :slapcat_utility
29
+
30
+ ##
31
+ # Takes the name of the archive and the configuration block
32
+ def initialize(model, database_id = nil, &block)
33
+ super
34
+ instance_eval(&block) if block_given?
35
+
36
+ @name ||= 'ldap_backup'
37
+ @use_sudo ||= false
38
+ @slapcat_args ||= Array.new
39
+ @slapcat_utility ||= utility(:slapcat)
40
+ @slapcat_conf ||= '/etc/ldap/slapd.d'
41
+ end
42
+
43
+ ##
44
+ # Performs the slapcat command and outputs the
45
+ # data to the specified path based on the 'trigger'
46
+ def perform!
47
+ super
48
+
49
+ pipeline = Pipeline.new
50
+ dump_ext = 'ldif'
51
+
52
+ pipeline << slapcat
53
+ if @model.compressor
54
+ @model.compressor.compress_with do |command, ext|
55
+ pipeline << command
56
+ dump_ext << ext
57
+ end
58
+ end
59
+
60
+ pipeline << "#{ utility(:cat) } > " +
61
+ "'#{ File.join(dump_path, dump_filename) }.#{ dump_ext }'"
62
+
63
+ pipeline.run
64
+ if pipeline.success?
65
+ log!(:finished)
66
+ else
67
+ raise Error, "Dump Failed!\n" + pipeline.error_messages
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ ##
74
+ # Builds the full slapcat string based on all attributes
75
+ def slapcat
76
+ command = "#{ slapcat_utility } #{ slapcat_conf_option } #{ slapcat_conf } #{ user_options }"
77
+ command.prepend("sudo ") if use_sudo
78
+ command
79
+ end
80
+
81
+ ##
82
+ # Uses different slapcat switch depending on confdir or conffile set
83
+ def slapcat_conf_option
84
+ @slapcat_conf.include?(".d") ? "-F" : "-f"
85
+ end
86
+
87
+ ##
88
+ # Builds a compatible string for the additional options
89
+ # specified by the user
90
+ def user_options
91
+ slapcat_args.join(' ')
92
+ end
93
+ end
94
+ end
95
+ end