phusion-backup 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/phusion-backup CHANGED
@@ -1,7 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
  # encoding: binary
3
+ require 'rubygems'
4
+ gem 'activesupport', '~> 2.3.0'
3
5
  require 'etc'
4
6
  require 'optparse'
7
+ require 'active_support'
5
8
 
6
9
  module PhusionBackup
7
10
 
@@ -48,6 +51,9 @@ end
48
51
 
49
52
  class Core
50
53
  ROOT = File.expand_path(File.dirname(__FILE__) + "/..")
54
+ MIN_BACKUPS_TO_KEEP = 5
55
+ MAX_BACKUP_AGE = 3.months
56
+ MAX_BACKUP_AGE_STR = "3 months"
51
57
 
52
58
  include TUI
53
59
 
@@ -126,12 +132,51 @@ class Core
126
132
  end
127
133
  end
128
134
 
129
- if timestamps.size > 5
130
- puts "<b>Deleting all but the most recent 5 backups...</b>"
135
+ puts "<b>Checking whether any old backups need to be deleted...</b>"
136
+ if timestamps.size > MIN_BACKUPS_TO_KEEP
137
+ nbackups = timestamps.size
138
+ ndelete = 0
139
+
131
140
  timestamps.sort! { |a, b| b <=> a }
132
- sh!("rdiff-backup", "--remove-older-than",
133
- timestamps[4].to_s, "--force",
134
- "#{server.dir}/data")
141
+ cleanable_timestamps = timestamps.dup
142
+ MIN_BACKUPS_TO_KEEP.times do
143
+ cleanable_timestamps.pop
144
+ end
145
+ cleanable_timestamps.each do |timestamp|
146
+ if timestamp < MAX_BACKUP_AGE.ago.to_i
147
+ ndelete += 1
148
+ end
149
+ end
150
+
151
+ if ndelete == 0
152
+ puts "Nothing deleted because although there are more than " +
153
+ "#{MIN_BACKUPS_TO_KEEP} backups (namely #{nbackups}), " +
154
+ "none of them are older than #{MAX_BACKUP_AGE_STR}."
155
+ else
156
+ puts "<yellow>Deleting backups older than #{MAX_BACKUP_AGE_STR}, excluding " +
157
+ "the #{MIN_BACKUPS_TO_KEEP} most recent backups.</yellow>"
158
+ timestamps.each do |timestamp|
159
+ if timestamp < MAX_BACKUP_AGE.ago.to_i
160
+ action = "Delete"
161
+ else
162
+ action = "Keep"
163
+ end
164
+ time = Time.at(timestamp)
165
+ relative_time = distance_of_time_in_words(Time.now, time, true)
166
+ printf " * %s %-30s %s\n",
167
+ time,
168
+ "(#{relative_time} ago)",
169
+ action
170
+ end
171
+ puts
172
+ sh!("rdiff-backup", "--remove-older-than",
173
+ MAX_BACKUP_AGE.ago.to_i.to_s, "--force",
174
+ "#{server.dir}/data")
175
+ end
176
+ else
177
+ puts "No old backups need to be deleted. Only #{timestamps.size} backups " +
178
+ "have been made so far. For phusion-backup to consider deleting " +
179
+ "old backups, more than #{MIN_BACKUPS_TO_KEEP} backups must exist."
135
180
  end
136
181
  end
137
182
 
@@ -188,6 +233,36 @@ private
188
233
  end
189
234
  return filename
190
235
  end
236
+
237
+ def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
238
+ from_time = from_time.to_time if from_time.respond_to?(:to_time)
239
+ to_time = to_time.to_time if to_time.respond_to?(:to_time)
240
+ distance_in_minutes = (((to_time - from_time).abs)/60).round
241
+ distance_in_seconds = ((to_time - from_time).abs).round
242
+
243
+ case distance_in_minutes
244
+ when 0..1
245
+ return (distance_in_minutes == 0) ? 'less than a minute' : '1 minute' unless include_seconds
246
+ case distance_in_seconds
247
+ when 0..4 then 'less than 5 seconds'
248
+ when 5..9 then 'less than 10 seconds'
249
+ when 10..19 then 'less than 20 seconds'
250
+ when 20..39 then 'half a minute'
251
+ when 40..59 then 'less than a minute'
252
+ else '1 minute'
253
+ end
254
+
255
+ when 2..44 then "#{distance_in_minutes} minutes"
256
+ when 45..89 then 'about 1 hour'
257
+ when 90..1439 then "about #{(distance_in_minutes.to_f / 60.0).round} hours"
258
+ when 1440..2879 then '1 day'
259
+ when 2880..43199 then "#{(distance_in_minutes / 1440).round} days"
260
+ when 43200..86399 then 'about 1 month'
261
+ when 86400..525599 then "#{(distance_in_minutes / 43200).round} months"
262
+ when 525600..1051199 then 'about 1 year'
263
+ else "over #{(distance_in_minutes / 525600).round} years"
264
+ end
265
+ end
191
266
  end
192
267
 
193
268
  class App
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "phusion-backup"
3
- s.version = "1.0.1"
3
+ s.version = "1.0.2"
4
4
  s.authors = ["Hongli Lai"]
5
5
  s.date = "2011-03-30"
6
6
  s.description = "Simple backup tool utilizing rdiff-backup."
@@ -16,5 +16,6 @@ Gem::Specification.new do |s|
16
16
  s.rdoc_options = ["--charset=UTF-8"]
17
17
  s.executables = ["phusion-backup"]
18
18
  s.require_paths = ["lib"]
19
+ s.add_dependency 'activesupport', '~> 2.3.0'
19
20
  end
20
21
 
@@ -14,17 +14,26 @@
14
14
  /etc/service
15
15
 
16
16
  # Backup all home directories, excluding non-critical things like caches.
17
+ - /root/.gem
18
+ - /root/.bundle
19
+ - /root/.*/bundle
17
20
  /root
18
21
  - /home/*/.gem
19
22
  - /home/*/.bundle
20
23
  - /home/*/.*/bundle
24
+ - /home/*/.passenger/native_support
25
+ + /home/*/.passenger/standalone/config
26
+ - /home/*/.passenger/standalone
21
27
  /home
22
28
 
23
29
  # Backup all Capistrano-deployed web applications, excluding non-critical things like caches.
24
30
  - /u/*/*/shared/cached-copy
25
31
  - /u/*/*/shared/bundle
32
+ - /u/*/*/shared/log
33
+ - /u/*/*/shared/pids
26
34
  - /u/*/*/releases/*/.git
27
35
  - /u/*/*/releases/*/vendor/bundle
36
+ - /u/*/*/releases/*/tmp
28
37
  - /u/*/**/*.log
29
38
  /u
30
39
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phusion-backup
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 1
10
- version: 1.0.1
9
+ - 2
10
+ version: 1.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Hongli Lai
@@ -17,8 +17,23 @@ cert_chain: []
17
17
 
18
18
  date: 2011-03-30 00:00:00 +02:00
19
19
  default_executable:
20
- dependencies: []
21
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activesupport
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 2
32
+ - 3
33
+ - 0
34
+ version: 2.3.0
35
+ type: :runtime
36
+ version_requirements: *id001
22
37
  description: Simple backup tool utilizing rdiff-backup.
23
38
  email: hongli@phusion.nl
24
39
  executables: