Rsync is a Free, Open Source, and Minimalistic tool which enables you to copy and paste all files with all their attributes. It is fast and quite versatile. You can use rsync to back up and restore your computer. It is better than the basic copy command cp
because it provides many more options than those by cp
. It is the best backup tool since it allows you to include/exclude the directories you want. Other backup tools don’t let this and thus increase the backup size. They are also very slow.
In this article, I will give you two commands for backup and restore. Then I will guide you on how you can create a simple script and cronjob for periodic backup.
Table of Contents
- How to backup your Linux Computer using Rsync
- How to restore your Linux computer using Rsync
- Creating script for backup and restore using rsync in Linux
- Creating a cron/anacron job for periodic backup using rsync
- Conclusion
How to backup your Linux Computer using Rsync
The command I use is the following:
sudo rsync --archive --acls --xattrs --hard-links --verbose --delete --log-file=/rsync.log --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/swapfile","/home/*/.gvfs","/var/lib/dhcpcd/*"} / "<backup_destination>"
📝 Note: you can save the log file in /var/log
.
here,
<backup_destination>
: location where you want your backups to be saved. I will recommend you use a separate drive. Ex – /mnt/crucial
--delete
: means files deleted on the source (in above command, it is /
) are to be deleted in the destination as well.--verbose
: verbose output.--archive
: copy all directories recursively; preserve almost everything (ex – permissions, symlinks, times, etc.) related to files.--acls
: preserve ACLs.--xattrs
: preserve eXtended attributes of a file.--partial
: by default, rsync deletes partially transferred files, but this option says not to delete but instead transfer the remaining part.--hard-links
: preserve hard links but uses more memory.--exclude={"/dev/*","/proc/*","...}
: becomes --exclude="/dev/*" --exclude="/proc/*" ...
. This is only available in bash and zsh shells. It is used to exclude everything in the given directories. But the directories themselves are created. You can exclude other directories as well such as "/home/ajay/.cache/*"
--sparse
: handles any sparse files, such as virtual disks, Docker images, and similar efficiently.--numeric-ids
: rsync transfers numeric ids of users and groups instead of usernames and group names.
How to restore your Linux computer using Rsync
To restore your Computer from the backup you just created, use the same rsync backup command but with the source and destination interchanged. But be careful with /
after a directory name.
Steps to restore your computer:
- Create a Live Boot Media.
- Boot into the Live USB.
- Mount all drives in your Computer using commands
lsblk
andmount <source> <destination>
mount /dev/"$from" /mnt/fromdir
mount /dev/"$to" /mnt/todir
- Now, execute the following rsync command:
rsync --archive --acls --xattrs --hard-links --verbose --log-file=/rsync.log --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/swapfile","/home/*/.gvfs","/var/lib/dhcpcd/*"} /mnt/fromdir/<location_of_your_backup>/ /mnt/todir
here, <location_of_your_backup>
is your backup location in the backup drive. For example, if you are backing up your computer on a hard disk. The hard disk has many folders. One is PC_backup
folder used to backup your PC. Then the <location_of_your_backup>
will be PC_backup
.
That’s all.
Creating script for backup and restore using rsync in Linux
Learn here about what is a shell script and how to create one.
The following is the script I use. I have restored my PC twice using this.
#!/bin/bash
set -o errexit # exit on error
set -o pipefail # fail the pipe if any of its command fails
set -o nounset # if any variable is undefined exit
backup(){
backup_destination="/mnt/VHD"
# `SOURCE/ means` only SOURCE's content will be transferred.
# `DESTINATION/ and DESTINATION` both are same for rsync
sudo rsync --archive --acls --xattrs --hard-links --verbose --log-file=/rsync.log --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/swapfile","/home/*/.gvfs","/var/lib/dhcpcd/*","/Anki2/*","/home/ajay/.cache/*"} / "$backup_destination"
}
# no need to use sudo because most of the time you will be executing it as root
restore(){
# users will answer the upcoming questions based on lsblk's output
lsblk
echo -e "where do you want to restore from? (ex- sda1, nvme1n1p1,) "
read -r from
echo -e "your backup location in the above source with no trailing forward slash? (ex- backup, and backup/archlinux) "
read -r location
echo -e "where do you want to restore to? (ex- sdb1) "
read -r to
# make directories otherwise error
mkdir /mnt/fromdir
mkdir /mnt/todir
# umount in case you have mounted the source/destination at somewhere else.
umount --lazy /dev/"$from" || :
umount --lazy /dev/"$to" || :
# mounting the source and destination
mount /dev/"$from" /mnt/fromdir
mount /dev/"$to" /mnt/todir
rsync --archive --acls --xattrs --hard-links --verbose --log-file=/rsync.log --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/swapfile","/home/*/.gvfs","/var/lib/dhcpcd/*","/Anki2/*","/home/ajay/.cache/*"} /mnt/fromdir/"$location"/ /mnt/todir
umount --lazy /mnt/"$from"
umount --lazy /mnt/"$to"
}
help_page(){
cat << document
-b [b]ackup
-r [r]estore
document
}
main(){
case "$1" in
"-b") backup ;;
"-r") restore ;;
"-h") help_page ;;
*) help_page && exit 1 ;;
esac
}
main "$1"
Creating a cron/anacron job for periodic backup using rsync
If you don’t know how cron works, look at my article cronjob in Linux.
Rsync backup using crontab -e
To backup using cron, execute the command sudo EDITOR=nvim crontab -e
.
Append a line:
30 23 * * TUE,FRI /path/to/rsync.sh -b
This will backup your computer at 23:30 on Tuesday and Friday. Change the day and time if you wish.
Rsync backup using /etc/crontab
The same can be done using /etc/crontab
as well:
30 23 * * TUE,FRI root /path/to/rsync.sh -b
Rsync backup using anacrontab
Using anacron is the best method since it does not lose the backup in case your computer is shut down when the backup is scheduled.
For this append the following line in the /etc/anacrontab
3 0 backup /path/to/rsync.sh -b
This will backup your computer every third day.
Conclusion
That was all. The same method can be used to backup and restore any directory as well. You can modify the script by adding a notification using dunstify/notify-send or email command in case the backup is interrupted (using the trap
command). Use the comment section if you have any questions/suggestions.
Pingback: How to send notifications in linux using dunstify/notify-send | SmartTech101