Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1840c06

Browse files
Added AutoBackup
1 parent eb8b816 commit 1840c06

File tree

7 files changed

+2566
-0
lines changed

7 files changed

+2566
-0
lines changed

‎Auto_Backup/Auto_Backup.ipynb

Lines changed: 2370 additions & 0 deletions
Large diffs are not rendered by default.

‎Auto_Backup/Auto_Backup.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
"""

‎Auto_Backup/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Automatic_Backup
2+
3+
Automatic Backup and Compression of large file, sped up using Threading.
4+
Multithreading helps in achieving MultiTasking using threads.
5+
6+
Along with Multithreading `gzip` has been used for Compressing large files.
7+
I have made sur that the script required no additional library other than the basic standard packages.
8+
9+
## Setup instructions
10+
11+
As explained there is no specific `requirements.txt`. So no additional library or packages are required.
12+
13+
There are two files one `python` and one `notebook`. [`Auto_Backup.py`](./Auto_Backup.py)
14+
is the script that can be quickly used to backup the desired file.
15+
16+
For greater understanding of the script and proof of concept, refer to [`Auto_Backup.ipynb`](./Auto_Backup.ipynb).
17+
The Notebook has further illustrated the Script and is much more detailed.
18+
19+
```
20+
Example Usage -
21+
22+
python Auto_backup.py -t ./MIREX_Backup -s ./MIREX_Dataset -c 100000
23+
24+
```
25+
26+
## Output
27+
28+
Command line Input
29+
30+
![Command Line Input](img/Command.PNG)
31+
32+
Directory Before Backup
33+
34+
![Before Compression](img/Before.PNG)
35+
36+
Directory After Backup
37+
38+
![After Backup](img/After.PNG)
39+
40+
Size Comparison after and before -
41+
42+
![CLI2](img/size.PNG)
43+
44+
45+
## Author(s)
46+
47+
Made by [Vybhav Chaturvedi](https://www.linkedin.com/in/vybhav-chaturvedi-0ba82614a/)

‎Auto_Backup/img/After.PNG

75.3 KB
Loading[フレーム]

‎Auto_Backup/img/Before.PNG

56.2 KB
Loading[フレーム]

‎Auto_Backup/img/Command.PNG

51 KB
Loading[フレーム]

‎Auto_Backup/img/size.PNG

32.3 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /