Skip to content

Commit 9ab886d

Browse files
author
Thomas Kishel
authored
Merge pull request #36 from m0dular/shell_refactor
Refactor shell scripts
2 parents aa688d1 + 8122a97 commit 9ab886d

File tree

2 files changed

+90
-66
lines changed

2 files changed

+90
-66
lines changed
Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,85 @@
11
#!/bin/bash
22

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+
}
77

8-
while [[ $# -gt 0 ]]; do
9-
arg="$1"
10-
case $arg in
8+
while [[ $1 ]]; do
9+
case "$1" in
1110
-t)
12-
BACKUPDIR="$2"
13-
shift; shift
11+
backup_dir="$2"
12+
shift 2
1413
;;
1514
-l)
16-
LOGDIR="$2"
17-
shift; shift
15+
log_dir="$2"
16+
shift 2
1817
;;
1918
-r)
20-
RETENTION="$2"
21-
shift; shift
19+
retention="$2"
20+
shift 2
2221
;;
23-
*)
24-
DATABASES="${DATABASES} $1"
22+
# If given the end of options string, shift it out and break
23+
--)
2524
shift
25+
break
26+
;;
27+
# No need to shift if we've processed all options
28+
*)
29+
break
2630
;;
2731
esac
2832
done
2933

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
3639

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}"
3944

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"
4149

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)
4356

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 ))
4762

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}"
5267
fi
5368

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
5576

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)"
5778

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"
6381
exit 1
64-
fi
82+
}
83+
84+
echo "Completed dump of database: ${db}"
6585
done

files/vacuum_full_tables.sh

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
#!/bin/bash
22

3-
if [ "$1" = "" ]; then
4-
echo "Usage: $0 <(facts, catalogs, or other) tables to VACUUM FULL> "
5-
exit
6-
fi
3+
usage() {
4+
cat <<EOF
5+
echo "usage: $0 <table set> [sleep duration]"
6+
where <table set> is one of: facts catalogs other
7+
and [sleep duration] is an optional integer of seconds to sleep. Defaults to 300
8+
EOF
9+
exit 1
10+
}
11+
# Print usage if given 0 arguments
12+
(( $# == 0 )) && usage
713

8-
if [ "$2" = "" ]; then
9-
SLEEP=300
10-
else
11-
SLEEP=$2
12-
fi
14+
sleep_duration="${2:-300}"
1315

14-
# TODO: Is this used in PE 2018 and newer? RE: fact_values
16+
case "$1" in
17+
'facts')
18+
vacuum_tables=("'facts'" "'factsets'" "'fact_paths'" "'fact_values'")
19+
;;
20+
'catalogs')
21+
vacuum_tables=("'catalogs'" "'catalog_resources'" "'edges'" "'certnames'")
22+
;;
23+
'other')
24+
vacuum_tables=("'producers'" "'resource_params'" "'resource_params_cache'")
25+
;;
26+
*)
27+
usage
28+
esac
1529

16-
if [ "$1" = 'facts' ]; then
17-
WHERE="'facts', 'factsets', 'fact_paths', 'fact_values'"
18-
elif [ "$1" = 'catalogs' ]; then
19-
WHERE="'catalogs', 'catalog_resources', 'edges', 'certnames'"
20-
elif [ "$1" = 'other' ]; then
21-
WHERE="'producers', 'resource_params', 'resource_params_cache'"
22-
else
23-
echo "Must pass facts, catalogs, or other as first argument"
24-
exit 1
25-
fi
2630

2731
SQL="SELECT t.relname::varchar AS table_name
2832
FROM pg_class t
2933
JOIN pg_namespace n
3034
ON n.oid = t.relnamespace
3135
WHERE t.relkind = 'r'
32-
AND t.relname IN ( $WHERE )"
36+
AND t.relname IN ( $(IFS=,; echo "${vacuum_tables[*]}") )"
37+
38+
tables=($(su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/bin/psql -d pe-puppetdb -c \"$SQL\" --tuples-only"))
3339

34-
for TABLE in $(su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/bin/psql -d pe-puppetdb -c \"$SQL\" --tuples-only")
35-
do
36-
# echo "$TABLE"
37-
su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/bin/vacuumdb -d pe-puppetdb -t $TABLE --full --analyze"
38-
sleep "$SLEEP"
40+
for table in "${tables[@]}"; do
41+
su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/bin/vacuumdb -d pe-puppetdb -t $table --full --analyze"
42+
sleep "$sleep_duration"
3943
done

0 commit comments

Comments
 (0)