|
| 1 | +""" Simple backup script which just creates the root structure in an other |
| 2 | +folder and syncs everything which recursively lies within one of the source |
| 3 | +folders. For files bigger than a threshold they are first gziped.""" |
| 4 | + |
| 5 | +import argparse |
| 6 | +import gzip |
| 7 | +import os |
| 8 | +import shutil |
| 9 | +import sys |
| 10 | +import threading |
| 11 | + |
| 12 | + |
| 13 | +def parse_input(): |
| 14 | + """ |
| 15 | + Argument Parser function, for parsing CLI. |
| 16 | + Returns: parse_args() |
| 17 | + |
| 18 | + """ |
| 19 | + parser = argparse.ArgumentParser() |
| 20 | + parser.add_argument('-t', '--target', nargs=1, required=True, |
| 21 | + help='Target Backup folder') |
| 22 | + parser.add_argument('-s', '--source', nargs='+', required=True, |
| 23 | + help='Source Files to be added') |
| 24 | + parser.add_argument('-c', '--compress', nargs=1, type=int, |
| 25 | + help='Gzip threshold in bytes, Deafault 1024KB', default=[1024000]) |
| 26 | + # Default Threshold is 1024KB |
| 27 | + |
| 28 | + # Help is triggered when there is no Input Provided |
| 29 | + if len(sys.argv) == 1: |
| 30 | + parser.print_help() |
| 31 | + sys.exit() |
| 32 | + |
| 33 | + return parser.parse_args() |
| 34 | + |
| 35 | + |
| 36 | +def size_if_newer(source, target): |
| 37 | + """ |
| 38 | + If there is a difference in file size, this function reports the difference. |
| 39 | + Args: |
| 40 | + source: Source Path |
| 41 | + target: Target for ZIP file |
| 42 | + |
| 43 | + Returns: The Size difference |
| 44 | + |
| 45 | + """ |
| 46 | + |
| 47 | + src_stat = os.stat(source) |
| 48 | + try: |
| 49 | + target_ts = os.stat(target).st_mtime |
| 50 | + except FileNotFoundError: |
| 51 | + try: |
| 52 | + target_ts = os.stat(target + '.gz').st_mtime |
| 53 | + except FileNotFoundError: |
| 54 | + target_ts = 0 |
| 55 | + |
| 56 | + # The time difference of one second is necessary since subsecond accuracy |
| 57 | + # of os.st_mtime is striped by copy2 |
| 58 | + return src_stat.st_size if (src_stat.st_mtime - target_ts > 1) else False |
| 59 | + |
| 60 | + |
| 61 | +def threaded_sync_file(source, target, compress): |
| 62 | + """ |
| 63 | + Multithreading for Synced files. |
| 64 | + Args: |
| 65 | + source: Source Path |
| 66 | + target: Target for ZIP file |
| 67 | + compress: The compression threshold |
| 68 | + |
| 69 | + Returns: The threads |
| 70 | + |
| 71 | + """ |
| 72 | + size = size_if_newer(source, target) |
| 73 | + |
| 74 | + if size: |
| 75 | + thread = threading.Thread(target=transfer_file, |
| 76 | + args=(source, target, size > compress)) |
| 77 | + thread.start() |
| 78 | + return thread |
| 79 | + |
| 80 | + |
| 81 | +def sync_file(source, target, compress): |
| 82 | + """ |
| 83 | + Synchronizing files |
| 84 | + Args: |
| 85 | + source: Source Path |
| 86 | + target: Target for ZIP file |
| 87 | + compress: The compression threshold |
| 88 | + """ |
| 89 | + size = size_if_newer(source, target) |
| 90 | + |
| 91 | + if size: |
| 92 | + transfer_file(source, target, size > compress) |
| 93 | + |
| 94 | + |
| 95 | +def transfer_file(source, target, compress): |
| 96 | + """ |
| 97 | + Transferring files |
| 98 | + Args: |
| 99 | + source: Source Path |
| 100 | + target: Target for ZIP file |
| 101 | + compress: The compression threshold |
| 102 | + """ |
| 103 | + |
| 104 | + try: |
| 105 | + if compress: |
| 106 | + with gzip.open(target + '.gz', 'wb') as target_fid: |
| 107 | + with open(source, 'rb') as source_fid: |
| 108 | + target_fid.writelines(source_fid) |
| 109 | + print('Compress {}'.format(source)) |
| 110 | + else: |
| 111 | + shutil.copy2(source, target) |
| 112 | + print('Copy {}'.format(source)) |
| 113 | + except FileNotFoundError: |
| 114 | + os.makedirs(os.path.dirname(target)) |
| 115 | + transfer_file(source, target, compress) |
| 116 | + |
| 117 | + |
| 118 | +def sync_root(root, arg): |
| 119 | + """ |
| 120 | + Synchronize Root with Target |
| 121 | + |
| 122 | + """ |
| 123 | + target = arg.target[0] |
| 124 | + compress = arg.compress[0] |
| 125 | + threads = [] |
| 126 | + |
| 127 | + for path, _, files in os.walk(root): |
| 128 | + for source in files: |
| 129 | + source = path + '/' + source |
| 130 | + threads.append(threaded_sync_file(source, |
| 131 | + target + source, compress)) |
| 132 | + # sync_file(source, target + source, compress) |
| 133 | + for thread in threads: |
| 134 | + thread.join() |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == '__main__': |
| 138 | + arg = parse_input() |
| 139 | + print('------------------------- Start copy -------------------------') |
| 140 | + print('______________________________________________________________') |
| 141 | + for root in arg.source: |
| 142 | + sync_root(root, arg) |
| 143 | + print('______________________________________________________________') |
| 144 | + print('------------------------- Done Done! -------------------------') |
| 145 | + |
| 146 | +""" |
| 147 | +Example Usage- |
| 148 | +> python Auto_Backup.py --target ./Backup_Folder --source ./Source_Folder |
| 149 | +""" |
0 commit comments