Feedback on my first Bash backup script

0
1
Asked By MellowCactus47 On

I wrote my first Bash script to back up a Minecraft server running on an old laptop. It creates a compressed tar archive, optionally keeps a configurable number of local archives, copies the archive to a Google Drive remote using rclone, and removes older local and remote backups. I plan to run it once a day with cron. I'd appreciate advice on shell-scripting conventions, reliability, backup retention, and anything that could cause data loss or unexpected behavior.

4 Answers

Answered By RiverStone22 On

Sorting filenames is only a reliable age check because your timestamp happens to sort chronologically. A more robust approach is to use file modification times, or let a tool handle retention based on dates. Also consider using rsync for the data transfer; it can avoid recopying unchanged files. Create required directories with mkdir -p rather than relying on them already existing. Decide carefully whether you want snapshots, incremental backups, or a mirror: deleting destination files that disappeared from the source saves space but also removes your ability to recover accidentally deleted files.

MellowCactus47 -

That makes sense. I hadn’t considered how mirroring and snapshot retention affect recovering deleted files.

Answered By QuietMaple8 On

This is a solid first script. Add a Bash shebang such as #!/usr/bin/env bash and run the script through ShellCheck; it will catch quite a few quoting and syntax issues. Consider using functions to separate creating, uploading, and pruning backups. You can also use variables such as $HOME instead of requiring the user to edit the full home-directory path. For timestamps, an ISO-style format like date -u +%Y-%m-%dT%H:%M:%SZ is less ambiguous and sorts chronologically.

MellowCactus47 -

Thanks, I’ll definitely try ShellCheck and reorganize the script into functions.

Answered By SilverLantern39 On

The most important issue before putting this in cron is error handling. If tar or rclone fails, the script may continue pruning old backups and still print that the backup completed. Enable strict handling with something like set -Eeuo pipefail, check command results, and only remove old archives after the new archive exists, is non-empty, and the remote upload has been verified. Also remember that cron can fail silently, so arrange some kind of notification or periodic check when the expected completion message is missing.

Answered By BluePebble61 On

Keeping both local and remote copies can be useful, but it does add complexity. If the local archive is mainly for quick restores, document that purpose; otherwise, copying the source directly to the remote may be simpler. Whatever method you choose, test restoring files from both locations instead of only checking that the backup command completed.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.