TLDR;
Upon further reading, the rsync method below will likely take more time and may be incomplete (for instance, booting may not work after so files will need to be updated such as fstab). I may update this answer in the future but for now just see Copy the entire root directory (/) for transferring OS to new computer? OR
https://unix.stackexchange.com/questions/608131/backup-and-restore-of-root-file-system-ubuntu-20-04
This will backup your entire root directory (notice /
is used before /Drive
which is where you're backing up to) and notify when complete for ten seconds (see man rsync
for details about the options used before running)
sudo rsync -aAXHS --info=progress2 --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*", "/Drive", "/lost+found"} / /Drive && notify-send -t 10000 "Backup complete"
If that doesn't work for you this should
sudo rsync -aAXHS --info=progress2 --delete --exclude "/dev/*" --exclude "/proc/*" --exclude "/sys/*" --exclude "/tmp/*" --exclude "/run/*" --exclude "/mnt/*" --exclude "/media/*" --exclude "/Drive" --exclude "/lost+found" / /Drive && notify-send -t 10000 "Backup complete"
Use an alias for this command to shorten the keystrokes by putting the following in your .bashrc
alias backup='sudo rsync -aAXHS --info=progress2 --delete --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*", "/USBDrive", "/lost+found"} / /USBDrive && notify-send -t 10000 "Backup complete"'
To restore the backup, just reverse the source and destination paths in the above command.
Please be mindful that this is suitable for local and stand-alone systems only. If your system is being actively accessed by some other systems on the network, it is not a better solution.
Because, the contents of the systems might be constantly updated every minute, and some files may change during the rsync process.
Say for example, when rsync will reach the file 2, the contents of the previous file (File 1) might be changed. This will leave you with a dependency error when you will need to use that backup.
In such cases, a snapshot-based backup is the better approach. Because the system will get "froze" before the backup process starts and get it "unfreeze" when the backup process finishes, so all the files are consistent.
Details (rsync method)
I looked into Clonezilla but currently, incremental/differential backups are not supported as seen here.
This means that every time you back up your system you will have to create a new image. This isn't efficient if you will be backing up your system often.
It looks like the accepted answer in this post will work but here are some more options; borrowing some ideas from the accepted answer. The accepted answer will not back up any repositories you have cloned into your system.
If you had to build any programs after cloning then you will have to do that over again. It may be true that this will not be as robust and stable as the accepted answer. But I think these are worth trying, especially if you have spent a lot of time building programs from source, for example.
Another advantage to these options below is they should work on many Linux distributions. Worst case, it doesn't work and then you can do the accepted answer (assuming you made a backup of your new installation first or you haven't changed the new installation enough to care if you have to install it again).
As described in the accepted answer, if some of your programs aren't working, you could try restoring the .* file or directory which is the package configuration file or directory for most programs. You could do this by simply deleting the file or directory and then running the respective program again. If the following concerns you from the accepted answer, "You could copy your whole /etc directory but this can create problems if your machine does not have the same setup in terms of hardware. Which it won't go from one machine to another." Then you could manually copy specific /etc directories separately or utilize section 3.9, Advanced usage of filter rules, described below. I think the section, "Your installed packages", in the accepted answer isn't required with the methods below. As stated in the accepted answer, "Copy your config files before or after you install new software. Don't do this while a software package install is currently running. You could break the installation."
I found this post which uses the rsync command (which should work on most Linux distributions).
But the only things I think you need to know from this post (since the source in this link describes this in more detail) are the following:
- Please be mindful that this is suitable for local and stand-alone systems only. If your system is being actively accessed by some other systems on the network, it is not a better solution. Because, the contents of the systems might be constantly updated every minute, and some files may change during the rsync process.
- Say for example, when rsync will reach file 2, the contents of the previous file (File 1) might be changed. This will leave you with a dependency error when you will need to use that backup. In such cases, a snapshot-based backup is the better approach. Because the system will get "frozen" before the backup process starts and get it "unfreezes" when the backup process finishes, all the files are consistent.
- First, insert your backup medium (USB thumb drive or External hard disk). Then find the drive letter using the 'fdisk -l' command. In my case, my Pen drive id is /dev/sdb1. Mount your drive to any location of your choice. I am going to mount it under /mnt. $ sudo mount /dev/sdb1 /mnt
- In one of the comments, somebody says: "SK, I appreciate your efforts. However, IMHO you should make it more forcefully clear to your readers that RSYNC is not a suitable complete disk backup solution. It is an excellent FILE backup utility. However, it’s not a suitable DISK backup utility. The reason why is a disk must be un-mounted before backing up or there is a very high chance of file corruption on restore."
- In another comment, someone says: “It doesn’t matter if your hard drive is a different size, or use a different filesystem.” Au contraire. Rsync is great, but it does matter about the destination file system. For instance, using rsync to back up files in an ext4 file system to a FAT file system, would mean loss of file ownership and permission information. The destination file system needs to have at least the capabilities of the source file system.
- A tip from a commenter: To be on the safe side with open files or ones that may change, you could always switch to runlevel 1 before starting the backup.
For me, external drives are mounted under /media/$USER if I right-click the drive in nautilus and then click mount.
Other than that, you'll want to read most of section 3 for the numerous backup options available to you here
In my case, I went with 3.7 and 3.8, which is what is outlined at ostechnix.com. I recommend reading 3.7, Full system backup, 3.8, Restore backup, and 3.9, Advanced usage of filter rules, at the very least as it goes into more detail than ostechnix.com. Section 3.6, Snapshot backup might be what you want if your system is being actively accessed by some other systems on the network, as described in the first point above.
I considered section 4, File system cloning, but upon further reading, this article says:
Tip: Over time file systems get new features and the mkfs utilities change their defaults, but not all new features can be enabled without reformatting. So, when moving data to a new drive, instead of cloning the block devices or file systems, consider creating a new file system and only copy the files (and their attributes, ACLs, extended attributes, etc.) with e.g. rsync.