Ubuntu system backup and restore to the same computer or another computer (tar method)

This article is transcoded by 简悦 SimpRead, original text at www.cnblogs.com

Backup and Restore Ubuntu System to the Same or Another Computer (using tar)

Original by laukal published on 2018-07-28 15:35:14, reads 2205, favorite, expand

Recently I have been working with Xtion2, openni2, and every time I install something, something else stops working. Since I’m not familiar with Ubuntu, I cannot find the errors, so I have to reinstall the system each time. After spending several days setting up the system with mxnet, orbslam, opencv, ROS inside, it becomes awkward; each reinstallation requires days to set up the environment, exhausting! Then I started thinking about system backup and restore. I began trying tar backup methods found online. At first, it only allowed restoring on the same computer without reinstalling the system. Later, when the graphical interface broke down, I thought since I need to test it, I should learn it fully; otherwise, I wouldn’t know how to deal with it. When reinstalling counts as changing the hard drive, it can be applied to different computers. Sometimes you’re forced to do it, so why not try!

Principle: “In Ubuntu system, everything is a file!!!”

1. System Backup

When backing up the system, installation matters. Usually, backup means backing up the system root directory (/.). If your system partitions directories differently, you will need to consider other things. This article assumes the system is divided into the primary partition / and swap partition. Specific partitioning methods can be referenced via the link below.

https://blog.csdn.net/zhangxiangweide/article/details/74779652

Steps: 1. Go to root directory

  1. cd / // go to root directory

  2. sudo su // get root privileges

  3. Packaging

tar cvpzf backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/sys --exclude=/media /

Explanation: Compress into backup.tgz in the current directory, excluding folders /proc, /lost+found, etc. The “/” at the end represents the root directory. The compressed file will be saved under the root directory /. If you want to save it somewhere else, add the path in front of backup.tgz. It should be placed under the media path, where external USB sticks etc. are mounted so you can copy it out.

Note: The packaging will show some previous errors at the end, which can be ignored.

2. System Restore (on the same machine, to avoid reinstalling the system)

Sometimes when the computer crashes and errors occur, you can restore by copying good files back to the previous state. The restore is relative because it only extracts — any newly installed files remain, only the files you had before overwrite the existing ones and revert to the old state. If the computer cannot enter the system, use a USB boot device to enter using a live system.

  1. Go to root directory

  2. cd / // go to root directory

  3. sudo su // get root privileges

  4. Extract

sudo tar -xvpfz backup.tgz -C /

If backup.tgz is placed elsewhere, add the full path in front, e.g. /media/kingston/backup.tgz

Then restart! Mainly based on the following two blogs. For partitions separated into 4, you need to test yourself; I am not sure if all can be packed at once. If it works, please comment and leave feedback so more people can know. Thanks!

https://blog.csdn.net/sinat_27554409/article/details/78227496

https://blog.csdn.net/qq_35523593/article/details/78545530

3. System Restore (different computer, including a reinstalled system)

This step continues from the first: after backing up the system, copy backup.tgz to a USB drive.

  1. Reinstall the system (or install a new system on a new computer)

  2. Backup new system files (backup the UUID)

  3. cd /etc/

  4. sudo cp -pdr fstab /home/laukal/

Copy the file to the home folder, save it somewhere that won’t be overwritten.

  1. Extract the system

Go to root directory, extract:

  1. cd /

  2. sudo su

  3. sudo tar -xvpfz /media/kingston/backup.tgz -C /

  4. Copy the saved fstab back to the restored system

sudo cp -pdr /home/laukal/fstab /etc/
  1. Open fstab and copy the hard disk’s UUIDs. Replace the UUIDs in the /boot/grub/grub.cfg file (there are more than 20). If you don’t replace them, the system won’t boot because it cannot find the hard disk.

UUID=bb089529-c213-45ad-aa16-89e36f1c63ae (the first one, not the swap one)

Then restart.

The main references are the following two articles:

https://blog.csdn.net/zeg635702733/article/details/53992224

https://www.linuxidc.com/Linux/2014-01/94975p2.htm