Projet

Général

Profil

0001-ctl-add-dump-in-backup-command-if-postgresql-is-true.patch

Jean-Baptiste Jaillet, 25 juillet 2017 01:36

Télécharger (3,08 ko)

Voir les différences:

Subject: [PATCH] ctl: add dump in backup command if postgresql is true
 (#17726)

 wcs/ctl/backup.py | 42 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)
wcs/ctl/backup.py
17 17
import tarfile
18 18
import time
19 19
import os
20
import shutil
21
import zipfile
22
import subprocess
20 23

  
21 24
from qommon.ctl import Command, make_option
22 25

  
......
42 45
        if not os.path.exists(pub.app_dir):
43 46
            return 1
44 47

  
48
        pub.set_config()
45 49
        if sub_options.filename:
50
            file_name = ''
46 51
            backup_filepath = sub_options.filename
47 52
        else:
48 53
            backup_dir = os.path.join(pub.app_dir, 'backups')
49 54
            if not os.path.exists(backup_dir):
50 55
                os.mkdir(backup_dir)
56
            file_name = '%s-backup-%s%s%s-%s%s%s' % (hostname,
57
                                                     time.localtime()[:6])
51 58
            backup_filepath = os.path.join(backup_dir,
52
                            'backup-%s%s%s-%s%s%s.tar.gz' % time.localtime()[:6])
59
                                           '%s.tar.gz' % file_name)
53 60

  
54 61
        backup = tarfile.open(backup_filepath, mode='w:gz')
55 62
        for basename, dirnames, filenames in os.walk(pub.app_dir):
......
62 69

  
63 70
        backup.close()
64 71

  
72
        if pub.is_using_postgresql():
73
            dest_dir = os.path.dirname(backup_filepath)
74
            db_conf = pub.cfg['postgresql']
75
            # use 'or' as value are at None when not set
76
            db_name = db_conf.get('database') or ''
77
            db_host = db_conf.get('host') or ''
78
            db_user = db_conf.get('user') or ''
79
            db_port = db_conf.get('port') or ''
80
            db_pwd = db_conf.get('password') or ''
81
            if db_pwd:
82
                os.environ['PGPASSWORD'] = db_pwd
83

  
84
            if not file_name:
85
                file_name = '%s-backup-%s%s%s-%s%s%s' % (hostname,
86
                                                         time.localtime()[:6])
87
            dump_path = os.path.join(dest_dir, '%s.dump' % file_name)
88

  
89
            subprocess.check_call(['pg_dump', '-Oc', '-h', db_host,
90
                                   '-U', db_user, '-p', db_port,
91
                                   '-w', '-f', dump_path, '-d', db_name])
92
            if db_pwd:
93
                os.environ.pop('PGPASSWORD')
94

  
95
            zip_path = os.path.join(dest_dir, '%s.zip' % file_name)
96
            with zipfile.ZipFile(zip_path, 'a') as zip_file:
97
                zip_file.write(backup_filepath,
98
                               os.path.basename(backup_filepath))
99
                zip_file.write(dump_path,
100
                               os.path.basename(dump_path))
101

  
102
            os.remove(backup_filepath)
103
            os.remove(dump_path)
104

  
65 105

  
66 106
CmdBackup.register()
67
-