forked from vicke4/ghost-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.py
112 lines (93 loc) · 3.44 KB
/
backup.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
import datetime
import os
import json
import time
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from google.oauth2.credentials import Credentials
from traceback import format_exc
from misc import (
error_and_exit,
execute_command,
format_subprocess_error,
get_ecredentials,
send_notif
)
config = {}
def read_config():
config_file = open('/opt/ghost-backup/.config.json', 'r')
config_file_json = json.loads(config_file.read())
config_file_json['timestamp'] = datetime.datetime.fromtimestamp(
time.time()).strftime('%Y%m%d')
config.update(config_file_json)
def dump_db():
if config['images'] and config['themes']:
dump_path = config['images_dir'] if config['images'] else config['themes_dir']
config['dump_path'] = os.path.normpath(dump_path + '/..')
else:
config['dump_path'] = os.getcwd()
config['dump_file'] = config['dump_path'] + '/{0}.sql'.format(config['timestamp'])
dump_command = ("mysqldump -h{mysql_hostname} -u'{mysql_username}' "
"-p'{mysql_password}' {mysql_db_name} > {0}".format(
config['dump_file'],
**config
)
)
status = execute_command(dump_command)
if status.returncode != 0:
print("state code here %s " %status.returncode)
error_and_exit(
'\nError while taking DB dump\n\n{0}'.format(format_subprocess_error(status)),
config.get('telegram_user_id')
)
def pack_files():
compress_command = 'tar -C {0} -cvzf {1}-{2}.tar.gz {2}.sql'.format(
config['dump_path'], config['app_name'], config['timestamp'])
if config['images']:
compress_command += ' images'
if config['themes']:
compress_command += ' themes'
status = execute_command(compress_command)
if status.returncode != 0:
print("state code here %s " %status.returncode)
error_and_exit(
'\nError while packing backup files\n\n{0}'.format(format_subprocess_error(status)),
config.get('telegram_user_id')
)
def upload_files():
oauth_config = config['oauth']
credentials = Credentials(
None,
refresh_token=oauth_config['refresh_token'],
token_uri=oauth_config['token_uri'],
client_id=get_ecredentials('yatch'),
client_secret=get_ecredentials('bakery')
)
drive = build('drive', 'v3', credentials=credentials)
media = MediaFileUpload('{0}-{1}.tar.gz'.format(config['app_name'], config['timestamp']))
file_metadata = {
'name': ( '{app_name}' + '-' + config['timestamp'] + '.tar.gz').format(**config),
'mimeType': 'application/gzip'
}
resp = drive.files().update(
body=file_metadata,
fileId=config['backup_file_id'],
media_body=media
).execute()
def delete_backups():
execute_command('rm {0} {1}.tar.gz'.format(config['dump_file'], config['timestamp']))
def main():
read_config()
dump_db()
pack_files()
upload_files()
delete_backups()
send_notif(config.get('telegram_user_id'), 'Backup completed successfully for ' + config.get('app_name') + '.com' + '!')
if __name__ == '__main__':
try:
main()
except Exception as e:
error_and_exit("\nFollowing error occured:\n{0}\n\n"
"More info about the error:\n{1}".format(e, format_exc()),
config.get('telegram_user_id')
)