diff --git a/servers/puppet/modules/humbug/files/postgresql/pg_backup_and_purge.py b/servers/puppet/modules/humbug/files/postgresql/pg_backup_and_purge.py new file mode 100644 index 0000000000..d5eba3acc5 --- /dev/null +++ b/servers/puppet/modules/humbug/files/postgresql/pg_backup_and_purge.py @@ -0,0 +1,45 @@ +#!/usr/bin/python + +import subprocess +import sys +import logging +import dateutil.parser +import pytz +from datetime import datetime, timedelta + +logger = logging.getLogger(__name__) + +def run(args, dry_run=False): + if dry_run: + print "Would have run: " + " ".join(args) + return "" + + p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + stdout, stderr = p.communicate() + if p.returncode: + logger.error("Could not invoke %s\nstdout: %s\nstderror: %s" + % (args[0], stdout, stderr)) + sys.exit(1) + return stdout + +# Only run if we're the master +if run(['psql', '-t', '-c', 'select pg_is_in_recovery()']).strip() != 'f': + sys.exit(0) + +run(['env-wal-e', 'backup-push', '/var/lib/postgresql/9.1/main']) + +backups = {} +lines = run(['env-wal-e', 'backup-list']).split("\n") +for line in lines[1:]: + if line: + backup_name, date, _, _ = line.split() + backups[dateutil.parser.parse(date)] = backup_name + +one_month_ago = datetime.now(tz=pytz.utc) - timedelta(days=30) +for date in sorted(backups.keys(), reverse=True): + if date < one_month_ago: + run(['env-wal-e', 'delete', '--confirm', 'before', backups[date]]) + # Because we're going from most recent to least recent, we + # only have to do one delete operation + break diff --git a/servers/puppet/modules/humbug/manifests/postgres-common.pp b/servers/puppet/modules/humbug/manifests/postgres-common.pp index 249e85156e..003105bf58 100644 --- a/servers/puppet/modules/humbug/manifests/postgres-common.pp +++ b/servers/puppet/modules/humbug/manifests/postgres-common.pp @@ -3,7 +3,7 @@ class humbug::postgres-common { $postgres_packages = [ "postgresql-9.1", "pgtune", "python-boto", "python-argparse", "python-gevent", - "lzop", "pv", "hunspell-en-us"] + "lzop", "pv", "hunspell-en-us", "python-dateutil"] package { $postgres_packages: ensure => "installed" } exec {"pip_wal-e": @@ -21,6 +21,15 @@ class humbug::postgres-common { source => "puppet:///modules/humbug/postgresql/env-wal-e", } + file { "/usr/local/bin/pg_backup_and_purge.py": + ensure => file, + owner => "root", + group => "postgres", + mode => 754, + source => "puppet:///modules/humbug/postgresql/pg_backup_and_purge.py", + require => File["/usr/local/bin/env-wal-e"], + } + file { "/etc/postgresql/9.1/main/pg_hba.conf": require => Package["postgresql-9.1"], ensure => file,