Skip to content

Instantly share code, notes, and snippets.

@des1roer
Created June 18, 2015 03:41
Show Gist options
  • Save des1roer/637f7cea1b4b0efbf375 to your computer and use it in GitHub Desktop.
Save des1roer/637f7cea1b4b0efbf375 to your computer and use it in GitHub Desktop.
Pyhton script для дампа и удаления партиции
#!/usr/bin/python
# -*- coding: utf-8 -*-
# python 2
# https://wiki.postgresql.org/wiki/Psycopg
import os
from datetime import datetime, time, timedelta, date
import psycopg2
import sys
conn = None
try:
conn = psycopg2.connect(user='user', password='pas', host='***.com', dbname='dname', port=5432)
cur = conn.cursor()
today = date.today()
month = today.replace(year=today.year - 3, month=today.month, day=today.day)
month = month.strftime("%Y-%m")
cur.execute('''select 1 from "myshem"."tagdata_%s";'''% month)
#dump
DB_HOST = 'localhost'
DB_USER = 'user'
DB_NAME = 'dname'
BACKUP_PATH = '/backups/tagdata/'
passFileCmd = 'export PGPASSFILE=/root/.pgpass'
os.popen(passFileCmd)
currentBackupName = BACKUP_PATH + DB_NAME + "_" + month + ".gz"
dumpcmd = "pg_dump -t SC_Tag.tagdata_+"+month+" -U " + DB_USER + " -w " + DB_NAME + " | gzip >> " + currentBackupName
#C:\Program Files (x86)\EMS\SQL Manager Lite for PostgreSQL\Dump\pg_dump93.exe -h asutpdb.vgok.ru -p 5432 -U asutp -F p -E UTF8 -Z 9 -t """SC_Tag"".""tagdata_2014-06""" -v -f "D:\postgres\pg\my.gzip" DB_ASUTP_2
retval = os.popen(dumpcmd).read()
#dump
except psycopg2.DatabaseError, e:
print 'Error %s' % e
sys.exit(1)
finally:
if conn:
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment