adding more options to backup script, like listing backup files
authorEduardo Robles Elvira <edulix@wadobo.com>
Fri, 27 Sep 2013 14:02:51 +0000 (16:02 +0200)
committerEduardo Robles Elvira <edulix@wadobo.com>
Fri, 27 Sep 2013 14:03:15 +0000 (16:03 +0200)
backup.py
deltatar/deltatar.py

index 2d19de9..adbae5f 100644 (file)
--- a/backup.py
+++ b/backup.py
@@ -18,6 +18,19 @@ PASSWORD = ''
 consoleLogger = logging.StreamHandler()
 consoleLogger.setLevel(logging.DEBUG)
 
+def launch_memory_usage_server(port = 8080):
+    import cherrypy
+    import dowser
+
+    cherrypy.tree.mount(dowser.Root())
+    cherrypy.config.update({
+        'environment': 'embedded',
+        'server.socket_port': port
+    })
+
+    cherrypy.engine.start()
+
+
 def check_equal_dirs(path1, path2, deltatar):
     '''
     compare the two directories source_dir and target_dir and check
@@ -49,6 +62,7 @@ def check_equal_dirs(path1, path2, deltatar):
             raise
 
 if __name__ == "__main__":
+    #launch_memory_usage_server()
     parser = argparse.ArgumentParser()
 
     parser.add_argument("-m", "--mode", default='',
@@ -61,15 +75,31 @@ if __name__ == "__main__":
     parser.add_argument("-f", "--full", action='store_true', help="Create a full backup")
     parser.add_argument("-d", "--diff", action='store_true', help="Create a diff backup")
     parser.add_argument("-i", "--indexes", nargs='+', help="indexes paths")
+    parser.add_argument("-l", "--list-files", action='store_true', help="List files in a tarball")
+    parser.add_argument("-x", "--excluded", nargs='+', default=[],
+                        help="excluded files")
+    parser.add_argument("-xp", "--excluded-path", default=None,
+                        help="path to the file containing excluded paths")
     parser.add_argument("-e", "--equals", action='store_true', help="Checks two dirs are equal")
 
     args = parser.parse_args()
-    deltatar = DeltaTar(mode=args.mode, password=args.password, logger=consoleLogger)
+
+    if args.excluded_path:
+        f = open(args.excluded_path, 'r')
+        excluded_files = f.readlines()
+        f.close()
+    else:
+        excluded_files = args.excluded
+
+    deltatar = DeltaTar(mode=args.mode, password=args.password,
+        logger=consoleLogger, excluded_files=excluded_files)
 
     if args.full:
         deltatar.create_full_backup(args.sourcepath, args.targetpath, args.volsize)
     elif args.diff:
         deltatar.create_diff_backup(args.sourcepath, args.targetpath, args.indexes[0], args.volsize)
+    elif args.list_files:
+        deltatar.list_backup(args.sourcepath)
     elif args.restore:
         if args.indexes is not None:
             deltatar.restore_backup(args.targetpath, backup_indexes_paths=args.indexes)
index e050453..3d624a2 100644 (file)
@@ -986,6 +986,54 @@ class DeltaTar(object):
 
         return (index1, index2)
 
+    def list_backup(self, backup_tar_path, list_func=None):
+        if not isinstance(backup_tar_path, basestring):
+            raise Exception('Backup tar path must be a string')
+
+        if not os.path.exists(backup_tar_path) or not os.path.isfile(backup_tar_path):
+            raise Exception('Source path "%s" does not exist or is not a '\
+                            'file' % backup_tar_path)
+
+        if not os.access(backup_tar_path, os.R_OK):
+            raise Exception('Source path "%s" is not readable' % backup_tar_path)
+
+        cwd = os.getcwd()
+
+        def new_volume_handler(deltarobj, cwd, backup_path, tarobj, base_name, volume_number):
+            '''
+            Handles the new volumes
+            '''
+            volume_name = deltarobj.volume_name_func(backup_path, True,
+                volume_number, guess_name=True)
+            volume_path = os.path.join(backup_path, volume_name)
+
+            # we convert relative paths into absolute because CWD is changed
+            if not os.path.isabs(volume_path):
+                volume_path = os.path.join(cwd, volume_path)
+            tarobj.open_volume(volume_path)
+
+        backup_path = os.path.dirname(backup_tar_path)
+        if not os.path.isabs(backup_path):
+            backup_path = os.path.join(cwd, backup_path)
+        new_volume_handler = partial(new_volume_handler, self, cwd, backup_path)
+        tarobj = tarfile.TarFile.open(backup_tar_path,
+                            mode='r' + self.mode,
+                            format=tarfile.GNU_FORMAT,
+                            concat_compression='#gz' in self.mode,
+                            password=self.password,
+                            new_volume_handler=new_volume_handler)
+
+        def filter(cls, list_func, tarinfo):
+            if list_func is None:
+                print tarinfo.path
+            else:
+                list_func(tarinfo)
+            return False
+        filter = partial(filter, self, list_func)
+
+        tarobj.extractall(filter=filter)
+        tarobj.close()
+
     def restore_backup(self, target_path, backup_indexes_paths=[],
                        backup_tar_path=None):
         '''
@@ -1139,7 +1187,10 @@ class DeltaTar(object):
                     # means that work is already done
                     if ipath['path'].startswith('delete://'):
                         continue
-                    helper.restore(ipath, l_no)
+                    try:
+                        helper.restore(ipath, l_no)
+                    except Exception, e:
+                        print "FAILED to restore: ", ipath.get('path', '')
                     continue
 
                 # if both files are equal, we have nothing to restore