Backup is important part of the Linux system administration. Without backups a system is not safe and also the system administrator position too 🙂 . There are different ways to take backup. Some tools provides user level backup operation like copying or sync files. Other backup tools provides more file system level backup operation. One part of the Linux operating system is low level. Simply it has a lot of low level applications those helps system administrators. In this tutorial we will look
dd command or tool which can do low level raw disk operations.
First of all
dd command is low level command and use cautiously. While providing disk and partitions parameters use
fdisk command to get disks path. Check twice before running command. You can find
fdisk command tutorial below.
Copy/Backup Entire Hard Disk
As we know Linux operating system provides raw access to the disk and partitions. These disks and partitions are provided in
/dev/ path. So in order to use
dd we will provide exact disk and partition file names. This operations also called as clone because a single is copied as a whole into another disk without a modification.
In this example we will create backup of
vda to the disk
sda . Before starting backup or copy operations be sure that
sda size is bigger than
$ sudo dd if=/dev/vda of=/dev/sda
ifprovides the source hard disk
ofprovides the destination hard disk
Copying operation duration can be changed according to write performance of the destination disk and read performance of source disk.
Set Byte Size
dd copy operations reads some bytes at a time and write to new disk. If we want to make operation faster and know that our disks are performative. We can change the a time copy size with
bs size is provided by byte but following suffixes can be used too.
K= 1024 byte
MB= 1000 * 1000 byte
M= 1024*1024 byte
GB= 1000*1000*1000 byte
G= 1024*1024*1024 byte
In this example we will set the byte size as 5M for copy operation.
$ sudo dd bs=5M if=/dev/vda of=/dev/sda
Create Hard Disk Image
In previous examples we have copied the whole disk image into another disk. In most situations we generally use disk images for backup creation. We can write copied disk image as a file into another disk.
In this example we will create disk image as a file named
sda.img into folder
/mnt/backup/ which is mounted from other disk. Other or destination disk can be an NFS, DAS, NAS, SAN etc.
$ sudo dd if=/dev/vda of=/mnt/backup/sda.img
Create Floppy Image
Once a time floppy images were very popular in IT. Event operation system was installed from Floppies and backing up them were important. Actually creating a floppy image is the same as disk image because all of the sources are same type.
In this example we assume the floppy device is named as
fd0 and we will copy into
~/ home directory as
floppy.img with the following command.
$ sudo dd if=/dev/fd0 of=~/floppy.img
Another device popular used in Linux servers is CDROM. CDROM can provide data about Linux installation or other critical data. We can copy CDROM device too with
dd command. But here we need to check one point. To make things without error we should unmount the CDROM if it is mounted.
In this example the cdrom device name is
cdrom and the backup name is
$ sudo dd if=/dev/cdrom of=~/linux.iso
Copy/Backup A Partition
Up to now we have copied who disks or devices. But generally we need to copy only a single partition of a hard disk. Actually there is no difference for copying disk partitions. Disk partitions generally named by numbering the partitions index and disk index. For example first partition of the disk named
vda0 and second partition will be named
In this example we will copy or backup the second partition of the disk
vda to a file named
$ sudo dd if=/dev/vda1 of=/mnt/backup/back.img
Redirect and Compress DD Backups
While taking backups about the disk there will be a lot of data. This will make the size very big. There are different ways to lower the size of the backup image taken by
dd command. What the dd command do is it reads from given source device and then we can compress the image before saving to a file.
In this example we will backup the device
vda1 and save as
back.img.gz . We will redirect
dd output with
| pipe to the
$ sudo dd if=/dev/vda1 | gzip /mnt/backup/back.img.gz
In this example we have used gzip but also use other compression formats and applications 7z , xz , … as is.
Fill Hard Disk with Zeros
dd provides low level operations. We can also write specified hard disk with a given data source. If we want to delete all data resides on the disk we can use
dd too. In Linux operation systems devices named
zero exists which is psedeu devices. This devices creates zeros as stream. In order to fill a hard disk device with zero we will read this devices and write the destination devices like below.
$ sudo dd if=/dev/zero of=/dev/vda
Fill Hard Disk with Random Data
Filling whole disk with zero is an option. But there is also other option which is more secure. We can use
random device of Linux operating system. This device will generate random numbers and we will write these into the destination hard disk or device.
In this example we will write random data device named
$ sudo dd if=/dev/random of=/dev/vda