Beginner Bash Backup Script: What Am I Missing?
Asked by
MellowCedar42
I'm fairly new to programming and wrote a Bash script that backs up files to an external drive using rsync and --link-dest. It creates daily point-in-time directories that look like full copies, while unchanged files are hard-linked between backups to save spaceโsimilar to the directory-based approach used by tools such as rsnapshot.
I built it mainly to learn Bash and rsync, but I'd appreciate experienced eyes on the design, safety, portability, and edge cases. I'm especially interested in anything that could cause data loss or make the backups unreliable. The script also creates an incremental SHA-256 manifest for each backup so the files can be checked for silent corruption later without re-hashing everything during every run.
Answers
One terminology point: these are directory-based rsync snapshots, not filesystem snapshots in the LVM, Btrfs, VSS, or storage-array sense. Those systems provide copy-on-write behavior and can help create crash-consistent views of live data. It would be worth clarifying that distinction in the documentation so users know exactly what guarantees the script provides.
Reply to this answer
Make sure temporary files are cleaned up with a trap rather than relying only on an rm command near the end. If the script is interrupted or exits early, the cleanup command may never run, leaving stale files behind. A trap can handle normal exits and signals more reliably.
Reply to this answer
There are established tools that use the same rsync-plus-hard-link design, such as rsnapshot. Your implementation is still worthwhile as a learning project, and the SHA-256 manifests are a useful addition for verifying old backups later. Just compare your script with mature tools to find edge cases you may not have considered.
Reply to this answer
The overall approach is reasonable, but the script would be easier to maintain if you organized it into functions and called a main function at the end. That keeps the execution flow clear and makes individual pieces easier to test. The Google Shell Style Guide is a useful reference for this style.
Reply to this answer
Add your answer
Answers are reviewed before publication.
MellowCedar42: Thatโs fair. Iโm using โsnapshotโ in the same directory-based sense as rsync and hard-link backup tools, but Iโll explain the distinction more clearly so it isnโt confused with filesystem-level snapshots.