-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.py
76 lines (61 loc) · 2.19 KB
/
backup.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
import subprocess
import shlex
import sys
"""
Author - pyCity
Date - 1/25/2019
Version - 1.0
Usage: - python backup.py [home/full]
Description: - Script to backup home directory or full system of all servers.
- SSH keys are necessary to avoid passwords.
- Run with cron -
- 0 0 * * * /home/dylan/Backups/backup.py home
"""
servers = {
"AdminConsole" : "10.0.0.1",
"DNSServer" : "10.0.0.2",
"Firewall" : "10.0.0.3",
"HiddenServ" : "10.0.0.4",
"node1" : "10.0.0.5",
"node2" : "10.0.0.6",
"node3" : "10.0.0.7"
}
def backup():
"""Backup servers, return output"""
output = ""
if option == "home":
for hostname, address in servers.items():
try:
cmd = "rsync -n -az --delete --info=progress2 root@" + address + ":/home/ /home/dylan/Backups/" + hostname
print(shlex.split(cmd))
return_code = subprocess.run(shlex.split(cmd), check=True)
output += str(return_code) + "\n"
except Exception:
print(Exception)
continue
elif option == "full":
for hostname, address in servers.items():
try:
cmd = 'rsync -aAX --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} root@' + address + ':/ /home/dylan/Backups/FullSysBackup/' + hostname
print(shlex.split(cmd))
return_code = subprocess.run(shlex.split(cmd), check=True)
output += str(return_code) + "\n"
except Exception:
print(Exception)
continue
else:
print("Invalid input. Options are: [home] or [full]")
sys.exit()
return output
def save_output(filename, output):
"""Log results stored in output variable to text file"""
with open(filename, "a") as f:
f.write(str(output))
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python old_backup.py home [home / full]")
sys.exit()
option = sys.argv[1]
out = backup()
save_output("results.txt", out)