Created
December 9, 2022 10:19
-
-
Save efelon/22069095aa97a659c1af027599b2b906 to your computer and use it in GitHub Desktop.
Very simple logrotate like script to create 8 backups before deletion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
readonly backlevel=8 | |
backshift() { | |
local target="${1:?}" | |
local cdown=$((backlevel+1)) | |
local work= | |
# No shift if there is no input | |
[[ ! -e "${target}" ]] && return 1 | |
while ((--cdown)) ; do | |
work="${target}.${cdown}" | |
[[ -e "${work}" ]] || continue | |
case "${cdown}" in | |
"${backlevel}") | |
rm -f "${work}" ;; | |
*) | |
mv "${work}" "${target}.$((cdown+1))" ;; | |
esac | |
done | |
mv "${target}" "${target}.1" | |
} | |
backshift "${1:?}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment