This article discusses how to keep the most recent 7 backups of a Linux system. When we say “we want x days of backups retention” we’re simply stating the number of days we can go back to retrieve files from the backups.
Here is an example:
/bin/tar -czf /media/backups/backup-$(date +%w).tgz /etc /var/log /var/www /home
The example above will backup the directories “/etc /var/log /var/www /home” and tar/zip them to the “/media/backups” directory. I specifically used “/media/backups” to show that backups should be stored on a different system – not the same server as where the production data lives. The “$(date +%w)” part is the key for retention. The “%w” means the day of the week from 0=Sunday to 6=Saturday. So the backups will look like this:
/media/backups/backup-0.tgz = Sunday /media/backups/backup-1.tgz = Monday /media/backups/backup-2.tgz = Tuesday /media/backups/backup-3.tgz = Wednesday /media/backups/backup-4.tgz = Thursday /media/backups/backup-5.tgz = Friday /media/backups/backup-6.tgz = Saturday
Each day the backups run, Tar will overwrite the previous file with the same name. Ie, Wednesday backups are always called “/media/backups/backup-3.tgz”.
Challenge: Try doing the same with Rsync. Ie, have Rsync create directories named by the day of the week on the backup location such that each Wednesday (for example) the Wednesday backup is updated rather than started from scratch. This would greatly improve performance but increase disk usage on the backup file-system.
/media/backup/Wednesday/ = all files from Wednesday