Linux backup and restore it to another hardware

I work in an organization with a small staff, the activity is closely related to IT, and we have problems with system administration. This is interesting to me and often I take the decision of some.

Last week we set up FreePBX under debian 7.8, hired a freelancer. During the setup process, it turned out that the server (yes, I call a regular PC) does not want to boot from the HDD when USB 3G modems are connected, which we use to make calls to mobile phones; BIOS flashing did not help. The mess. I decided that I need to transfer it to another piece of iron. So two related tasks appeared at once:

  • make a backup of the server;
  • restore backup on another hardware.

Googling did not give clear answers on how to do this, I had to collect information in pieces and try. All acronis'y discarded immediately, because it is not interesting.

I have little experience with linux-systems: setting up a VPN server on open-vpn, ftp-server and a couple of little things. I characterize myself as a person who knows how to read mana and edit configs :)

Below I describe my particular case and why I did just that. I hope it will be useful for beginners, and bearded admins will smile when they remember youth.

We start digging the theory:

On creating backups for a lot of articles, I noted for myself two ways: tar - packs and compresses all files, while MBR is not saved, my backup will weigh about 1.5 Gb; dd - makes a full copy of the partition, including the MBR and the entire area where there are no files, the archive will be equal to the size of the partition, in my case ~ 490 Gb.

The second method requires an external hard drive with a volume of at least the partition that is archived. And then what to do with it, it is not clear, to store on a shelf? I stopped at tar, it’s a little more difficult to implement, you will need to create an MBR, but the time to create / restore the archive is much shorter, it’s easier to store a backup, you can upload a half gig to the cloud and download it when you need it. You can record it on the same live flash drive with which I will boot.

So, the action plan:

  1. backup creation;
  2. formatting, disk layout, file system creation;
  3. backup recovery;
  4. creating an MBR;
  5. testing and troubleshooting.

1. Creating a backup

We boot from a live flash drive, I have it debian-live-7.8.0-amd64-standard.

Switch to root:

sudo su

We mount the partition that we will archive, I have sda1, so as not to accidentally break firewood, we mount it for reading only. You can view all your sections using the ls / dev | grep sd or df -l

mount -o ro /dev/sda1 /mnt 

Our flash drive is already mounted, but in read-only mode, you need to remount it for read-write in order to write a backup there.

mount -o remount,rw /dev/sdb1 /lib/live/mount/medium

Everything is ready to create an archive

tar -cvzpf /lib/live/mount/medium/backupYYYYMMDD.tgz --exclude=/mnt/var/spool/asterisk/monitor --exclude=/mnt/var/spool/asterisk/backup /mnt/

Here we have the parameters: c - create an archive, v - display information about the process, z - use gzip compression, p - save information about owners and access rights, f - write the archive to a file, file path, --exclude - exclude from archive directory (I excluded directories with conversation records and a directory with FreePBX backups), / mnt / - the directory that is archived.

We are waiting ... all the preparation and creation of the archive took me 10 minutes. If the flash drive was faster, it would be in 7-8 minutes.

Unmount the disk:

umount /mnt

... and reboot.


We put the archive in a safe place outside the office.

Restore backup on another hardware

2. Mark up the disk, create a file system

We boot from a live flash drive, I still have the same debian-live-7.8.0.

Switch to root:

sudo su

Mark up the disk. I liked the utility with the cfdisk pseudo-graphic interface. Everything is simple and clear there.


Delete all available sections. I created two new partitions, one on 490 Gb under / (sda1) and 10 Gb under swap (sda2) at the end of the disk, because it will be practically not involved. Check the types of partitions. Which for the system should be type 83 Linux, the second - 82 Linux swap / Solaris. We mark the system partition bootable, save the changes and exit.

We create the file system in the first section.

mkfs.ext4 /dev/sda1

3. Unpack the archive.

Mount a formatted partition

mount /dev/sda1 /mnt

Unpack the archive directly from the flash drive

tar --same-owner -xvpf /lib/live/mount/medium/backupYYYYMMDD.tgz -C /mnt/

The --same-owner parameter - saves the owners of the files to be unpacked, x - extract from the archive, v - display information about the process, p - save access rights, f - specify the file to be unpacked, C - unpack to the category.

4. We create MBR on a new disk.

To correctly create a boot record, we mount the working directories to our future root directory, I have this / mnt. The / dev and / proc directories are now used by the live system, we use the bind parameter so that they are available in two places at once:

mount --bind /dev /mnt/dev
mount --bind /proc /mnt/proc

Switch to the new system using chroot:

chroot /mnt

We make a swap section for the new system:

mkswap /dev/sda2 

We connect it the same:

swapon /dev/sda2

For grub to work, you need to tell it the correct UUIDs of the partitions in fstab, now the partitions of the previous system are registered there:

nano /etc/fstab

Open the second terminal (Alt + F2) as root:

sudo su

We call:


And we see the current UUID of partitions.

We manually rewrite them in fstab, switching between Alt + F1 and Alt + F2. Yes, it’s dreary, but trying to copy took me more time than rewriting. Save fstab.

Install grub2. I have one physical disk, so put it on sda:

grub-install /dev/sda

It should stand on a blank disk without errors. Updating information from fstab:


Returning to the live system:


Unmount all directories:

umount /mnt/dev
umount /mnt/proc
umount /mnt

If processes that use these directories get out, kill them using fuser.

Everyone, let's go. We boot from the hard drive:


Here the article was supposed to end, but I was having problems connecting to the Internet. The server sees the network, sees the computers in it, but does not go to the Internet ... and this is, as it were, important for telephony.

5. Testing and troubleshooting.

ifconfig -a

It shows the eth1 and lo interfaces, googling said that the gateway can only be registered to the eth0 connection, the rest are designed only for work inside the network.

It seems that the absence of eth0 is caused by the system transfer method. We find the file that is responsible for the numbering of the interfaces, look there:

nano /etc/udev/rules.d/70-persistent-net.rules

Indeed, there are two active interfaces defined by MACs. We comment on the first, prescribe eth0 to the second.

Restarting /etс/init.d/networking did not help, so we reboot:


We connect dongles, we check, everything works.
Thanks for attention.

Also popular now: