Skip to content

Instantly share code, notes, and snippets.

@fintanmm
Last active October 27, 2016 14:49
Show Gist options
  • Save fintanmm/17378bcc889b6d3655f869ee70238906 to your computer and use it in GitHub Desktop.
Save fintanmm/17378bcc889b6d3655f869ee70238906 to your computer and use it in GitHub Desktop.
Simple archive and clean postgresql wal files.
from os import listdir, setgid, setuid
from subprocess import call
import psycopg2
# We run the sql as the postgres user
setgid(104)
setuid(109)
conn = psycopg2.connect("user=postgres")
cur = conn.cursor()
# Start backup
cur.execute("SELECT pg_start_backup('base'), current_timestamp;")
cur.execute("SELECT pg_sleep(5);")
cur.execute("SELECT pg_stop_backup();")
cur.execute("SHOW server_version;")
version = cur.fetchall()
conn.commit()
cur.close()
conn.close()
archive_path = '/data/postgresql/archive/'
usage = disk_usage(archive_path)
percentage = usage.used/usage.total
weight = .70
backup_files = [f for f in listdir(archive_path) if '.backup' in f]
backup_files.sort()
if backup_files and percentage > weight:
last_backup_file = backup_files[-1]
try:
pg_archivecleanup = "/usr/lib/postgresql/{0}/bin/pg_archivecleanup".format(version[0][0][0:3])
cmd = [pg_archivecleanup, archive_path, last_backup_file]
print(' '.join(cmd))
call(cmd)
except Exception as e:
raise e
else:
print('No wal files to archive.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment