]>
Commit | Line | Data |
---|---|---|
25a66962 MF |
1 | #!/usr/bin/env python3 |
2 | import os | |
3 | import sys | |
4 | from subprocess import Popen, PIPE | |
5 | import shlex | |
6 | import datetime | |
7 | ||
8 | def backup_host(host, base_dir, fp): | |
9 | dest_dir = os.path.join(base_dir, host, "latest") | |
10 | if not os.path.exists(base_dir): | |
11 | print("New host",host,".") | |
12 | run("btrfs subvol create %s"% os.path.join(base_dir, host)) | |
13 | os.makedirs(dest_dir) | |
14 | command = ("rsync -a --numeric-ids --delete --delete-excluded --human-readable --inplace ") | |
15 | excludes = fp.readline()[:-1] | |
16 | (stdout,stderr) = run(command + excludes + " root@%s:/ "%(host) + dest_dir) | |
17 | if stdout: | |
18 | print(stdout) | |
19 | if stderr: | |
20 | print(stdout) | |
21 | run("btrfs subvol snapshot %s %s"%(os.path.join(base_dir, host),os.path.join(base_dir, host, datetime.datetime.now().date().strftime("%F")))) | |
22 | ||
23 | def run(cmd): | |
24 | (stdout, stderr) = Popen(shlex.split(cmd), stdout=PIPE).communicate() | |
25 | if stdout: | |
26 | stdout = stdout.decode('utf-8') | |
27 | if stderr: | |
28 | stderr = stderr.decode('utf-8') | |
29 | return(stdout, stderr) | |
30 | ||
31 | if __name__ == "__main__": | |
32 | if os.geteuid() != 0: | |
33 | print("You need to be root. Otherwise all permissions will be lost.") | |
34 | sys.exit(-1) | |
35 | base_path="/etc/butterbackup" | |
36 | dest_dir="/mnt/data2" | |
37 | if not os.path.exists(base_path): | |
38 | print("No hosts to backup, please place them in",base_path) | |
39 | sys-exit(-1) | |
40 | hosts = os.listdir(base_path) | |
41 | for host in hosts: | |
42 | fp = open(os.path.join(base_path, host),"r") | |
43 | backup_host(host, dest_dir, fp) | |
44 | sys.exit(0) |