backup_management 0.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/lib/backup_management.rb +103 -0
- metadata +46 -0
@@ -0,0 +1,103 @@
|
|
1
|
+
# This set of classes require date to manipulate backup date and compare age between backups
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
# This class represent one backup of "something"
|
5
|
+
class Backup
|
6
|
+
|
7
|
+
# One backup refers to the #filepath that contains that backup.
|
8
|
+
# That file has to be represented by #{name_of_something}_#{date}
|
9
|
+
# The #date has to be represented this way : %Y%m%dT%H%M
|
10
|
+
attr_reader :filepath, :date
|
11
|
+
|
12
|
+
# create one instance of Backup with the #filepath
|
13
|
+
def initialize(filepath)
|
14
|
+
@filepath = filepath
|
15
|
+
@date = Date.strptime(filepath[/\d{8}T\d{4}/],'%Y%m%dT%H%M')
|
16
|
+
end
|
17
|
+
|
18
|
+
# delete that backup (you could need that if the backup is too old)
|
19
|
+
def delete!
|
20
|
+
puts "#{filepath} deleted."
|
21
|
+
File.delete(filepath)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
# This class handles a collection of #Backup for the same object "something".
|
27
|
+
# This is useful to manipulate backups as a whole
|
28
|
+
class BackupList < Array
|
29
|
+
|
30
|
+
# The object you want to manage the backups has
|
31
|
+
# * a #name
|
32
|
+
# * the backups are saved in a #folder
|
33
|
+
attr_reader :name, :folder
|
34
|
+
|
35
|
+
# This is populating the array with #Backup objects
|
36
|
+
# It is sorted at the end for later manipulation on a date sorted array.
|
37
|
+
def initialize(folder,name)
|
38
|
+
@name = name
|
39
|
+
@folder = folder
|
40
|
+
list = Dir.glob("#{folder}/#{name}*")
|
41
|
+
list.each do |path|
|
42
|
+
self << Backup.new(path)
|
43
|
+
end
|
44
|
+
self.sort_by! {|bck| bck.date}
|
45
|
+
end
|
46
|
+
|
47
|
+
# A short method to print the list of backups
|
48
|
+
def list
|
49
|
+
puts "The list of backups for #{self.name}"
|
50
|
+
self.each do |bck|
|
51
|
+
puts bck.filepath
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# A useful method to rotate the files
|
56
|
+
# * #nb_to_keep is the number of backups to keep betwwen the two #down_date and #up_date
|
57
|
+
# (number above #number_to_keep will be deleted)
|
58
|
+
def rotate!(nb_to_keep, down_date, up_date)
|
59
|
+
range = down_date..up_date
|
60
|
+
list = []
|
61
|
+
self.each do |bck|
|
62
|
+
list << bck if range === bck.date
|
63
|
+
end
|
64
|
+
|
65
|
+
if list.length == 0
|
66
|
+
puts "No backup between #{down_date} and #{up_date}."
|
67
|
+
elsif list.length <= nb_to_keep
|
68
|
+
puts "There is no need to delete backups."
|
69
|
+
else
|
70
|
+
nb_to_delete = list.length - nb_to_keep
|
71
|
+
puts "#{nb_to_keep} backup(s) will be kept and #{nb_to_delete} backup(s) will be deleted."
|
72
|
+
list.pop(nb_to_keep)
|
73
|
+
list.each {|bck| bck.delete!}
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# A method to do an automatic GrandFather Father Son rotation of the backups
|
78
|
+
# Here a month is 30 days
|
79
|
+
# * #nb_g is the number of GrandFather backups to keep (Monthly)
|
80
|
+
# * #nb_f is the number of Father backups to keep (weekly)
|
81
|
+
# * #nb_s is the number of son backups to keep (daily)
|
82
|
+
#/!\ It checks until the prev year but not before.
|
83
|
+
def rotate_gfs!(nb_g, nb_f, nb_s)
|
84
|
+
|
85
|
+
puts "Rotate GrandFather"
|
86
|
+
self.rotate!(nb_g,Date.today.prev_year, Date.today.prev_day(30))
|
87
|
+
|
88
|
+
puts "Rotate Father"
|
89
|
+
self.rotate!(nb_f,Date.today.prev_day(29), Date.today.prev_day(7))
|
90
|
+
|
91
|
+
puts "Rotate Son"
|
92
|
+
self.rotate!(nb_s,Date.today.prev_day(6),Date.today)
|
93
|
+
|
94
|
+
end
|
95
|
+
# A method to rsync the backup folder with a remote host describe by #host
|
96
|
+
# #host should be username@machine
|
97
|
+
# the ssh_keys have to be exchanged before using this (if not, it will failed)
|
98
|
+
# /!\ the folder has to exist on the remote host.
|
99
|
+
def rsync!(host, remote_folder)
|
100
|
+
puts "rsync #{folder}/#{name}* #{host}:#{remote_folder}"
|
101
|
+
`rsync #{folder}/#{name}* #{host}:#{remote_folder}`
|
102
|
+
end
|
103
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: backup_management
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Pierre Ozoux
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-10 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: It is for writing scripts for UNIX-like systems to handle your backups
|
15
|
+
in a quick way.
|
16
|
+
email: pierre.ozoux@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/backup_management.rb
|
22
|
+
homepage: http://rubygems.org/gems/backup_management
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.23
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: A sinmple way to manage your backup files.
|
46
|
+
test_files: []
|