This repository has been archived by the owner on Aug 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.rb
101 lines (90 loc) · 3.32 KB
/
backup.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/ruby
require 'logger'
require 'benchmark'
#============================= OPTIONS ==============================#
# == List of VMs.
DOE_POOL = []
DOMAIN_POOL = []
SAMBP_XEN = []
SUNFIRE_XEN = []
MONTH = []
WEEK = []
DAY = []
# == Log.
LOG_AGE = 'monthly'
# == Email.
FROM = ''
TO = ''
BACKUP_DIR = '/media/test/'
DIR_MONTH = 'month/'
DIR_WEEK = 'week/'
DIR_DAY = 'day/'
MONTH_DAY = [12,22]
CONDITION_MONTH = '&&(not MONTH_DAY.include?(t.day))'
CONDITION_WEEK = '&&(not t.monday?)'
CONDITION_DAY = '&&(not t.sunday?)&&(not t.saturday?)'
#========================== END OF OPTIONS ==========================#
def backup(frequency,dir,t,condition)
frequency.each do |vm|
break if (Time.now.hour >= 5) && (Time.now.hour <= 20)
mkdir = system("mkdir #{BACKUP_DIR}#{dir}#{vm} > /dev/null 2>&1")
%x[/bin/rm #{BACKUP_DIR}#{dir}#{vm}/*.log] if (File.exist?("#{BACKUP_DIR}#{dir}#{vm}/#{frequency.last}-#{t.month}-#{t.year}.log"))#{condition}
next if File.exist?("#{BACKUP_DIR}#{dir}#{vm}/#{vm}-#{t.month}-#{t.year}.log")
@logger = Logger.new("#{BACKUP_DIR}#{dir}#{vm}/#{vm}-#{t.month}-#{t.year}.log", LOG_AGE)
@logger.info("Started running")
case true
when DOE_POOL.include?(vm)
server = ''
pass = ""
when DOMAIN_POOL.include?(vm)
server = ''
pass = ""
when SAMBP_XEN.include?(vm)
server = ''
pass = ""
when SUNFIRE_XEN.include?(vm)
server = ''
pass = ""
end
result = []
run_time = Benchmark.realtime do
begin
@logger.info("Suspend start")
result << system("xe vm-suspend vm=#{vm} -s #{server} -u root -pw #{pass} >> #{BACKUP_DIR}#{dir}#{vm}/#{vm}-#{t.month}-#{t.year}.log 2>&1")
@logger.info("Backup start")
result << system("xe vm-export vm=#{vm} filename=#{BACKUP_DIR}#{dir}#{vm}/#{vm}-$(date +%d-%m-%Y).backup -s #{server} -u root -pw #{pass} >> #{BACKUP_DIR}#{dir}#{vm}/#{vm}-#{t.month}-#{t.year}.log 2>&1")
@logger.info("Resume start")
result << system("xe vm-resume vm=#{vm} -s #{server} -u root -pw #{pass} >> #{BACKUP_DIR}#{dir}#{vm}/#{vm}-#{t.month}-#{t.year}.log 2>&1")
end
end
@logger.info("Removing old backup")
%x[rm #{BACKUP_DIR}#{dir}#{vm}/`ls -t1r #{BACKUP_DIR}#{dir}#{vm} | head -n 1`] if (result[1] == true) && (mkdir != true)
@logger.info("Finished running - Backup run time: #{run_time.to_s[0, 5]}")
send_email_with_log(vm,t,dir) if result.include?(false)
end
end
def send_email_with_log(vm,t,dir)
csvnamefile = "#{BACKUP_DIR}#{DIR_DAY}#{vm}/#{vm}-#{t.month}-#{t.year}.log"
binary = File.read(csvnamefile)
encoded = [binary].pack("m") # base64 econding
puts value = %x[/usr/sbin/sendmail << EOF
subject: Backup VM #{vm}
from: backup-vm
Content-Description: "#{csvnamefile}"
Content-Type: text/csv; name="#{csvnamefile}"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="#{csvnamefile}"
#{encoded}
EOF]
end
#========================== Main program ==========================#
t=Time.now
if (not t.sunday?)&&(not t.saturday?) then
backup(DAY,DIR_DAY,t,CONDITION_DAY)
end
if (t.sunday?) || (t.saturday?) then
backup(WEEK,DIR_WEEK,t,CONDITION_WEEK)
end
if MONTH_DAY.include?(t.day) then
backup(MONTH,DIR_MONTH,t,CONDITION_MONTH)
end