Last active
October 1, 2025 00:51
-
-
Save simonLeary42/00f83633859ffa6bfa5bc6e27447f871 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/bin/bash | |
| set -euo pipefail | |
| trap 's=$?; echo "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR | |
| # usage: backup.bash /path/to/backup/dir num_backups_to_keep | |
| # backup dir must contain a file called 'index' which must contain just a number | |
| # this index number is used to delete older backup files | |
| # if this script fails, the index will be modified so that no backups will be deleted in the future | |
| # to re-enable deletion of old backups, overwrite index back to a number | |
| # recommended usage is with cron jobs of varying frequency | |
| # this way it is easy to keep logs from recently, yesterday, last week, last month, ... | |
| source='/home/simon/minecraft/world' | |
| source_dir="$(dirname "$source")" | |
| source_basename="$(basename "$source")" | |
| backup_dir="$(realpath "$1")" | |
| search='fabric-server-mc.1.19.2-loader.0.14.25-launcher.1.1.0.jar' | |
| num_backups_to_keep="$2" | |
| index_path="$backup_dir/index" | |
| if ! pgrep -f "$search" >/dev/null; then | |
| echo "server not running, nothing doing..." | |
| exit 0 | |
| fi | |
| most_recent_backup_index="$(cat "$index_path")" | |
| if [[ "$most_recent_backup_index" =~ ^[0-9]+$ ]]; then | |
| backup_index="$(($most_recent_backup_index + 1))" | |
| echo "$backup_index" > "$backup_dir/index" | |
| else | |
| echo "index file '$index_path' does not contain a number" | |
| exit 1 | |
| fi | |
| backup_path="$(realpath "$backup_dir/$backup_index.tar.gz")" | |
| (cd "$source_dir" && tar --ignore-failed-read --create --gzip --file="$backup_path" "$source_basename") | |
| delete_backup_index="$(($backup_index - $num_backups_to_keep))" | |
| if [ $delete_backup_index -ge 1 ]; then | |
| delete_backup_path="$backup_dir/$delete_backup_index.tar.gz" | |
| if [ -f "$delete_backup_path" ]; then | |
| rm "$delete_backup_path" | |
| else | |
| echo "warning: old backup '$delete_backup_path' supposed to be deleted but it does not exist" | |
| fi | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment