-
Notifications
You must be signed in to change notification settings - Fork 1
/
getbackups.py
51 lines (43 loc) · 1.48 KB
/
getbackups.py
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
#!/usr/bin/python3
import os
import subprocess
import datetime
import time
#List of Instances ID=====
Command=" nova list --minimal | awk -F'|' '/\|/ && !/ID/{system(\"echo \"$2\"\")}'"
ListOfInstances = subprocess.Popen([Command], stdout=subprocess.PIPE, shell=True)
(output, err) = ListOfInstances.communicate()
output=str(output)
output=output.replace("b'","")
output=output.replace("'"," ")
output=output.replace(chr(92)+"n",' ')
ListOfInstances=output.split()
#Backup=====
#BackupType can be "daily" or "weekly" too
BackupType="manual"
#Int parameter representing how many backups to keep around
Rotation="7"
ListOfOutputs = []
log = ""
for ID in ListOfInstances:
#Name of the backup image
Command = "echo 'Start OpenStack snapshot creation for instance ID'"+ID
os.system(Command)
Now = datetime.datetime.now()
Now=str(Now)
Now = Now.replace(" ","-")
NameOfBackupImage=Now+ID
Command ="nova backup "+ ID +" "+NameOfBackupImage+" "+ BackupType +" "+Rotation
x = subprocess.Popen([Command], stdout=subprocess.PIPE, shell=True)
(output, err) = x.communicate()
output = str (output)
output=output.replace("b'","")
log = output + log + "\n"
#If the number of your instances is high, backups may need to be delayed a bit
time.sleep(3)
#Report=====
EmailFrom = "senderemail"
EmailTo = "youremail"
#Mailx must be pre-installed
Command = " mail -s \"Logs of backups instances\" -r \"" + EmailFrom + "\" \"" + EmailTo + "\""
os.system(Command)