Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

BashintermediatedevopsAI generated

Automated Backup Rotation Tool

Build a Bash backup tool that archives chosen folders with tar and gzip, uploads the archive to a remote server over SSH, and keeps a sensible set of old backups: for example the last 7 daily copies, 4 weekly and 6 monthly, deleting the rest automatically. Every upload is verified with a checksum before any old backup is removed, so you never end up with fewer good backups than before. A restore script lists the available backups and restores the one you choose. It's meant to run every night from cron and report clearly when something goes wrong. You'll learn tar, gzip and checksums, scp and ssh with keys, date calculations in shell, retention policies, and writing scripts you can trust with your data.

Estimate
~10.5h
Steps
7
Completed by
0
Proposed by
codeseed.app

tar · gzip · scp · ssh

Project roadmap

  1. 01

    Create the archive

    ~1.5h

    Write backup.sh with set -euo pipefail, reading settings from a config file: source folders, exclude patterns, the remote host and folder, and the retention counts. Create a timestamped archive (hostname-2026-03-15T0200.tar.gz) with tar and gzip, using --exclude-from, and write a SHA-256 checksum file next to it. Stop with a clear error when a source folder is missing. Done when: running the script creates the archive and its checksum.

  2. 02

    Upload over SSH

    ~1.5h

    Upload the archive and checksum with scp (or rsync over ssh) using a key and -o BatchMode=yes, to a temporary name first, then rename it on the remote host once complete. Retry failed transfers up to 3 times with increasing waits. Stop and report when the remote host can't be reached. Done when: the archive appears on the remote host under its final name.

  3. 03

    Verify the upload

    ~1h

    After uploading, run sha256sum on the remote copy over ssh and compare it with the local checksum. Mark the backup good only when they match; otherwise delete the broken remote copy and fail. Optionally also list the archive's contents with tar -tzf to check it isn't corrupted. Done when: a deliberately corrupted upload is detected and the run fails.

  4. 04

    Apply the retention policy

    ~2h

    List the remote backups, parse the dates from their names, and decide which to keep: the newest in each of the last 7 days, the last 4 weeks and the last 6 months. Delete the rest only after the new backup has been verified, and never delete the newest backup. Add --dry-run to print what would be deleted. Done when: a test with backups spread over a year keeps exactly the right ones.

  5. 05

    Restore backups

    ~1.5h

    Write restore.sh that lists available backups with their date and size, lets you choose one, downloads it, verifies its checksum, and extracts it into a target folder (refusing to extract into a non-empty folder unless --force is given). Support restoring a single file or folder from the archive. Done when: a backup can be restored and matches the original files.

  6. 06

    Run from cron and report

    ~1h

    Log every run with timestamps to a log file, use a lock file (flock) so runs never overlap, clean up local temporary archives with a trap, and exit with a non-zero code on any failure. Add an example crontab line and optional email or webhook notification on failure to README.md. Done when: the tool runs unattended, and failures are clearly reported.

  7. 07

    Test it

    ~2h

    Test with a local SSH server in Docker (or ssh to localhost): full backup and restore, a failed upload, a corrupted upload, retention with fake backup names across a year (test the date logic separately with bats-core), and dry runs. Run shellcheck on both scripts. Done when: the tests pass, and shellcheck reports no warnings.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~10.5h · 7 steps

Tech stack

targzipscpssh