-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.py
More file actions
70 lines (53 loc) · 1.99 KB
/
backup.py
File metadata and controls
70 lines (53 loc) · 1.99 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
61
62
63
64
65
66
67
68
69
70
import logging
import dj_database_url
import configargparse
from datetime import datetime
from sultan.api import Sultan
from urllib import parse as urlparse
import ftputil
root = logging.getLogger()
root.setLevel(logging.INFO)
p = configargparse.ArgumentParser()
p.add('--folder', required=True, help='folder to backup',
env_var='FOLDER')
p.add('--ftp', required=True, help='FTP url (e.g. ftp://backup:password@backup.network/backups/mydb)',
env_var='FTP_URL')
p.add('--max', required=False, help='maximum count of backups',
env_var='MAX_FILES', type=int, default=100)
p.add('--name', required=False, help='backup name',
env_var='BACKUP_NAME', default='manual_backup')
options = p.parse_args()
s = Sultan()
def backup_front_name():
return options.name
def backup_name():
now = datetime.now()
now = now.replace(microsecond=0)
return backup_front_name() + '-' + now.isoformat() + '.tar.gz'
def backup(folder, ftp, ):
logging.info('Starting backup')
name = backup_name()
path = '/tmp/' + name
logging.info('Backup data to {}'.format(path))
s.tar('cf', path, folder).run()
ftp = urlparse.urlparse(ftp, 'ftp')
logging.info('Sending backup {} to {}'.format(path, ftp.hostname))
host = ftp.hostname
if ftp.port:
host = host + ':' + ftp.port
# noinspection is due to an error into FTPHost code
# noinspection PyDeprecation
with ftputil.FTPHost(host, ftp.username, ftp.password) as host:
host.makedirs(ftp.path)
host.upload_if_newer(path, ftp.path + name + '.tar.gz')
host.chdir(ftp.path)
files = [file for file in host.listdir(ftp.path) if file.startswith(backup_front_name())]
files.sort()
while len(files) > options.max:
old_file = files.pop(0)
host.remove(ftp.path + old_file)
logging.info("Deleted old file {}".format(old_file))
s.rm(path).run()
logging.info('Ended')
if __name__ == '__main__':
backup(options.folder, options.ftp)