Backing up ~/Sync with tar
Need to backup ~/Sync directory. Since it's used heavily for trivial and important things I want to run weekly, monthly, and yearly backups. Weekly so a recent backup is never far. Monthly so I can delete the weekly's once in a while. Yearly so I have snapshots to look back on over time.
Basics of a tar backup command:
tar -czf /mnt/data-write/Backups/Sync/Sync-test.tar.gz -C ~/ Sync
Took a bit to figure out how to get this so that it can run anywhere
(will be in crontab soon). If you just specify the input directory
like this you'll end up with the entire path in the archive. To get around
this use -C to specify which directory to perform the operation from.
After that the directory to be tar'd needs to be specified relative
to that. This makes the syntax slightly awkward and not available for
auto complete.
Next need to extract date information from date to name the backups:
date "+%V" # ISO week number date "+%Y" # Year date "+%m" # Padded month date "+%F" # Date in the format 2026-03-19
All backups should start with the file name, then the date so that they are all in order, then an identifier for week, month, or year so they can be singled out if necessary in the future.
tar -czf /mnt/data-write/Backups/Sync/Sync-$(date "+%F")-weekly-$(date "+%V").tar.gz -C ~/ Sync tar -czf /mnt/data-write/Backups/Sync/Sync-$(date "+%F")-monthly-$(date "+%m").tar.gz -C ~/ Sync tar -czf /mnt/data-write/Backups/Sync/Sync-$(date "+%F")-yearly-$(date "+%Y").tar.gz -C ~/ Sync
Setup the crontab (crontab -e):
# Weekly: Midnight on the dot every Sunday 0 0 * * 0 tar -czf /mnt/data-write/Backups/Sync/Sync-$(date "+%F")-weekly-$(date "+%V").tar.gz -C ~/ Sync # Monthly: 15 min after midnight every 1st 15 0 1 * * tar -czf /mnt/data-write/Backups/Sync/Sync-$(date "+%F")-monthly-$(date "+%m").tar.gz -C ~/ Sync # Yearly: 30 min after midnight every January 1st 30 0 1 1 * tar -czf /mnt/data-write/Backups/Sync/Sync-$(date "+%F")-yearly-$(date "+%Y").tar.gz -C ~/ Sync
Set reminders to check next week, next month, next year.