-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup-vms
More file actions
61 lines (55 loc) · 2.26 KB
/
backup-vms
File metadata and controls
61 lines (55 loc) · 2.26 KB
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
#!/usr/bin/python
import subprocess
import sys
import time
import smtplib
#we are basing the week number off the epoch to avoid overlapping weeks on the new year
week = str(int(time.time() / (3600*24*7)) % 4)
uuids = []
labels = []
errors = []
successes = []
#first get all the uuids
procuuid = subprocess.Popen(['xe', 'vm-list', 'is-control-domain=false'],stdout=subprocess.PIPE)
for line in procuuid.stdout:
if 'uuid' in line:
uuids.append(line.split(':').pop().strip())
if 'name-label' in line:
labels.append(line.split(':').pop().strip())
#now backup each uuid
for i, uuid in enumerate(uuids):
#uncomment and modify to backup a single vm
#if not 'u1sb' in labels[i]:
# break
#else:
# print labels[i]
#create snapshot
procsnapshot = subprocess.Popen(['xe', 'vm-snapshot', 'uuid=' + uuids[i], 'new-name-label=backup-' + labels[i]],stdout=subprocess.PIPE)
suuid = procsnapshot.stdout.readline().strip()
#create template
proctemplate = subprocess.Popen(['xe', 'template-param-set', 'is-a-template=false', 'ha-always-run=false', 'uuid='+suuid])
#remove previous xva file
removexva = subprocess.Popen(['rm','-rf','/media/backupdrive/'+week+'/'+labels[i]+'.xva'])
#export template
procexporttemplate = subprocess.Popen(['xe', 'vm-export', 'vm='+suuid, 'filename=/media/backupdrive/'+week+'/'+labels[i]+'.xva'],stdout=subprocess.PIPE)
result = procexporttemplate.stdout.readline().strip()
#make sure it says 'Export succeeded'
if not 'Export succeeded' in result:
errors.append(uuids[i] + ': ' + labels[i])
else:
successes.append(uuids[i] + ': ' + labels[i])
#delete the snapshot
procdelsnapshot = subprocess.Popen(['xe', 'vm-uninstall', 'uuid='+suuid, 'force=true'])
#send an email with details of backup
msg = "From: backup@pelagodesign.com\r\n"
msg = msg + "To: backup@pelagodesign.com\r\n"
msg = msg + "Subject: VM Backup Results\r\n"
msg = msg + "\r\n"
msg = msg + "Backed up the following VMs:\n"
msg = msg + "\n".join(successes)+"\n"
if len(errors):
msg = msg + "ERROR: The following VMs did not get backed up:\n"
msg = msg + "\n".join(errors)+"\n"
server = smtplib.SMTP('192.168.1.42')
server.sendmail("backup@pelagodesign.com", "backup@pelagodesign.com", msg)
server.quit()