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 curlftps
curlftpfs USERNAME:PASSWORD@backup.contabo.net /mnt
cd /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 will 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 msdos
parted /dev/vda 'mkpart primary 1 -1'
parted /dev/vda set 1 boot on
mkfs.ext4 /dev/vda1Now we can mount the new root partition and the FTP Backup-Space:
mount /dev/vda1 /mnt/custom
curlftpfs USERNAME:PASSWORD@178.238.239.254 /mnt/backupAnd finally start the actual restore process:
cd /mnt/custom
tar xzf /mnt/backup/rootfs_backup.tar.gzThere are a few modifications necessary to make the system bootable. We change the working environment with chroot:
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/bashThe 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:
blkid
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
grub-install /dev/vdaWe leave the chroot environment with exit and ultimately the rescue system with exitrescue. 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.sql
This 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:
mysql_install_db
service mysql start
mysql < db_backup.sql
service mysql restart
As you can see a few command 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!