Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the # of backups at each level configurable #31

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
26f2144
Make the # of backups at each level configurable
assistcontrol Jun 10, 2019
732dbb7
Provide better naming with backuptarget=/
assistcontrol Jun 11, 2019
e2353d5
Remove all /'s from archive names
assistcontrol Jun 11, 2019
123b470
Remove dangling debugging variable
assistcontrol Jun 11, 2019
481ef19
Disable shfmt in Travis
alexjurkiewicz Jun 11, 2019
a4eba96
Fix postbackup log string (fixes #25) (#34)
assistcontrol Jun 14, 2019
635a2b0
Accept configuration path as argument (fixes #27) (#33)
assistcontrol Jun 14, 2019
e31c0c8
Make the # of backups at each level configurable
assistcontrol Jun 10, 2019
a7971a4
Provide better naming with backuptarget=/
assistcontrol Jun 11, 2019
9171b4c
Remove all /'s from archive names
assistcontrol Jun 11, 2019
c0c01f3
Remove dangling debugging variable
assistcontrol Jun 11, 2019
c2d6898
Merge branch 'configurable_limits' of github.com:alexjurkiewicz/acts …
assistcontrol Jun 14, 2019
005d0cb
Remove the archive-naming changes from this topic branch
assistcontrol Jun 14, 2019
65ae8a3
Make sure $acts_conf is always defined
assistcontrol Jun 14, 2019
800ed3b
Fix limit sanity checks
assistcontrol Jun 14, 2019
81dcecb
This is an extended regex
assistcontrol Jun 14, 2019
cdef28d
Fix debug message
assistcontrol Jun 14, 2019
b74549b
Off-by-one error deleted one too many backups
assistcontrol Jun 14, 2019
484fec0
Shellcheck can safely ignore the user-specified acts.conf path
assistcontrol Jun 14, 2019
a45f14e
Compute the number of backups once
assistcontrol Jun 14, 2019
a4315cc
dailybackups is more expressive than dailymax
assistcontrol Jun 14, 2019
a4e147b
Improve grammar
assistcontrol Jun 14, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ design goals:
- Portable, small, code footprint.

One Tarsnap archive is created per-target per-run. 31 daily, 12 monthly,
and indefinite yearly backups are kept.
and indefinite yearly backups are kept by default.

Download
--------
Expand All @@ -40,7 +40,7 @@ Notes on behaviour:
- If no monthly backup for the current month exists, create a
monthly backup.
- Otherwise, create a daily backup.
- Archives are deleted using the following logic:
- Archives are deleted using the following logic by default:
- If any backups failed, delete nothing.
- Keep the most recent 31 daily backups, and delete any older
ones.
Expand Down
70 changes: 36 additions & 34 deletions acts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ postbackupscript="${postbackupscript=}"
tarsnap="${tarsnap:=tarsnap}"
tarsnapbackupoptions="${tarsnapbackupoptions=}"
lockfile="${lockfile=/var/run/acts}"
dailymax="${dailymax:=31}"
monthlymax="${monthlymax:=12}"
yearlymax="${yearlymax:=0}"
LANG="${LANG=en_US.UTF-8}"
export LANG

Expand All @@ -102,6 +105,10 @@ else
die "config-error message=\"invalid \$uselocaltime in acts.conf; valid options are 0 (for UTC) or 1 (for local time).\""
fi

[ $(( dailymax )) = "$dailymax" ] || die "config-error message=\"Invalid \$dailymax; must be a number.\""
[ $(( monthlymax )) = "$dailymax" ] || die "config-error message=\"Invalid \$monthlymax; must be a number.\""
[ $(( yearlymax )) = "$dailymax" ] || die "config-error message=\"Invalid \$yearlymax; must be a number.\""

# Create the lock. mkdir is atomic test/set, so use that instead of the typical test -f/touch combo
if [ -d "$lockfile" ]; then
die "acts-error message=\"$lockfile exists referencing PID $(cat "$lockfile/pid" 2>/dev/null || echo "<none>"). Hint: If acts is not already running, rm -rf $lockfile.\""
Expand Down Expand Up @@ -146,10 +153,10 @@ backuprc=0 # Notice any failed backups
for dir in $backuptargets; do
log_debug "message=\"Starting backup of $dir\""
archive_starttime=$(date +%s)
nicedirname=$(echo "$dir" | tr -d '/')
nicedirname=${dir:+-${dir#/}} # /foo -> -foo, / -> ''
alexjurkiewicz marked this conversation as resolved.
Show resolved Hide resolved
# Determine the archive type to create
if echo "$archives" | grep -E -q "^$hostname-yearly-$year-.+-$nicedirname$"; then
if echo "$archives" | grep -E -q "^$hostname-monthly-$year-$month-.+-$nicedirname$"; then
if echo "$archives" | grep -E -q "^$hostname-yearly-$year-.+$nicedirname$"; then
if echo "$archives" | grep -E -q "^$hostname-monthly-$year-$month-.+$nicedirname$"; then
# There's a yearly and monthly backup already
archivetype="daily"
else
Expand All @@ -161,7 +168,7 @@ for dir in $backuptargets; do
archivetype="yearly"
fi
log_verbose "archive-type type=$archivetype"
archivename="$hostname-$archivetype-$today-$nicedirname"
archivename="$hostname-$archivetype-$today$nicedirname"
log_verbose "backup-start type=$archivetype dir=/$dir name=$archivename"
# Uncontrolled expansion is bad, but we have little choice. See https://github.com/koalaman/shellcheck/wiki/Sc2086
# shellcheck disable=SC2086
Expand All @@ -180,40 +187,35 @@ if [ "$backuprc" != "0" ]; then
die "acts-tarsnap-error One of the backups failed -- not deleting old backups"
fi

for dir in $backuptargets; do
log_debug "message=\"Checking $dir for old backups to delete\""
nicedirname=$(echo "$dir" | tr -d '/')
# We don't delete any yearly backups

# We keep 12 monthly backups
monthlybackups=$(echo "$archives" | grep "$hostname-monthly-.+-$nicedirname$" | sort -rn)
if [ "$(echo "$monthlybackups" | wc -l)" -gt 12 ]; then
log_debug 'message="More than 12 monthly backups, deleting the oldest"'
echo "$monthlybackups" | tail -n +13 | while read -r archiveprefixtodel; do
log_verbose "message=\"Deleting backup prefix $archiveprefixtodel*\""
echo "$archives" | grep -E "^$archiveprefixtodel" | while read -r archivetodel; do
log_debug "message=\"Deleting backup $archivetodel\""
$tarsnap -d -f "$archivetodel"
prune_backups() {
backuplevel=$1 max=$2

if [ "$max" -gt 0 ]; then
backupslist=$(echo "$archives" | grep "$hostname-$backuplevel-.+$nicedirname$" | sort -rn)
if [ "$(echo "$backupslist" | wc -l)" -gt "$max" ]; then
log_debug "message=\"More than $max yearly backups, deleting the oldest\""
echo "$backupslist" | tail -n +"$max" | while read -r archiveprefixtodel; do
log_verbose "message=\"Deleting backup prefix $archiveprefixtodel*\""
echo "$archives" | grep -E "^$archiveprefixtodel" | while read -r archivetodel; do
log_debug "message=\"Deleting backup $archivetodel\""
$tarsnap -d -f "$archivetodel"
done
done
done
else
log_debug "message=\"Found $(echo "$backupslist" | wc -l) $backuplevel backups, not deleting\""
fi
else
log_debug "message=\"Found $(echo "$monthlybackups" | wc -l) monthly backups, not deleting\""
log_debug "message=\"Keeping all $backuplevel backups indefinitely; not deleting any\""
fi
}

# We keep 31 daily backups
dailybackups=$(echo "$archives" | grep "$hostname-daily-.+-$nicedirname$" | sort -rn)
if [ "$(echo "$dailybackups" | wc -l)" -gt 31 ]; then
log_debug "message=\"More than 30 daily backups, deleting the oldest\""
echo "$dailybackups" | tail -n +32 | while read -r archiveprefixtodel; do
log_verbose "message=\"Deleting backup prefix $archiveprefixtodel*\""
echo "$archives" | grep -E "^$archiveprefixtodel" | while read -r archivetodel; do
log_debug "message=\"Deleting backup $archivetodel\""
$tarsnap -d -f "$archivetodel"
done
done
else
log_debug "message=\"Found $(echo "$dailybackups" | wc -l) daily backups, not deleting any\""
fi
for dir in $backuptargets; do
log_debug "message=\"Checking $dir for old backups to delete\""
nicedirname=${dir:+-${dir#/}} # /foo -> -foo, / -> ''
alexjurkiewicz marked this conversation as resolved.
Show resolved Hide resolved

prune_backups yearly $yearlymax
prune_backups monthly $monthlymax
prune_backups daily $dailymax
done

# Run the post-backup script
Expand Down
7 changes: 7 additions & 0 deletions acts.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,10 @@ verbose=1
# Where acts should write its lockfile.
# Default: /var/run/acts
#lockfile=/tmp/acts

# How many backups of each type to keep
# For all limits, 0 means keep indefinitely
# Defaults: 31 daily, 12 monthly, and yearly indefinitely
# dailymax=31
# monthlymax=12
# yearlymax=0