Commit | Line | Data |
---|---|---|
e4a93a2f ERE |
1 | #!/usr/bin/env python3 |
2 | ||
3 | # Copyright (C) 2013 Intra2net AG | |
4 | # | |
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. | |
9 | # | |
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. | |
14 | # | |
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> | |
18 | ||
10c70dc6 ERE |
19 | import argparse |
20 | import binascii | |
21 | import json | |
22 | import logging | |
23 | import os | |
24 | import re | |
25 | import random | |
26 | import shutil | |
27 | from datetime import datetime | |
28 | from functools import partial | |
29 | ||
30 | from deltatar.deltatar import DeltaTar, NO_MATCH, MATCH, PARENT_MATCH | |
31 | from deltatar.tarfile import TarFile, GNU_FORMAT | |
32 | ||
10c70dc6 ERE |
33 | consoleLogger = logging.StreamHandler() |
34 | consoleLogger.setLevel(logging.DEBUG) | |
35 | ||
36 | def check_equal_dirs(path1, path2, deltatar): | |
37 | ''' | |
38 | compare the two directories source_dir and target_dir and check | |
39 | # they are the same | |
40 | ''' | |
41 | nbars1 = len(path1.split('/')) | |
42 | nbars2 = len(path2.split('/')) | |
43 | ||
44 | source_it = deltatar._recursive_walk_dir(path1) | |
45 | source_it = deltatar.jsonize_path_iterator(source_it, strip=nbars1) | |
46 | target_it = deltatar._recursive_walk_dir(path2) | |
47 | target_it = deltatar.jsonize_path_iterator(target_it, strip=nbars2) | |
48 | while True: | |
49 | try: | |
e4a93a2f ERE |
50 | sitem = next(source_it) |
51 | titem = next(target_it) | |
10c70dc6 ERE |
52 | except StopIteration: |
53 | try: | |
e4a93a2f | 54 | titem = next(target_it) |
10c70dc6 ERE |
55 | raise Exception("iterators do not stop at the same time") |
56 | except StopIteration: | |
57 | break | |
58 | try: | |
59 | assert deltatar._equal_stat_dicts(sitem, titem) | |
e4a93a2f ERE |
60 | except Exception as e: |
61 | print(sitem) | |
62 | print(titem) | |
63 | print("FAIL") | |
10c70dc6 ERE |
64 | raise |
65 | ||
66 | if __name__ == "__main__": | |
1046c582 ERE |
67 | #import tracemalloc |
68 | #tracemalloc.enable() | |
69 | #top = tracemalloc.DisplayTop(25) | |
70 | #top.show_lineno = True | |
253d4cdd | 71 | #top.start(20) |
10c70dc6 ERE |
72 | parser = argparse.ArgumentParser() |
73 | ||
74 | parser.add_argument("-m", "--mode", default='', | |
75 | help="Mode in which to read/write the backup") | |
76 | parser.add_argument("-t", "--targetpath", help="target path directory") | |
77 | parser.add_argument("-s", "--sourcepath", help="source path directory") | |
78 | parser.add_argument("-p", "--password", default='', help="password") | |
79 | parser.add_argument("-v", "--volsize", default=None, help="maximum volume size, in Megabytes") | |
80 | parser.add_argument("-r", "--restore", action='store_true', help="Restore a backup") | |
81 | parser.add_argument("-f", "--full", action='store_true', help="Create a full backup") | |
82 | parser.add_argument("-d", "--diff", action='store_true', help="Create a diff backup") | |
83 | parser.add_argument("-i", "--indexes", nargs='+', help="indexes paths") | |
8c65a2b1 ERE |
84 | parser.add_argument("-l", "--list-files", action='store_true', help="List files in a tarball") |
85 | parser.add_argument("-x", "--excluded", nargs='+', default=[], | |
86 | help="excluded files") | |
4cbed687 ERE |
87 | parser.add_argument("-inc", "--included", nargs='+', default=[], |
88 | help="included files") | |
89 | parser.add_argument("-ip", "--included-path", default=None, | |
90 | help="path to the file containing included paths") | |
8c65a2b1 ERE |
91 | parser.add_argument("-xp", "--excluded-path", default=None, |
92 | help="path to the file containing excluded paths") | |
10c70dc6 ERE |
93 | parser.add_argument("-e", "--equals", action='store_true', help="Checks two dirs are equal") |
94 | ||
95 | args = parser.parse_args() | |
8c65a2b1 ERE |
96 | |
97 | if args.excluded_path: | |
98 | f = open(args.excluded_path, 'r') | |
99 | excluded_files = f.readlines() | |
100 | f.close() | |
101 | else: | |
102 | excluded_files = args.excluded | |
103 | ||
4cbed687 ERE |
104 | if args.included_path: |
105 | f = open(args.included_path, 'r') | |
106 | included_files = f.readlines() | |
107 | f.close() | |
108 | else: | |
109 | included_files = args.included | |
110 | ||
8c65a2b1 | 111 | deltatar = DeltaTar(mode=args.mode, password=args.password, |
4cbed687 ERE |
112 | logger=consoleLogger, excluded_files=excluded_files, |
113 | included_files=included_files) | |
10c70dc6 ERE |
114 | |
115 | if args.full: | |
116 | deltatar.create_full_backup(args.sourcepath, args.targetpath, args.volsize) | |
117 | elif args.diff: | |
118 | deltatar.create_diff_backup(args.sourcepath, args.targetpath, args.indexes[0], args.volsize) | |
8c65a2b1 ERE |
119 | elif args.list_files: |
120 | deltatar.list_backup(args.sourcepath) | |
10c70dc6 ERE |
121 | elif args.restore: |
122 | if args.indexes is not None: | |
123 | deltatar.restore_backup(args.targetpath, backup_indexes_paths=args.indexes) | |
124 | else: | |
125 | deltatar.restore_backup(args.targetpath, backup_tar_path=args.sourcepath) | |
126 | elif args.equals: | |
1817bfb4 PG |
127 | check_equal_dirs(os.path.abspath(args.sourcepath), os.path.abspath(args.targetpath), deltatar) |
128 | else: | |
129 | import sys | |
130 | print("Nothing to do.\nPlease specify one of --full, --diff, --list-files, " | |
131 | "--restore, --equals.\n", file=sys.stderr) | |
132 | parser.print_help(file=sys.stderr) | |
133 |