Backup and image restoration using dd

What is dd?

The dd command is a utility for copying and converting files, commonly used in most Unix-like operating systems. It can be used for tasks such as creating data backups, cloning disks, creating partition images, and more. However, caution should be exercised when using dd, as improper usage can lead to data loss.

Here is the basic syntax of the dd command:

dd if=input_file of=output_file [options]

Where:
• if (input file) specifies the input file or data source.
• of (output file) specifies the output file or destination for the data.

Below are some common options and parameters of dd:

1. bs (block size): Determines the size of the data block. For example, bs=4K sets the block size to 4 kilobytes.
2. count: Specifies the number of blocks to copy. For instance, count=100 copies 100 data blocks.
3. iflag and oflag: Options for controlling input and output flags. For example, iflag=direct can be used to read data directly, bypassing the operating system cache.
4. seek and skip: Options for moving the input or output file pointer to start or end copying from a specific position.
5. status: Option to display information about the progress of the copy operation.
6. conv (conversion): Allows various data conversions, such as changing case (conv=ucase or conv=lcase), deleting characters (conv=sync), and more.
7. iflag and oflag: These options allow you to configure flags for input (iflag) and output (oflag) data. For example, direct can be used to read or write data directly without buffering.
8. status: This option enables the display of information regarding the progress of the copying operation.
9. seek and skip: Allow you to move the input or output file pointer to start copying from a specific position.

Let's consider examples of using dd to copy files and directories to remote Cloud Storage provided by Introserv using SSH protocol and the rsync command. Additionally, the example will demonstrate backing up and restoring a system image using a third disk. Important note: Similar commands, with the specification of a target server, will allow you to copy directories and files to any remote server via SSH. Additionally, for data integrity, we will use a "recovery" image called Finnix.

Creating a Backup of the System Image

1.Creating a backup:

Preparations:
1. Download Finnix to your computer from https://www.finnix.org.
2. Connect the ISO image via IP-KVM.
3. Boot the server from the Finnix ISO image.

2.Working with Finnix

We will explore two methods, one of which is possible using Introserv's Cloud Storage.

To mount a remote file storage (cloud storage) via SSH, we recommend using a tool like SSHFS (SSH File System), which allows you to connect remote directories on your local computer using the SSH protocol. Here's how to do it:

2.1. Install SSHFS. 

Since it is already installed in Finnix, we won't perform this step.

sudo apt-get install sshfs

2.2. Create a local directory where you want to mount the remote folder. 

For example:

sudo mkdir -p /root/mnt/backup

This command creates the necessary directory structure, including missing parent directories (-p flag).

2.3. Mount the remote directory via SSHFS:

sudo sshfs box17469@box17469.introserv.cloud:/backups ~/mnt/backup/

• box*****: The SSH username on the remote server.
• Box*****.introserv.cloud: The IP address or hostname of the remote server.
• /backups: The path to the directory on cloudstorage.
• ~/remote_mount: The path to the local directory we created above, where we want to mount our remote folder from cloudstorage.

The system will ask for the password, which you can find in the order details in the Backup section.

2.4. Check our network folder.

2.5. To create a backup of your system, use the following command:

dd if=/dev/sda of=/root/mnt/backup/sda.ing bs=8M conv=sync,noerror

2 method is possible through a third disk or other external storage

2.1 Determine the hard drive where the main system is installed (in this case, Ubuntu)

In this case, it is sda.

2.2 Also mount the folder for the external drive so that you can place the backup image there.

To mount the created file system in the /mnt/ directory, run the following commands:

sudo mount /dev/sdX /mnt/папка # replace sdX with the specific device or partition (in this case, we have sdb)

To create a backup of your hard drive, use the following command:

dd if=/dev/sda of=/mnt/backup/sda.img bs=8M conv=sync,noerror

• if=/dev/sda - copy the entire sda hard drive;
• of=/mnt/backup/sda.img - copy to /mnt/backup/sda.img, where the /mnt/backup directory is the mount point of the disk that will contain the image;
• bs=8M - set the size of the hard drive cache to speed up the copying process (otherwise the data will be flushed in small batches of 512 bytes);
• conv=sync,noerror - tell dd to copy bit-by-bit with error ignoring.

Restoring a system backup to another server or disk

1. Connecting the rescue image

  • Connect the ISO image via IP-KVM.
  • Reboot the server into the Finnix ISO image.

2. Working with Finnix

Just like with the backup, we have two ways, using CloudStorage:

2.1. Create a local directory where you want to mount the remote folder. For example:

sudo mkdir -p /root/mnt/backup

2.2. Mount the remote directory via SSHFS:

sudo sshfs box17469@box17469.introserv.cloud:/backups ~/mnt/backup

The system will prompt for a password, which you can find in the order details under the Backup section.

Also check if the folder is mounted.

ls -l /root/mnt/backup/

2.3. Find the image that is signed sda.img on our connected network drive.

To restore your system from a backup, use the following command:

dd if=/root/mnt/backup/sda.img of=/dev/sda bs=8M conv=sync,noerror

2.4. After that, we reboot into BIOS

Set the disk where we deployed the system to be the boot priority, reboot and try to start the system.

Also, let's consider deploying a system image using a third disk or other storage:

2.1. Перезеграужаемся в Finniх. 

Mount the network storage or disk with the image using the command sudo mount /dev/external_network_disk_or_storage /mnt/folder_for_mounting.

Find the image that is signed sda.img

To restore your system from a backup, use the following command:

dd if=/mnt/backup/sda.img of=/dev/sda bs=8M conv=sync,noerror

After that, we reboot into BIOS and set the disk where we deployed the system to be the boot priority, reboot and try to start the system.