Merge lp:~barryprice/charms/trusty/postgresql/more_frequent_backups into lp:charms/trusty/postgresql

Proposed by Barry Price
Status: Merged
Merged at revision: 143
Proposed branch: lp:~barryprice/charms/trusty/postgresql/more_frequent_backups
Merge into: lp:charms/trusty/postgresql
Diff against target: 154 lines (+27/-19)
6 files modified
config.yaml (+2/-2)
hooks/postgresql.py (+1/-0)
scripts/pgbackup.py (+4/-5)
templates/pg_backup_job.tmpl (+17/-9)
templates/postgres.cron.tmpl (+2/-2)
tests/test_postgresql.py (+1/-1)
To merge this branch: bzr merge lp:~barryprice/charms/trusty/postgresql/more_frequent_backups
Reviewer Review Type Date Requested Status
Review Queue (community) automated testing Approve
Stuart Bishop (community) Approve
Review via email: mp+283245@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Barry Price (barryprice) wrote :

As stub points out, there will usually be at least two backup files per run, potentially more (basically, $number_of_databases +1 for globals).

So the pruning of old backups needs some work here, I'm setting this to WIP for now.

145. By Barry Price

Another attempt at getting the DB deletions correct

146. By Barry Price

Correct syntax

Revision history for this message
Stuart Bishop (stub) wrote :

Looks good.

review: Approve
Revision history for this message
Review Queue (review-queue) wrote :

This item has failed automated testing! Results available here http://juju-ci.vapour.ws:8080/job/charm-bundle-test-lxc/2273/

review: Needs Fixing (automated testing)
Revision history for this message
Review Queue (review-queue) wrote :

The results (PASS) are in and available here: http://juju-ci.vapour.ws:8080/job/charm-bundle-test-aws/2252/

