Skip to content

Testing — Backup Script

Test every code path: backup, list, verify, restore, and rotation.

Basic Backup

cd ~/projects/backup-script
bash backup.sh --source test_source --dest /tmp/test_backups --keep 3

Expected output:

[2024-01-15 09:00:00] [INFO ] === Backup started → /tmp/test_backups/2024-01-15_090000 === [2024-01-15 09:00:00] [INFO ] rsync: test_source → /tmp/test_backups/2024-01-15_090000/... [2024-01-15 09:00:00] [INFO ] rsync OK: test_source [2024-01-15 09:00:00] [INFO ] === Backup complete. Failed sources: 0 === [2024-01-15 09:00:00] [INFO ] Rotating backups — keeping last 3 [2024-01-15 09:00:00] [INFO ] No rotation needed (1/3 slots used)

Verify the backup exists:

find /tmp/test_backups -type f | sort
/tmp/test_backups/2024-01-15_090000/.../config.conf /tmp/test_backups/2024-01-15_090000/.../file1.txt /tmp/test_backups/2024-01-15_090000/.../subdir/nested.txt /tmp/test_backups/backup.log

Test Incremental Backup

Modify a file, run backup again, and confirm rsync only transfers the changed file:

echo "Modified line" >> test_source/file1.txt
bash backup.sh --source test_source --dest /tmp/test_backups --keep 3

The rsync output should show only file1.txt was transferred (not all files).

Test Compressed Backup

bash backup.sh --source test_source --dest /tmp/test_backups --keep 3 --compress
ls /tmp/test_backups/*.tar.gz
/tmp/test_backups/2024-01-15_090001.tar.gz

Test List

bash backup.sh list --dest /tmp/test_backups
Available backups in /tmp/test_backups: 1 2024-01-15_090001 (4.0K) ← newest 2 2024-01-15_090000 (4.0K)

Test Verify

bash backup.sh verify 1 --dest /tmp/test_backups
Verifying backup: 2024-01-15_090001 Checksum OK

Corrupt the archive and verify detection:

echo "corrupted" >> /tmp/test_backups/2024-01-15_090001.tar.gz
bash backup.sh verify 1 --dest /tmp/test_backups
echo "Exit: $?"
/tmp/test_backups/2024-01-15_090001.tar.gz: FAILED sha256sum: WARNING: 1 computed checksum did NOT match Checksum FAILED Exit: 1

Test Rotation

Create 5 backups and verify only the last 3 remain:

for i in $(seq 1 5); do
    bash backup.sh --source test_source --dest /tmp/test_backups --keep 3
    sleep 1   # ensure different timestamps
done

ls /tmp/test_backups/

Should show exactly 3 timestamped directories (the 2 oldest removed) plus backup.log.

Test Missing Source

bash backup.sh --source /nonexistent --dest /tmp/test_backups --keep 3
echo "Exit: $?"
[2024-01-15 09:00:05] [ERROR] Source not found: /nonexistent [2024-01-15 09:00:05] [INFO ] === Backup complete. Failed sources: 1 === Exit: 1

Checklist

□ Backup creates timestamped directory with source contents □ Second backup only syncs changed files (rsync --verbose shows 1 file) □ --compress creates .tar.gz and .sha256 checksum file □ list shows backups newest-first with sizes □ verify detects checksum mismatch □ Rotation removes oldest when count exceeds --keep □ Missing source reports error but doesn't crash script □ Exit code 0 when all sources succeed, 1 if any fail □ shellcheck backup.sh returns 0 warnings

implementation | enhancements