-
Notifications
You must be signed in to change notification settings - Fork 0
/
bkp.py
executable file
·133 lines (102 loc) · 3.55 KB
/
bkp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env conda run -n bkp python
#############################################
from getpass import getpass
from datetime import datetime
import tarfile
import os.path
import os
import shutil
import sys
import click
import pyAesCrypt
#############################################
BUFFER_SIZE = 64 * 1024
#############################################
@click.command()
@click.option('-c', '--create', is_flag=True)
@click.option('-x', '--extract', is_flag=True)
@click.option('-e', '--encrypt', is_flag=True)
@click.option('-d', '--decrypt', is_flag=True)
@click.option('-t', '--timestamp', is_flag=True)
@click.option('--extension/--no-extension', default=True)
@click.argument('path', type=click.Path(exists=True))
@click.argument('dest', default='')
def main(create, extract, encrypt, decrypt, timestamp, extension, path, dest):
"""
DEST if not provided will be same path with '.bkp' for archiving or '.full' for extracting
"""
final_extension = ''
if timestamp:
final_extension += '.' + get_timestamp()
if extension:
final_extension += '.bkp' if (create and not extract) else '.full'
if dest == '':
dest = path + final_extension
else:
dest = dest + final_extension
if create and not extract:
click.echo(click.style('Copying files ...', fg='cyan'))
cp(path, dest)
make_tarfile(dest, dest + '.tgz')
rm(dest)
mv(dest + '.tgz', dest)
click.echo(click.style('Archive created.', fg='green'))
if encrypt and not decrypt:
encrypt_aes(dest, dest + '.enc')
rm(dest)
mv(dest + '.enc', dest)
click.echo(click.style('Archive encrypted.', fg='green'))
elif extract and not create:
if decrypt and not encrypt:
decrypt_aes(path, dest + '.dec')
click.echo(click.style('Archive decrypted.', fg='green'))
extract_tarfile(dest + '.dec', dest)
rm(dest + '.dec')
click.echo(click.style('Extraction complete.', fg='green'))
else:
extract_tarfile(input, dest)
click.echo(click.style('Extraction complete.', fg='green'))
#############################################
def make_tarfile(source_dir, output_filename):
with tarfile.open(output_filename, "w:gz") as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))
def extract_tarfile(path, dest):
tar = tarfile.open(path)
tar.extractall(path=dest)
tar.close()
def encrypt_aes(path, dest):
if os.path.isfile(path):
password = prompt_pswd(confirmation=True)
pyAesCrypt.encryptFile(path, dest, password, BUFFER_SIZE)
def decrypt_aes(path, dest):
password = prompt_pswd()
pyAesCrypt.decryptFile(path, dest, password, BUFFER_SIZE)
def get_timestamp():
now = datetime.now()
return now.strftime("%d.%m.%Y.%H.%M.%S")
def mv(path, dest):
os.rename(path, dest)
def cp(path, dest):
if os.path.isdir(path):
shutil.copytree(path, dest)
if os.path.isfile(path):
shutil.copyfile(path, dest)
def rm(path):
if os.path.isdir(path):
shutil.rmtree(path)
elif os.path.isfile(path):
os.remove(path)
def prompt_pswd(confirmation=False):
entry1 = getpass()
if not confirmation:
return entry1
else:
entry2 = getpass('Confirmation: ')
if entry1 != entry2:
click.echo(click.style('Not matching', fg='red'))
sys.exit(-1)
else:
return entry1
#############################################
if __name__ == '__main__':
main()