review: Approve (automated testing)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'config.yaml'
--- config.yaml 2015-12-30 09:05:19 +0000
+++ config.yaml 2016-01-26 10:45:35 +0000
@@ -124,11 +124,11 @@
124 backup_schedule:124 backup_schedule:
125 default: "13 4 * * *"125 default: "13 4 * * *"
126 type: string126 type: string
127 description: Cron-formatted schedule for daily database backups.127 description: Cron-formatted schedule for regular database backups.
128 backup_retention_count:128 backup_retention_count:
129 default: 7129 default: 7
130 type: int130 type: int
131 description: Number of daily backups to retain.131 description: Number of backups to retain.
132 nagios_context:132 nagios_context:
133 default: "juju"133 default: "juju"
134 type: string134 type: string
135135
=== modified file 'hooks/postgresql.py'
--- hooks/postgresql.py 2015-10-01 01:36:42 +0000
+++ hooks/postgresql.py 2016-01-26 10:45:35 +0000
@@ -132,6 +132,7 @@
132 ver = version()132 ver = version()
133 return set(['postgresql-{}'.format(ver),133 return set(['postgresql-{}'.format(ver),
134 'postgresql-common', 'postgresql-client-common',134 'postgresql-common', 'postgresql-client-common',
135 'run-one',
135 'postgresql-contrib-{}'.format(ver),136 'postgresql-contrib-{}'.format(ver),
136 'postgresql-client-{}'.format(ver)])137 'postgresql-client-{}'.format(ver)])
137138
138139
=== modified file 'scripts/pgbackup.py'
--- scripts/pgbackup.py 2014-03-11 13:37:56 +0000
+++ scripts/pgbackup.py 2016-01-26 10:45:35 +0000
@@ -1,11 +1,11 @@
1#!/usr/bin/python1#!/usr/bin/python
22
3# Copyright 2008-2014 Canonical Ltd. All rights reserved.3# Copyright 2008-2016 Canonical Ltd. All rights reserved.
44
5"""5"""
6Backup one or more PostgreSQL databases.6Backup one or more PostgreSQL databases.
77
8Suitable for use in crontab for daily backups.8Suitable for use in crontab for regular backups.
9"""9"""
1010
11__metaclass__ = type11__metaclass__ = type
@@ -24,9 +24,7 @@
2424
2525
26def main(options, databases):26def main(options, databases):
27 #Need longer file names if this is used more than daily27 today = datetime.now().strftime('%Y%m%d_%H%M%S')
28 #today = datetime.now().strftime('%Y%m%d_%H:%M:%S')
29 today = datetime.now().strftime('%Y%m%d')
3028
31 backup_dir = options.backup_dir29 backup_dir = options.backup_dir
32 rv = 030 rv = 0
@@ -80,6 +78,7 @@
80 # If the file already exists, it is from an older dump today.78 # If the file already exists, it is from an older dump today.
81 # We don't know if it was successful or not, so abort on this79 # We don't know if it was successful or not, so abort on this
82 # dump. Leave for operator intervention80 # dump. Leave for operator intervention
81 # n.b. this is extremely unlikely with %H%M%S in the filename
83 if os.path.exists(dest):82 if os.path.exists(dest):
84 log.error("%s already exists. Skipping." % dest)83 log.error("%s already exists. Skipping." % dest)
85 continue84 continue
8685
=== modified file 'templates/pg_backup_job.tmpl'
--- templates/pg_backup_job.tmpl 2015-08-11 10:36:28 +0000
+++ templates/pg_backup_job.tmpl 2016-01-26 10:45:35 +0000
@@ -6,12 +6,12 @@
6export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin6export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
77
8usage () {8usage () {
9 echo "usage: $0 <retain_days>"9 echo "usage: $0 <retain_files>"
10}10}
1111
12typeset -i DAYS=$112typeset -i FILES=$1
13shift13shift
14if [ $DAYS -le 0 ]; then14if [ $FILES -le 0 ]; then
15 usage15 usage
16 exit 116 exit 1
17fi17fi
@@ -27,14 +27,14 @@
27# Set umask so backups are automatically created without group and other27# Set umask so backups are automatically created without group and other
28umask 007728umask 0077
2929
30echo backups started `date`30echo backups started $(date)
3131
32export PGPORT=5432 32export PGPORT=5432
33export PGUSER=postgres33export PGUSER=postgres
3434
35DUMP_DIR={{backups_dir}}35DUMP_DIR={{backups_dir}}
36SCRIPTS={{scripts_dir}}36SCRIPTS={{scripts_dir}}
37TODAY=`date +'%Y%m%d'`37TODAY=$(date +'%Y%m%d_%H%M%S')
3838
39# Dump globals, which we often need to restore a database dump.39# Dump globals, which we often need to restore a database dump.
40pg_dumpall --globals-only --quote-all-identifiers \40pg_dumpall --globals-only --quote-all-identifiers \
@@ -81,9 +81,17 @@
81nice -n 19 ${SCRIPTS}/dump-pg-db --dir=${DUMP_DIR} --compression=postgres ${DATABASES}81nice -n 19 ${SCRIPTS}/dump-pg-db --dir=${DUMP_DIR} --compression=postgres ${DATABASES}
8282
83# Prune old backups:83# Prune old backups:
84# - Delete anything older than $DAYS days84# - Delete anything beyond $FILES count of each backup
85find ${DUMP_DIR}/ -mtime +${DAYS} -delete85
8686# get name/size of the globals dump from $FILES runs ago
87echo backups completed `date`87THRESHOLD_GLOBALS_DUMP=$(find ${DUMP_DIR} -type f -name 'globals.*.sql' | sort | tail -${FILES}| head -1)
88TIME_NOW=$(date +%s)
89TIME_DUMP=$(date +%s -r $THRESHOLD_GLOBALS_DUMP)
90THRESHOLD_AGE=$(($TIME_NOW - $TIME_DUMP))
91THRESHOLD_MINS=$(($THRESHOLD_AGE / 60))
92THRESHOLD_MINS=$(($THRESHOLD_MINS + 1)) # always round up
93find ${DUMP_DIR} -type f -mmin +${THRESHOLD_MINS} -delete
94
95echo backups completed $(date)
8896
89backup_success97backup_success
9098
=== modified file 'templates/postgres.cron.tmpl'
--- templates/postgres.cron.tmpl 2015-08-10 11:44:18 +0000
+++ templates/postgres.cron.tmpl 2016-01-26 10:45:35 +0000
@@ -4,10 +4,10 @@
4# Scheduled logical dump of the primary using pg_dump. We don't dump hot4# Scheduled logical dump of the primary using pg_dump. We don't dump hot
5# standbys, as operations performed on the primary can cause queries on5# standbys, as operations performed on the primary can cause queries on
6# the standby to be aborted.6# the standby to be aborted.
7{{backup_schedule}} postgres {{scripts_dir}}/pg_backup_job {{backup_retention_count}}7{{backup_schedule}} postgres run-one {{scripts_dir}}/pg_backup_job {{backup_retention_count}}
8{% endif -%}8{% endif -%}
99
10{% if is_master and wal_e_enabled and wal_e_backup_schedule -%}10{% if is_master and wal_e_enabled and wal_e_backup_schedule -%}
11# Scheduled filesystem backup of the primary using wal-e.11# Scheduled filesystem backup of the primary using wal-e.
12{{wal_e_backup_schedule}} postgres {{wal_e_backup_command}} && {{wal_e_prune_command}}12{{wal_e_backup_schedule}} postgres run-one {{wal_e_backup_command}} && {{wal_e_prune_command}}
13{% endif -%}13{% endif -%}
1414
=== modified file 'tests/test_postgresql.py'
--- tests/test_postgresql.py 2015-08-25 13:50:07 +0000
+++ tests/test_postgresql.py 2016-01-26 10:45:35 +0000
@@ -137,7 +137,7 @@
137 def test_packages(self, version):137 def test_packages(self, version):
138 version.return_value = '9.9'138 version.return_value = '9.9'
139 expected = set(['postgresql-9.9', 'postgresql-common',139 expected = set(['postgresql-9.9', 'postgresql-common',
140 'postgresql-client-common',140 'postgresql-client-common', 'run-one',
141 'postgresql-contrib-9.9', 'postgresql-client-9.9'])141 'postgresql-contrib-9.9', 'postgresql-client-9.9'])
142 self.assertSetEqual(postgresql.packages(), expected)142 self.assertSetEqual(postgresql.packages(), expected)
143143

Subscribers

People subscribed via source and target branches

to all changes: