|
1 | 1 | #!/bin/bash
|
2 | 2 |
|
3 |
| -PG_VERSION=$(/usr/local/bin/facter -p pe_postgresql_info.installed_server_version) |
4 |
| -BACKUPDIR=/opt/puppetlabs/server/data/postgresql/$PG_VERSION/backups |
5 |
| -LOGDIR=/var/log/puppetlabs/pe_databases_backup |
6 |
| -RETENTION=2 |
| 3 | +usage() { |
| 4 | + echo "usage: $0 [-t BACKUP_TARGET] [-l LOG_DIRECTORY] [-r retention] <DATABASE> [DATABASE_N ...]" |
| 5 | + exit 1 |
| 6 | +} |
7 | 7 |
|
8 |
| -while [[ $# -gt 0 ]]; do |
9 |
| - arg="$1" |
10 |
| - case $arg in |
| 8 | +while [[ $1 ]]; do |
| 9 | + case "$1" in |
11 | 10 | -t)
|
12 |
| - BACKUPDIR="$2" |
13 |
| - shift; shift |
| 11 | + backup_dir="$2" |
| 12 | + shift 2 |
14 | 13 | ;;
|
15 | 14 | -l)
|
16 |
| - LOGDIR="$2" |
17 |
| - shift; shift |
| 15 | + log_dir="$2" |
| 16 | + shift 2 |
18 | 17 | ;;
|
19 | 18 | -r)
|
20 |
| - RETENTION="$2" |
21 |
| - shift; shift |
| 19 | + retention="$2" |
| 20 | + shift 2 |
22 | 21 | ;;
|
23 |
| - *) |
24 |
| - DATABASES="${DATABASES} $1" |
| 22 | + # If given the end of options string, shift it out and break |
| 23 | + --) |
25 | 24 | shift
|
| 25 | + break |
| 26 | + ;; |
| 27 | + # No need to shift if we've processed all options |
| 28 | + *) |
| 29 | + break |
26 | 30 | ;;
|
27 | 31 | esac
|
28 | 32 | done
|
29 | 33 |
|
30 |
| -if [[ -z "${DATABASES}" ]]; then |
31 |
| - echo "Usage: $0 [-t BACKUP_TARGET] [-l LOG_DIRECTORY] [-r RETENTION] <DATABASE> [DATABASE_N ...]" |
32 |
| - exit 1 |
33 |
| -fi |
34 |
| - |
35 |
| -RETENTION_ENFORCE=$((RETENTION-1)) |
| 34 | +# The remaining parameters will be the databases to backup |
| 35 | +databases=("$@") |
| 36 | +# shellcheck disable=SC2128 |
| 37 | +# We only care if the array contains any elements |
| 38 | +[[ $databases ]] || usage |
36 | 39 |
|
37 |
| -for db in $DATABASES; do |
38 |
| - echo "Enforcing retention policy of storing only ${RETENTION_ENFORCE} backups for ${db}" >> "${LOGDIR}/${db}.log" 2>&1 |
| 40 | +[[ $pg_version ]] || pg_version="$(/usr/local/bin/facter -p pe_postgresql_info.installed_server_version)" |
| 41 | +backup_dir="${backup_dir:-/opt/puppetlabs/server/data/postgresql/$pg_version/backups}" |
| 42 | +log_dir="${log_dir:-/var/log/puppetlabs/pe_databases_backup}" |
| 43 | +retention="${retention:-2}" |
39 | 44 |
|
40 |
| - ls -1tr ${BACKUPDIR}/${db}_* | head -n -${RETENTION_ENFORCE} | xargs -d '\n' rm -f -- |
| 45 | +for db in "${databases[@]}"; do |
| 46 | + # For each db, redirect all output to the log inside the backup dir |
| 47 | + exec &>"${log_dir}/${db}.log" |
| 48 | + echo "Enforcing retention policy of storing only $retention backups for $db" |
41 | 49 |
|
42 |
| - echo "Starting dump of database: ${db}" >> "${LOGDIR}/${db}.log" 2>&1 |
| 50 | + unset backups |
| 51 | + # Starting inside <(), use stat to print mtime and the filename and pipe to sort |
| 52 | + # Add the filename to the backups array, giving us a sorted list of filenames |
| 53 | + while IFS= read -r -d '' line; do |
| 54 | + backups+=("${line#* }") |
| 55 | + done < <(stat --printf '%Y %n\0' "${backup_dir}/${db}_"* 2>/dev/null | sort -nz) |
43 | 56 |
|
44 |
| - if [ "${db}" == "pe-classifier" ]; then |
45 |
| - # Save space before backing up by clearing unused node_check_ins table. |
46 |
| - /opt/puppetlabs/server/bin/psql -d pe-classifier -c 'TRUNCATE TABLE node_check_ins' >> "${LOGDIR}/${db}.log" 2>&1 |
| 57 | + # Our array offset will be the number of backups - $retention + 1 |
| 58 | + # e.g. if we have 2 existing backups and retention=2, offset will be one |
| 59 | + # We'll delete from element 0 to 1 of the array, leaving one backup. |
| 60 | + # The subsequent backup will leave us with 2 again |
| 61 | + offset=$(( ${#backups[@]} - retention + 1 )) |
47 | 62 |
|
48 |
| - result=$? |
49 |
| - if [ $result != 0 ]; then |
50 |
| - echo "Failed to truncate node_check_ins table" >> "${LOGDIR}/${db}.log" 2>&1 |
51 |
| - fi |
| 63 | + if (( offset > 0 )); then |
| 64 | + # Continue if we're retaining more copies of the db than currently exist |
| 65 | + # This will also be true if no backups currently exist |
| 66 | + rm -f -- "${backups[@]:0:$offset}" |
52 | 67 | fi
|
53 | 68 |
|
54 |
| - DATETIME=$(date +%m_%d_%y_%H_%M) |
| 69 | + echo "Starting dump of database: $db" |
| 70 | + |
| 71 | + if [[ $db == 'pe-classifier' ]]; then |
| 72 | + # Save space before backing up by clearing unused node_check_ins table. |
| 73 | + /opt/puppetlabs/server/bin/psql -d pe-classifier -c 'TRUNCATE TABLE node_check_ins' || \ |
| 74 | + echo "Failed to truncate node_check_ins table" |
| 75 | + fi |
55 | 76 |
|
56 |
| - /opt/puppetlabs/server/bin/pg_dump -Fc "${db}" -f "${BACKUPDIR}/${db}_$DATETIME.bin" >> "${LOGDIR}/${db}.log" 2>&1 |
| 77 | + datetime="$(date +%Y%m%d%S)" |
57 | 78 |
|
58 |
| - result=$? |
59 |
| - if [[ $result -eq 0 ]]; then |
60 |
| - echo "Completed dump of database: ${db}" >> "${LOGDIR}/${db}.log" 2>&1 |
61 |
| - else |
62 |
| - echo "Failed to dump database ${db}. Exit code is: ${result}" >> "${LOGDIR}/${db}.log" 2>&1 |
| 79 | + /opt/puppetlabs/server/bin/pg_dump -Fc "$db" -f "${backup_dir}/${db}_$datetime.bin" || { |
| 80 | + echo "Failed to dump database $db" |
63 | 81 | exit 1
|
64 |
| - fi |
| 82 | + } |
| 83 | + |
| 84 | + echo "Completed dump of database: ${db}" |
65 | 85 | done
|
0 commit comments