]>
git.frykholm.com Git - butterbackup.git/blob - butterbackup.py
a049efc0d9b66465badadaf134da2375bcd27b9b
4 from subprocess
import check_call
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 subvol_dir
= os
.path
.join(self
.dest_dir
, host
)
24 dest_dir
= os
.path
.join(subvol_dir
, "latest")
25 if not os
.path
.exists(subvol_dir
):
26 print("New host",host
,".")
28 check_call(shlex
.split("btrfs subvol create %s"% subvol_dir
))
29 except subprocess
.CalledProcessError
as ex
:
30 print("Failed to create subvol! Aborting backup.")
33 command
= ("rsync --timeout=10 -a --numeric-ids --delete --delete-excluded --human-readable --inplace ")
34 excludes
= host_config
.readline()[:-1]
36 check_call(shlex
.split(command
+ excludes
+ " root@%s:/ "%(host) + dest_dir
))
37 except subprocess
.CalledProcessError
as ex
:
38 if ex
.returncode
not in (30, 255):
39 print("Rsync did not transfer anything, skipping snapshot.")
41 todays_date
= datetime
.datetime
.now().date().strftime("%F")
43 check_call(shlex
.split("btrfs subvol snapshot %s %s"%(subvol_dir
,os
.path
.join(subvol_dir
, todays_date
))))
44 except subprocess
.CalledProcessError
as ex
:
47 if __name__
== "__main__":
49 print("You need to be root. Otherwise all permissions will be lost.")
51 br
= BackupRunner("/etc/butterbackup", "/mnt/data2")