INT 21h

Select language

Backup disk image using dd with on-the-fly compression and transfer via ssh or netcat

№ 8232 В разделе Sysadmin от November 16th, 2016,
В подшивках: ,

Of course, backing up disk or partition must be unmounted from the OS. If you are backing up the root partition, you must at least remount it in RO mode or do it with LiveCD on a flash drive.

If the backup saved on the local machine

It is quite reasonable not to backup the same disk where you writing the image

To make a full backup of the hard drive into a compressed file:

dd if=/dev/sda bs=1M | gzip -5 -c > /media/backups/diskimages/sda_full.image.gz

Command explanation:

[read the block device using dd and send it via pipe to gzip] | [gzip reads data from dd and compresses it, writing to stdout] > [send stdout from gzip to a file]

Unpack the image from the file and write to the disk:

gunzip -c /media/backups/diskimages/sda_full.image.gz | dd of=/dev/sda bs=1M

Command explanation:

[using gunzip read the file, unpack it and send it via pipe to dd] | [dd accepts unpacked data and immediately writes it to hard drive]

Backup to a remote server inside ssh tunnel

This method guarantees privacy, but the speed drops dramatically. It is ideal for backups via the Internet or untrusted networks.

Run locally, copy from local to remote:

dd if=/dev/sda | gzip -5 - | ssh root@backup.server.ru dd of=/media/backups/diskimages/sda_full.image.gz

And this is if the backup server itself is the backup initiator:

ssh root@client.server.ru "dd if=/dev/sda | gzip -5 -" | dd of=/media/backups/diskimages/sda_full.image.gz

Fast backup with netcat in a local network

Best method in a trusted local network, without cryptography.

Many people on the Internet have examples of this command, but with compression on the backup server side. Let’s do it normally and compress the data in the same place where we read the data and transfer it over the network already in compressed form. By removing cryptography and adding compression, the backup will be as fast as possible.

First, on the backup server, you need to open the port and be ready to write data into the file:
nc -l -p 31337 > /media/backups/diskimages/sda_full.image.gz

On the machine that needs to be backed up:
dd if=/dev/sda bs=1M | gzip -5 - | nc -w 5 backup.server.ru 31337

Check if the full disk image was backed up correctly

Unlike a partition backup, a full disk backup is a bit more complicated.

sudo losetup -Pf /media/backups/diskimages/sda_full.image.gz
sudo mkdir /mnt/test
sudo mount /dev/loop0p1 /mnt/test

After running losetup on backup file the OS recognize partitions and add new block devices /dev/loopX, which can then be mounted and the backup can be verified to be working.

Нет комментариев »

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Облачная платформа
Яндекс.Метрика

Fortune cookie: "Organized religion: The world's largest pyramid scheme." [Bernard Katz]