user pios cat home user backup_artely py coding utf-8 import os import

 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
user@pios:~$ cat /home/user/backup_artely.py
#-*- coding: utf-8 -*-
import os
import time
DB_HOST = "127.0.0.1"
DB_PORT = 5432
DB_NAME = ""
DB_USER = ""
DB_PASS = ""
S3_BUCKET_NAME = "artely"
DATE = time.strftime("%Y_%m_%d_%H%M", time.gmtime())
DIR_TMP = "/tmp/backup_%s" % DATE
ARCHIVE_NAME = "%s.tgz" % DATE
ARCHIVE_PATH = "/tmp/%s" % ARCHIVE_NAME
DIR_ROOT = "/var/www/artel.www"
if not os.path.exists(DIR_TMP):
os.mkdir(DIR_TMP)
# Backup database
CMD_PG_DUMP = "export PGPASSWORD=%s && pg_dump %s -O -x -h%s -p %d -U %s -f %s/db.sql"
CMD_PG_DUMP %= DB_PASS, DB_NAME, DB_HOST, DB_PORT, DB_USER, DIR_TMP
os.system(CMD_PG_DUMP)
# Copy user uploads
CMD_CP_STATIC = "cp -r %s/www/static %s/static" % (DIR_ROOT, DIR_TMP)
os.system(CMD_CP_STATIC)
# Create archive
CMD_ARCHIVE = "tar -czf %s %s" % (ARCHIVE_PATH, DIR_TMP)
os.system(CMD_ARCHIVE)
# Remove folder
CMD_REMOVE = "rm -rf %s" % DIR_TMP
os.system(CMD_REMOVE)
# Upload archive
CMD_UPLOAD = "s3cmd put %s s3://%s/%s"
CMD_UPLOAD %= ARCHIVE_PATH, S3_BUCKET_NAME, ARCHIVE_NAME
os.system(CMD_UPLOAD)
# Unlink archive file
CMD_REMOVE = "unlink %s" % ARCHIVE_PATH
os.system(CMD_REMOVE)
user@pios-ru:~$