]> git.frykholm.com Git - butterbackup.git/blobdiff - butterbackup.py
Applied patch from tuttle@somsants.net.
[butterbackup.git] / butterbackup.py
index 7c80e8b344467f61e1a9738508ec09f020a311c0..110077c1065fe23c9726306a3fd84f5e74107425 100755 (executable)
@@ -19,8 +19,8 @@ class Host():
 
     def backup(self):
         if not os.path.exists(self.host_dir):
-            print("New host",host,".")
-            os.makedir(self.host_dir)
+            print("New host",self.name,".")
+            os.makedirs(self.host_dir)
         if not os.path.exists(self.subvol_dir):
             try:
                 check_call(shlex.split("btrfs subvol create %s"% self.subvol_dir))
@@ -29,13 +29,20 @@ class Host():
                 return() 
             
         command = ("rsync -a --acls --xattrs --whole-file --numeric-ids --delete --delete-excluded --human-readable --inplace ")
-        excludes = " --exclude " + " --exclude ".join(self.config.get("host", "exclude").split(',')) #FIXME
+        if self.config.has_option("host", "include"):
+            includes = " --include " + " --include ".join(self.config.get("host", "include").split(',')) #FIXME
+            command = command + includes
+        if self.config.has_option("host", "exclude"):
+            excludes = " --exclude " + " --exclude ".join(self.config.get("host", "exclude").split(',')) #FIXME
+            command = command + excludes
         try:
-            print(command + excludes + " root@%s:/ "%(self.name) + self.subvol_dir)
-            check_call(shlex.split(command + excludes + " root@%s:/ "%(self.name) + self.subvol_dir))
+            print(command + " root@%s:/ "%(self.name) + self.subvol_dir)
+            check_call(shlex.split(command + " root@%s:/ "%(self.name) + self.subvol_dir))
         except CalledProcessError as ex:
-            if ex.returncode not in (12, 30):
-                print("Rsync did not transfer anything from %s, skipping snapshot."%self.name)
+            if ex.returncode in (24,):
+                pass
+            else:
+                print("Rsync error from %s, skipping snapshot. Rsync exit value=%s"%(self.name, ex.returncode))
                 return()
         todays_date = datetime.datetime.now().date().strftime("%F")
         if os.path.exists(os.path.join(self.host_dir, todays_date)):
@@ -53,7 +60,9 @@ class Host():
         if self.keep == -1:
             print("No keep specified for %s, keeping all"%self.name)
             return
-
+        if not os.path.exists(self.host_dir):
+            print("New host, no pruning needed")
+            return
         snaps = sorted([snap for snap in os.listdir(self.host_dir) if not snap == "latest" ], reverse=True)
         while len(snaps) > self.keep:
             snap = snaps.pop()
@@ -63,23 +72,28 @@ class Host():
                 pass    
 
 class BackupRunner():
-    def __init__(self, config_dir, dest_dir):
+    def __init__(self, config_dir):
         self.config_dir = config_dir
-        self.dest_dir = dest_dir
         if not os.path.exists(self.config_dir):
             print("No config found", self.config_dir)
             sys-exit(-1)
 
-    def run(self):
-        self.hosts = os.listdir(self.config_dir)
+    def run(self, hostlist=None):
+        self.hosts = hostlist or os.listdir(self.config_dir)
 
         for host in self.hosts:
             if host == 'default.cfg':
                 continue
             try:
+                configfile = os.path.join(self.config_dir, host)
+
+                if not os.path.exists(configfile):
+                    # Trigger logging in the except clause
+                    raise BaseException()
+
                 config = configparser.ConfigParser(strict=False)
                 config.read_file(open(os.path.join(self.config_dir, 'default.cfg'),'r'))
-                config.read(os.path.join(self.config_dir, host))
+                config.read(configfile)
             except BaseException as ex:
                 print("Config error for %s. Skipping host."%host)
                 continue
@@ -91,6 +105,10 @@ if __name__ == "__main__":
     if os.geteuid() != 0:
         print("You need to be root. Otherwise all permissions will be lost.")
         sys.exit(-1)
-    br = BackupRunner("/etc/butterbackup", "/mnt/data2")
-    br.run()
+    br = BackupRunner("/etc/butterbackup")
+
+    hostlist = sys.argv[1:]
+    br.run(hostlist=hostlist)
+
     sys.exit(0)
+