28
28
29
29
# The remaining parameters will be the databases to backup
30
30
databases=(" $@ " )
31
+ # shellcheck disable=SC2128
32
+ # We only care if the array contains any elements
31
33
[[ $databases ]] || {
32
34
echo " Usage: $0 [-t BACKUP_TARGET] [-l LOG_DIRECTORY] [-r retention] <DATABASE> [DATABASE_N ...]"
33
35
exit 1
@@ -39,7 +41,7 @@ log_dir="${log_dir:-/var/log/puppetlabs/pe_databases_backup}"
39
41
retention=" ${retention:- 2} "
40
42
41
43
for db in " ${databases[@]} " ; do
42
- # For each db, redirect all output the the log inside the backup dir
44
+ # For each db, redirect all output to the log inside the backup dir
43
45
exec & > " ${log_dir} /${db} .log"
44
46
echo " Enforcing retention policy of storing only $retention backups for $db "
45
47
@@ -48,13 +50,13 @@ for db in "${databases[@]}"; do
48
50
# Add the filename to the backups array, giving us a sorted list of filenames
49
51
while IFS= read -r -d ' ' line; do
50
52
backups+=(" ${line#* } " )
51
- done < <( stat --printf ' %Y %n\0' ${backup_dir} /${db} _* 2> /dev/null | sort -nz)
53
+ done < <( stat --printf ' %Y %n\0' " ${backup_dir} /${db} _" * 2> /dev/null | sort -nz)
52
54
53
55
# Our array offset will be the number of backups - $retention + 1
54
56
# e.g. if we have 2 existing backups and retention=2, offset will be one
55
57
# We'll delete from element 0 to 1 of the array, leaving one backup.
56
58
# The subsequent backup will leave us with 2 again
57
- offset=$(( ${# backups[@]} - $ retention + 1 ))
59
+ offset=$(( ${# backups[@]} - retention + 1 ))
58
60
59
61
if (( offset > 0 )) ; then
60
62
# Continue if we're retaining more copies of the db than currently exist
0 commit comments