Commit | Line | Data |
---|---|---|
10c70dc6 ERE |
1 | import argparse |
2 | import binascii | |
3 | import json | |
4 | import logging | |
5 | import os | |
6 | import re | |
7 | import random | |
8 | import shutil | |
9 | from datetime import datetime | |
10 | from functools import partial | |
11 | ||
12 | from deltatar.deltatar import DeltaTar, NO_MATCH, MATCH, PARENT_MATCH | |
13 | from deltatar.tarfile import TarFile, GNU_FORMAT | |
14 | ||
15 | MODE = '' | |
16 | PASSWORD = '' | |
17 | ||
18 | consoleLogger = logging.StreamHandler() | |
19 | consoleLogger.setLevel(logging.DEBUG) | |
20 | ||
8c65a2b1 ERE |
21 | def launch_memory_usage_server(port = 8080): |
22 | import cherrypy | |
23 | import dowser | |
24 | ||
25 | cherrypy.tree.mount(dowser.Root()) | |
26 | cherrypy.config.update({ | |
27 | 'environment': 'embedded', | |
28 | 'server.socket_port': port | |
29 | }) | |
30 | ||
31 | cherrypy.engine.start() | |
32 | ||
33 | ||
10c70dc6 ERE |
34 | def check_equal_dirs(path1, path2, deltatar): |
35 | ''' | |
36 | compare the two directories source_dir and target_dir and check | |
37 | # they are the same | |
38 | ''' | |
39 | nbars1 = len(path1.split('/')) | |
40 | nbars2 = len(path2.split('/')) | |
41 | ||
42 | source_it = deltatar._recursive_walk_dir(path1) | |
43 | source_it = deltatar.jsonize_path_iterator(source_it, strip=nbars1) | |
44 | target_it = deltatar._recursive_walk_dir(path2) | |
45 | target_it = deltatar.jsonize_path_iterator(target_it, strip=nbars2) | |
46 | while True: | |
47 | try: | |
48 | sitem = source_it.next() | |
49 | titem = target_it.next() | |
50 | except StopIteration: | |
51 | try: | |
52 | titem = target_it.next() | |
53 | raise Exception("iterators do not stop at the same time") | |
54 | except StopIteration: | |
55 | break | |
56 | try: | |
57 | assert deltatar._equal_stat_dicts(sitem, titem) | |
58 | except Exception, e: | |
59 | print sitem | |
60 | print titem | |
61 | print "FAIL" | |
62 | raise | |
63 | ||
64 | if __name__ == "__main__": | |
8c65a2b1 | 65 | #launch_memory_usage_server() |
10c70dc6 ERE |
66 | parser = argparse.ArgumentParser() |
67 | ||
68 | parser.add_argument("-m", "--mode", default='', | |
69 | help="Mode in which to read/write the backup") | |
70 | parser.add_argument("-t", "--targetpath", help="target path directory") | |
71 | parser.add_argument("-s", "--sourcepath", help="source path directory") | |
72 | parser.add_argument("-p", "--password", default='', help="password") | |
73 | parser.add_argument("-v", "--volsize", default=None, help="maximum volume size, in Megabytes") | |
74 | parser.add_argument("-r", "--restore", action='store_true', help="Restore a backup") | |
75 | parser.add_argument("-f", "--full", action='store_true', help="Create a full backup") | |
76 | parser.add_argument("-d", "--diff", action='store_true', help="Create a diff backup") | |
77 | parser.add_argument("-i", "--indexes", nargs='+', help="indexes paths") | |
8c65a2b1 ERE |
78 | parser.add_argument("-l", "--list-files", action='store_true', help="List files in a tarball") |
79 | parser.add_argument("-x", "--excluded", nargs='+', default=[], | |
80 | help="excluded files") | |
81 | parser.add_argument("-xp", "--excluded-path", default=None, | |
82 | help="path to the file containing excluded paths") | |
10c70dc6 ERE |
83 | parser.add_argument("-e", "--equals", action='store_true', help="Checks two dirs are equal") |
84 | ||
85 | args = parser.parse_args() | |
8c65a2b1 ERE |
86 | |
87 | if args.excluded_path: | |
88 | f = open(args.excluded_path, 'r') | |
89 | excluded_files = f.readlines() | |
90 | f.close() | |
91 | else: | |
92 | excluded_files = args.excluded | |
93 | ||
94 | deltatar = DeltaTar(mode=args.mode, password=args.password, | |
95 | logger=consoleLogger, excluded_files=excluded_files) | |
10c70dc6 ERE |
96 | |
97 | if args.full: | |
98 | deltatar.create_full_backup(args.sourcepath, args.targetpath, args.volsize) | |
99 | elif args.diff: | |
100 | deltatar.create_diff_backup(args.sourcepath, args.targetpath, args.indexes[0], args.volsize) | |
8c65a2b1 ERE |
101 | elif args.list_files: |
102 | deltatar.list_backup(args.sourcepath) | |
10c70dc6 ERE |
103 | elif args.restore: |
104 | if args.indexes is not None: | |
105 | deltatar.restore_backup(args.targetpath, backup_indexes_paths=args.indexes) | |
106 | else: | |
107 | deltatar.restore_backup(args.targetpath, backup_tar_path=args.sourcepath) | |
108 | elif args.equals: | |
109 | check_equal_dirs(os.path.abspath(args.sourcepath), os.path.abspath(args.targetpath), deltatar) |