I'm looking for how to automatically backup a user's home directory in CentOs 7 to a remote host or NAS or just to ~/.snapshot. In some Linux setups, I have seen a .snapshot folder in the user's home directory (~/.snapshot/) that holds hourly, nightly, and weekly backups of their home directory (ie ~/.snapshot/weekly1 for a copy of what was in the user's home directory 1 week ago).
The /home/username/.snapshot/ directory would be read-only by the user. It's not a backup for the purpose of guarding against hardware failure. It's just nice to have the ability to recover a file from yesterday or this morning if you don't like the changes that have been made.
I have seen several related posts on stack overflow, but so far, I haven't seen a guide that explains the complete workflow.
This is what I know so far:
- Use
rsync
to copy the contents of a given folder to the remote host, NAS, or (~/.snapshot/hourly0) - Create a shell script to execute the
rsync
command
#!/bin/bash
sudo rsync -av --progress --delete --log-file=/home/username/$(date +%Y%m%d)_rsync.log --exclude "/home/username/.snapshot" /home/username/ /home/username/.snapshot/hourly1
- Change the permissions on the script to make it executable
sudo chmod +x /home/username/myscript.sh
Use
crontab
to schedule the rsync command at the desired backup intervalSomehow move hourly0 to hourly1 before running the scheduled hourly rsync
Delete the oldest backup once rsync completes successfully
Are there any guides that cover how to do this?
I don't understand how to automatically rename the folders as time goes on (ie weekly1 to weekly2), or how to delete "week10" if I decide to only keep weeks up to 9. Is this another cron
job?
Update: After some more Googling, I've discovered that NetApp creates the snapshot folders. I just don't currently have a NetApp NAS. https://library.netapp.com/ecmdocs/ECMP1635994/html/GUID-FB79BB68-B88D-4212-A401-9694296BECCA.html
/home
into that directory doesn't seem like an actual backup...