Bakcup DB
This commit is contained in:
parent
20f0d08c3e
commit
8d786121a5
|
@ -1,4 +1,9 @@
|
|||
|
||||
0.2.0 [28-Ene-2021]
|
||||
---------------------
|
||||
- Script para respaldar localmente la base de datos.
|
||||
|
||||
|
||||
0.1.0 [25-Ene-2021]
|
||||
---------------------
|
||||
- Script para actualizar Empresa Libre
|
||||
- Script para actualizar Empresa Libre.
|
||||
|
|
|
@ -4,6 +4,7 @@ import argparse
|
|||
import logging
|
||||
import shlex
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
LOG_FORMAT = '%(asctime)s - %(levelname)s - %(message)s'
|
||||
|
@ -16,6 +17,7 @@ log = logging.getLogger(__name__)
|
|||
|
||||
|
||||
PATH_SISTEMA = '/opt/empresa-libre'
|
||||
PATH_BK = f'{Path.home()}/compartido/bkdb'
|
||||
|
||||
|
||||
def _call(args):
|
||||
|
@ -39,12 +41,45 @@ def _actualizar():
|
|||
return
|
||||
|
||||
|
||||
def _backup():
|
||||
cmd = f'mkdir -p {PATH_BK}'
|
||||
_call(cmd)
|
||||
|
||||
msg = 'Respaldando bases de datos...'
|
||||
log.info(msg)
|
||||
|
||||
db_exclude = ('', 'postgres', 'template1', 'template0')
|
||||
args = 'psql -U postgres -t -c "select datname from pg_database"'
|
||||
result = _call(args).split('\n')
|
||||
clients = sorted([e.strip() for e in result if not e.strip() in db_exclude])
|
||||
|
||||
cmd = 'pg_dump -U postgres -Fc {} -f "{}"'
|
||||
t = len(clients)
|
||||
for i, f in enumerate(clients):
|
||||
msg = f'\t{i+1:02} de {t} - BD: {f}'
|
||||
log.info(msg)
|
||||
|
||||
path = _join(PATH_BK, f'{f}.bk')
|
||||
_call(cmd.format(f, path))
|
||||
log.info('\t\tRespaldado localmente...')
|
||||
|
||||
# ~ _send_to_seafile(f, path)
|
||||
|
||||
msg = 'Base de datos respaldadas...'
|
||||
log.info(msg)
|
||||
return
|
||||
|
||||
|
||||
def main(args):
|
||||
|
||||
if args.actualizar:
|
||||
_actualizar()
|
||||
return
|
||||
|
||||
if args.backup:
|
||||
_backup()
|
||||
return
|
||||
|
||||
return
|
||||
|
||||
|
||||
|
@ -53,6 +88,8 @@ def _process_command_line_arguments():
|
|||
description='Empresa Libre Util')
|
||||
parser.add_argument('-a', '--actualizar', dest='actualizar',
|
||||
action='store_true', default=False, required=False)
|
||||
parser.add_argument('-b', '--backup', dest='backup',
|
||||
action='store_true', default=False, required=False)
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue