{"id":2481,"date":"2023-03-18T00:34:04","date_gmt":"2023-03-17T19:04:04","guid":{"rendered":"https:\/\/smarttech101.com\/?p=2481"},"modified":"2023-08-17T15:02:21","modified_gmt":"2023-08-17T09:32:21","slug":"backup-and-restore-your-computer-using-rsync","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/backup-and-restore-your-computer-using-rsync\/","title":{"rendered":"Backup and Restore Your Computer Using Rsync"},"content":{"rendered":"\n
Rsync<\/a> 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 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.<\/p>\n\n\n\n The command I use is the following:<\/p>\n\n\n\n \ud83d\udcdd Note<\/strong>: you can save the log file in here,<\/p>\n\n\n\n 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 here, That’s all.<\/p>\n\n\n\n Learn here about what is a shell script and how to create one.<\/a><\/p>\n\n\n\n The following is the script I use. I have restored my PC twice using this.<\/p>\n\n\n\n If you don’t know how cron works, look at my article cronjob in Linux<\/a>.<\/p>\n\n\n\n To backup using cron, execute the command This will backup your computer at 23:30 on Tuesday and Friday. Change the day and time if you wish.<\/p>\n\n\n\n The same can be done using cp<\/code><\/a> because it provides many more options than those by
cp<\/code>. 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.<\/p>\n\n\n\n
Table of Contents<\/h2>\n\n\n\n
\n
\n
\n
How to backup your Linux Computer using Rsync<\/h2>\n\n\n\n
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>\"<\/code><\/pre>\n\n\n\n
\/var\/log<\/code>.<\/p>\n\n\n\n
<backup_destination><\/code>: location where you want your backups to be saved. I will recommend you use a separate drive. Ex –
\/mnt\/crucial<\/code>
--delete<\/code>: means files deleted on the source (in above command, it is
\/<\/code>) are to be deleted in the destination as well.
--verbose<\/code>: verbose output.
--archive<\/code>: copy all directories recursively; preserve almost everything (ex – permissions, symlinks, times, etc.) related to files.
--acls<\/code>: preserve ACLs.
--xattrs<\/code>: preserve eXtended attributes of a file.
--partial<\/code>: by default, rsync deletes partially transferred files, but this option says not to delete but instead transfer the remaining part.
--hard-links<\/code>: preserve hard links but uses more memory.
--exclude={\"\/dev\/*\",\"\/proc\/*\",\"...}<\/code>: becomes
--exclude=\"\/dev\/*\" --exclude=\"\/proc\/*\" ...<\/code>. 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\/*\"<\/code>
--sparse<\/code>: handles any sparse files, such as virtual disks, Docker images, and similar efficiently.
--numeric-ids<\/code>: rsync transfers numeric ids of users and groups instead of usernames and group names.<\/p>\n\n\n\n
How to restore your Linux computer using Rsync<\/h2>\n\n\n\n
\/<\/code> after a directory name.<\/p>\n\n\n\n
Steps to restore your computer:<\/h3>\n\n\n\n
\n
lsblk<\/code> and
mount <source> <destination><\/code><\/li>\n<\/ol>\n\n\n\n
mount \/dev\/\"$from\" \/mnt\/fromdir\nmount \/dev\/\"$to\" \/mnt\/todir<\/code><\/pre>\n\n\n\n
\n
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<\/code><\/pre>\n\n\n\n
<location_of_your_backup><\/code> 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<\/code> folder used to backup your PC. Then the
<location_of_your_backup><\/code> will be
PC_backup<\/code>.<\/p>\n\n\n\n
Creating script for backup and restore using rsync in Linux<\/h2>\n\n\n\n
#!\/bin\/bash\n\nset -o errexit # exit on error\nset -o pipefail # fail the pipe if any of its command fails\nset -o nounset # if any variable is undefined exit\n\nbackup(){\n\nbackup_destination=\"\/mnt\/VHD\"\n\n# `SOURCE\/ means` only SOURCE's content will be transferred.\n# `DESTINATION\/ and DESTINATION` both are same for rsync\nsudo 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\"\n\n}\n\n# no need to use sudo because most of the time you will be executing it as root\nrestore(){\n\n# users will answer the upcoming questions based on lsblk's output\nlsblk\n\necho -e \"where do you want to restore from? (ex- sda1, nvme1n1p1,) \"\nread -r from\necho -e \"your backup location in the above source with no trailing forward slash? (ex- backup, and backup\/archlinux) \"\nread -r location\necho -e \"where do you want to restore to? (ex- sdb1) \"\nread -r to\n\n# make directories otherwise error\nmkdir \/mnt\/fromdir\nmkdir \/mnt\/todir\n\n# umount in case you have mounted the source\/destination at somewhere else.\numount --lazy \/dev\/\"$from\" || :\numount --lazy \/dev\/\"$to\" || :\n\n# mounting the source and destination\nmount \/dev\/\"$from\" \/mnt\/fromdir\nmount \/dev\/\"$to\" \/mnt\/todir\n\nrsync --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\n\numount --lazy \/mnt\/\"$from\"\numount --lazy \/mnt\/\"$to\"\n\n}\n\n\nhelp_page(){\n\ncat << document\n-b [b]ackup\n-r [r]estore\ndocument\n\n}\n\n\nmain(){\n\n case \"$1\" in\n \"-b\") backup ;;\n \"-r\") restore ;;\n \"-h\") help_page ;;\n *) help_page && exit 1 ;;\n esac\n\n}\n\n\nmain \"$1\"<\/code><\/pre>\n\n\n\n
Creating a cron\/anacron job for periodic backup using rsync<\/h2>\n\n\n\n
Rsync backup using crontab -e<\/h3>\n\n\n\n
sudo EDITOR=nvim crontab -e<\/code>.
Append a line:<\/p>\n\n\n\n30 23 * * TUE,FRI \/path\/to\/rsync.sh -b<\/code><\/pre>\n\n\n\n
Rsync backup using \/etc\/crontab<\/h3>\n\n\n\n
\/etc\/crontab<\/code> as well:<\/p>\n\n\n\n
30 23 * * TUE,FRI root \/path\/to\/rsync.sh -b<\/code><\/pre>\n\n\n\n
Rsync backup using anacrontab<\/h3>\n\n\n\n