Linux (Debian/Ubuntu): Backup
In this post I want to show you how to create a basic full backup of your file system and how to restore it. A Linux system with root access is required. It does not matter whether it is a Dedicated Server or VPS.
Important: The following commands were tested with Ubuntu 14.04 (64 Bit). They should work with Debian as well. Other distributions like CentOS require a few adjustments here and there.
- Create backup
We use tar with gzip compression to create an archive of the root partition. Special directories will be ignored as they do not contain any relevant data. The MySQL directory won’t be archived as well. I get back to that later.We create the backup file in a separate folder which will be also excluded. In this example, I will make use of the Contabo Backup-Space.
apt-get install curlftpfs
curlftpfs USERNAME:[email protected] /mnt
tar czf rootfs_backup.tar.gz --directory=/ --exclude=dev/* --exclude=proc/* --exclude=run/* --exclude=sys/* --exclude=tmp/* --exclude=var/lib/mysql/* --exclude=mnt/* .
The backup can also be temporarily created on the server hard disk. The directory in which the archive will be created must be excluded in any case. I strongly recommend to store the archive in an other secure location though.We now have an image of the whole root partition. This image can be used to restore the server in its current state at any time.
- Restore backup
We start the server into the rescue system und login via SSH. It is important to choose the equivalent version, because else the chroot command would fail. In most cases that would be 64 Bit. In this example, the new hard disk is completely empty. Thus, we need to create a new root partition first and also a swap partition if necessary. Use parted to create the partition:
parted /dev/vda mklabel msdosNow we can mount the new root partition and the FTP Backup-Space:
parted /dev/vda 'mkpart primary 1 -1'
parted /dev/vda set 1 boot on
mount /dev/vda1 /mnt/customAnd finally start the actual restore process:
curlftpfs USERNAME:[email protected] /mnt/backup
cd /mnt/customThere are a few modifications necessary to make the system bootable. We change the working environment with chroot:
tar xzf /mnt/backup/rootfs_backup.tar.gz
mount -o bind /dev /mnt/custom/dev
mount -o bind /sys /mnt/custom/sys
mount -t proc /proc /mnt/custom/proc
chroot /mnt/custom /bin/bash
The operating system uses UUIDs to identify partitions. Since we created a new root partition we have to replace the old UUID with the new one. We find the new UUID with this command:
Open /etc/fstab with your favorite text editor, e.g. nano and change the UUID of /. Then fix the GRUB configuration and install the boot loader like this:
grub-mkconfig > /boot/grub/grub.cfg
We leave the chroot environment with exit. After a reboot your server server should be running normally again with the backup state.
- MySQL exception
MySQL databases can’t be copied directly from the directory while the MySQL server is running. This could result in data corruption. Thus, we use mysqldump here:
mysqldump -p --all-databases > db_backup.sqlThis command should be executed before creating the root filesystem backup to include it in the same image. Use the following commands to restore the database backup after the server has been booted normally again for the first time:
service mysql start
mysql < db_backup.sql
service mysql restart
As you can see a few commands can be enough to not only backup important personal files, but also the complete system including all settings. Regular and reliable backups can prevent long outages and – the real important part – data loss.
Always remember: Data without backup is insignificant data!