]>
git.frykholm.com Git - butterbackup.git/blob - butterbackup.py
ccf6229473a99c46fda0fd470a583a3034ae3482
4 from subprocess
import check_call
, CalledProcessError
8 def __init__(self
, config_dir
, dest_dir
):
9 self
.config_dir
= config_dir
10 self
.dest_dir
= dest_dir
11 if not os
.path
.exists(self
.config_dir
):
12 print("No config found", self
.config_dir
)
16 self
.hosts
= os
.listdir(self
.config_dir
)
17 for host
in self
.hosts
:
18 fp
= open(os
.path
.join(self
.config_dir
, host
),"r")
19 self
.backup_host(host
, fp
)
22 def backup_host(self
, host
, host_config
):
23 host_dir
= os
.path
.join(self
.dest_dir
, host
)
24 subvol_dir
= os
.path
.join(host_dir
, "latest")
25 if not os
.path
.exists(host_dir
):
26 print("New host",host
,".")
28 if not os
.path
.exists(subvol_dir
):
30 check_call(shlex
.split("btrfs subvol create %s"% subvol_dir
))
31 except CalledProcessError
as ex
:
32 print("Failed to create subvol! Aborting backup.")
35 command
= ("rsync -a --acls --xattrs --whole-file --numeric-ids --delete --delete-excluded --human-readable --inplace ")
36 excludes
= host_config
.readline()[:-1]
38 check_call(shlex
.split(command
+ excludes
+ " root@%s:/ "%(host) + subvol_dir
))
39 except CalledProcessError
as ex
:
40 if ex
.returncode
not in (12, 30, 255):
41 print("Rsync did not transfer anything from %s, skipping snapshot."%host
)
43 todays_date
= datetime
.datetime
.now().date().strftime("%F")
44 if os
.path
.exists(os
.path
.join(host_dir
, todays_date
)):
45 #There is a snapshot for today, removing it and creating a new one
47 check_call(shlex
.split("btrfs subvol delete %s"%(os
.path
.join(host_dir
, todays_date
))))
48 except CalledProcessError
as ex
:
51 check_call(shlex
.split("btrfs subvol snapshot -r %s %s"%(subvol_dir
,os
.path
.join(host_dir
, todays_date
))))
52 except CalledProcessError
as ex
:
55 if __name__
== "__main__":
57 print("You need to be root. Otherwise all permissions will be lost.")
59 br
= BackupRunner("/etc/butterbackup", "/mnt/data2")