3 # Copyright (C) 2013 Intra2net AG
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published
7 # by the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see
17 # <http://www.gnu.org/licenses/lgpl-3.0.html>
27 from datetime import datetime
28 from functools import partial
30 from deltatar.deltatar import DeltaTar, NO_MATCH, MATCH, PARENT_MATCH
31 from deltatar.tarfile import TarFile, GNU_FORMAT
36 consoleLogger = logging.StreamHandler()
37 consoleLogger.setLevel(logging.DEBUG)
39 def check_equal_dirs(path1, path2, deltatar):
41 compare the two directories source_dir and target_dir and check
44 nbars1 = len(path1.split('/'))
45 nbars2 = len(path2.split('/'))
47 source_it = deltatar._recursive_walk_dir(path1)
48 source_it = deltatar.jsonize_path_iterator(source_it, strip=nbars1)
49 target_it = deltatar._recursive_walk_dir(path2)
50 target_it = deltatar.jsonize_path_iterator(target_it, strip=nbars2)
53 sitem = next(source_it)
54 titem = next(target_it)
57 titem = next(target_it)
58 raise Exception("iterators do not stop at the same time")
62 assert deltatar._equal_stat_dicts(sitem, titem)
63 except Exception as e:
69 if __name__ == "__main__":
72 #top = tracemalloc.DisplayTop(25)
73 #top.show_lineno = True
75 parser = argparse.ArgumentParser()
77 parser.add_argument("-m", "--mode", default='',
78 help="Mode in which to read/write the backup")
79 parser.add_argument("-t", "--targetpath", help="target path directory")
80 parser.add_argument("-s", "--sourcepath", help="source path directory")
81 parser.add_argument("-p", "--password", default='', help="password")
82 parser.add_argument("-v", "--volsize", default=None, help="maximum volume size, in Megabytes")
83 parser.add_argument("-r", "--restore", action='store_true', help="Restore a backup")
84 parser.add_argument("-f", "--full", action='store_true', help="Create a full backup")
85 parser.add_argument("-d", "--diff", action='store_true', help="Create a diff backup")
86 parser.add_argument("-i", "--indexes", nargs='+', help="indexes paths")
87 parser.add_argument("-l", "--list-files", action='store_true', help="List files in a tarball")
88 parser.add_argument("-x", "--excluded", nargs='+', default=[],
89 help="excluded files")
90 parser.add_argument("-inc", "--included", nargs='+', default=[],
91 help="included files")
92 parser.add_argument("-ip", "--included-path", default=None,
93 help="path to the file containing included paths")
94 parser.add_argument("-xp", "--excluded-path", default=None,
95 help="path to the file containing excluded paths")
96 parser.add_argument("-e", "--equals", action='store_true', help="Checks two dirs are equal")
98 args = parser.parse_args()
100 if args.excluded_path:
101 f = open(args.excluded_path, 'r')
102 excluded_files = f.readlines()
105 excluded_files = args.excluded
107 if args.included_path:
108 f = open(args.included_path, 'r')
109 included_files = f.readlines()
112 included_files = args.included
114 deltatar = DeltaTar(mode=args.mode, password=args.password,
115 logger=consoleLogger, excluded_files=excluded_files,
116 included_files=included_files)
119 deltatar.create_full_backup(args.sourcepath, args.targetpath, args.volsize)
121 deltatar.create_diff_backup(args.sourcepath, args.targetpath, args.indexes[0], args.volsize)
122 elif args.list_files:
123 deltatar.list_backup(args.sourcepath)
125 if args.indexes is not None:
126 deltatar.restore_backup(args.targetpath, backup_indexes_paths=args.indexes)
128 deltatar.restore_backup(args.targetpath, backup_tar_path=args.sourcepath)
130 check_equal_dirs(os.path.abspath(args.sourcepath), os.path.abspath(args.targetpath), deltatar)