Python_scripts/manage_bdd.py
2025-02-25 16:52:00 +01:00

64 lines
2.0 KiB
Python

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#Nom : manage_bdd.py
#Description : MAnagement d'une base de données postgresql
#Copyright : 2024, CEN 38
#Auteur : Colas Geier
import os
def dump_bdd(file_out,host,base,user,passwd=None,schema=None):
pwd = 'export PGPASSWORD="{pwd}";'.format(pwd=passwd)
cmd = 'pg_dump -h {H} -d {bdd} -U {U} -s > "{f}"'.format(
H=host,
bdd=base,
U=user,
f=file_out
)
if schema:
cmd += ' --schema="{s}"'.format(s=schema)
os.system(pwd+cmd if passwd else cmd)
def copy_2another_server(host_in,base_in,user_in,host_out,base_out,user_out,passwd_in=None,schema_in=None,passwd_out=None,schema_out=None):
# RESTORE AU NOM DE LA BDD ORIGINE
# pg_dump -C -h localhost -U localuser dbname | psql -h remotehost -U remoteuser dbname
# pg_dump -C -h 172.17.0.2 -U postgres -d postgres --schema="07_202107" | psql -h 91.134.194.221 -U cgeier -d bd_cen
# RESTORE AU NOM DE LA BDD CIBLE
# pg_dump -C -h 172.17.0.2 -U postgres -d postgres --schema="*_202207" --format=custom | pg_restore -h 91.134.194.221 -U cgeier --dbname="azalee_restore"
pwd_in = 'export PGPASSWORD="{pwd}";'.format(pwd=passwd_in)
cmd_in = 'pg_dump -C -h {H} -d {bdd} -U {U}'.format(
H=host_in,
bdd=base_in,
U=user_in
)
if schema_in:
cmd_in += ' --schema="{s}"'.format(s=schema_in)
CMD_IN = pwd_in+cmd_in if passwd_in else cmd_in
pwd_out = 'export PGPASSWORD="{pwd}";'.format(pwd=passwd_out)
cmd_out = 'psql -h {H} -d {bdd} -U {U}'.format(
H=host_out,
bdd=base_out,
U=user_out
)
if schema_out:
cmd_out += ' --schema="{s}"'.format(s=schema_out)
CMD_out = pwd_out+cmd_out if passwd_out else cmd_out
os.system(CMD_IN+'|'+CMD_out)
if __name__ == "__main__":
dump_bdd(
file_out='/media/colas/SRV/FICHIERS/OUTILS/BASES DE DONNEES/FONCIER/CEN73/V4/_backup_V3/bd_cen_V3.sql',
host='91.134.194.221',
base='bd_cen',
user='cgeier',
passwd='adm1n*bdCen')