Amazing-Python-Scripts

Форк
0
156 строк · 4.3 Кб
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',
21
                        '--target',
22
                        nargs=1,
23
                        required=True,
24
                        help='Target Backup folder')
25
    parser.add_argument('-s',
26
                        '--source',
27
                        nargs='+',
28
                        required=True,
29
                        help='Source Files to be added')
30
    parser.add_argument('-c',
31
                        '--compress',
32
                        nargs=1,
33
                        type=int,
34
                        help='Gzip threshold in bytes, Deafault 1024KB',
35
                        default=[1024000])
36
    # Default Threshold is 1024KB
37

38
    # Help is triggered when there is no Input Provided
39
    if len(sys.argv) == 1:
40
        parser.print_help()
41
        sys.exit()
42

43
    return parser.parse_args()
44

45

46
def size_if_newer(source, target):
47
    """
48
    If there is a difference in file size, this function reports the difference.
49
    Args:
50
        source: Source Path
51
        target: Target for ZIP file
52

53
    Returns: The Size difference
54

55
    """
56
    src_stat = os.stat(source)
57
    try:
58
        target_ts = os.stat(target).st_mtime
59
    except FileNotFoundError:
60
        try:
61
            target_ts = os.stat(target + '.gz').st_mtime
62
        except FileNotFoundError:
63
            target_ts = 0
64

65
    # The time difference of one second is necessary since subsecond accuracy
66
    # of os.st_mtime is striped by copy2
67
    return src_stat.st_size if (src_stat.st_mtime - target_ts > 1) else False
68

69

70
def threaded_sync_file(source, target, compress):
71
    """
72
    Multithreading for Synced files.
73
    Args:
74
        source: Source Path
75
        target: Target for ZIP file
76
        compress: The compression threshold
77

78
    Returns: The threads
79

80
    """
81
    size = size_if_newer(source, target)
82

83
    if size:
84
        thread = threading.Thread(target=transfer_file,
85
                                  args=(source, target, size > compress))
86
        thread.start()
87
        return thread
88

89

90
def sync_file(source, target, compress):
91
    """
92
    Synchronizing  files
93
    Args:
94
        source: Source Path
95
        target: Target for ZIP file
96
        compress: The compression threshold
97
    """
98
    size = size_if_newer(source, target)
99

100
    if size:
101
        transfer_file(source, target, size > compress)
102

103

104
def transfer_file(source, target, compress):
105
    """
106
    Transferring  files
107
    Args:
108
        source: Source Path
109
        target: Target for ZIP file
110
        compress: The compression threshold
111
    """
112
    try:
113
        if compress:
114
            with gzip.open(target + '.gz', 'wb') as target_fid:
115
                with open(source, 'rb') as source_fid:
116
                    target_fid.writelines(source_fid)
117
            print('Compress {}'.format(source))
118
        else:
119
            shutil.copy2(source, target)
120
            print('Copy {}'.format(source))
121
    except FileNotFoundError:
122
        os.makedirs(os.path.dirname(target))
123
        transfer_file(source, target, compress)
124

125

126
def sync_root(root, arg):
127
    """
128
    Synchronize Root with Target
129

130
    """
131
    target = arg.target[0]
132
    compress = arg.compress[0]
133
    threads = []
134

135
    for path, _, files in os.walk(root):
136
        for source in files:
137
            source = path + '/' + source
138
            threads.append(
139
                threaded_sync_file(source, target + source, compress))
140
    #            sync_file(source, target + source, compress)
141
    for thread in threads:
142
        thread.join()
143

144

145
if __name__ == '__main__':
146
    arg = parse_input()
147
    print('------------------------- Start copy -------------------------')
148
    print('______________________________________________________________')
149
    for root in arg.source:
150
        sync_root(root, arg)
151
    print('______________________________________________________________')
152
    print('------------------------- Done Done! -------------------------')
153
"""
154
Example Usage-
155
> python Auto_Backup.py --target ./Backup_Folder --source ./Source_Folder
156
"""
157

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.