-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplebackup.py
More file actions
65 lines (49 loc) · 1.77 KB
/
simplebackup.py
File metadata and controls
65 lines (49 loc) · 1.77 KB
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
#!/usr/bin/env python3
from subprocess import run
from os import environ
from sys import exit
from datetime import datetime
def main():
#Get user's home Directory and append /Directory
home = environ['HOME'] + '/Documents'
#Set gpguser, most likely the owner of $home
gpguser = environ['USER']
#Temporary backup location
temp_dir = '/tmp'
#Get time
now = datetime.now()
time_str = str(now.day) + '_' + str(now.month) + '_' + str(now.year)
now_str = str(now.hour) + ':' + str(now.minute) + ':' + str(now.second)
#Full Path Filename of backup
backname = temp_dir + '/' + time_str + '_' + environ['USER'] + '_' + 'Documents.tgz'
cryptout = backname + '.pgp'
#Create Archive
print("Creating Archive.")
backup_job = run(['tar', '-czf', backname, home])
#Check for successful archive creation
if backup_job.returncode != 0:
print('A filesystem error has occurred, exiting')
exit(1)
else:
print('Archive completed successfully.')
#Encrypt with pgp
print(f'Encrypting {backname} as {backname}.pgp')
crypt = run(['gpg', '-r', gpguser, '-o', cryptout, '-e', backname])
if crypt.returncode == 0:
print(f'{backname} encrypted successfully.')
else:
print(f'could not encrypt {backname}, exiting')
exit(1)
##Backup to gdrive with rclone
run_args = ['rclone', 'copy', cryptout, 'gdrive:backup']
print('Backing up to google drive.')
print(f'Started at {now_str}')
cloud_backup_job = run(run_args)
##Get time after upload
now = datetime.now()
now_str = str(now.hour) + ':' + str(now.minute) + ':' + str(now.second)
print('Upload was successful.')
print(f'Finished at {now_str}')
exit(0)
if __name__ == "__main__":
main()