Backup external drive to image using pv



After building my new PC, I wanted to backup the old drive to an image. This way, if I needed any files, I could mount the image and retrieve anything I needed. I connected a USB backup drive to my laptop, pulled the drive out of the old PC, and connected the old drive to my laptop with this.

After plugging everything in, you can see the output of lsblk.

sda                   8:0    0 298.1G  0 disk  
├─sda1                8:1    0  1007K  0 part  
├─sda2                8:2    0   128M  0 part  /boot
└─sda3                8:3    0   298G  0 part  
  └─VolGroup00      254:0    0   298G  0 crypt 
                    254:1    0     8G  0 lvm   [SWAP]
                    254:2    0    25G  0 lvm   /
                    254:3    0   265G  0 lvm   /home
sdb                   8:16   0 465.7G  0 disk  
└─sdb1                8:17   0 465.7G  0 part  /run/media/logan/Passport
sdc                   8:32   0 232.9G  0 disk  
├─sdc1                8:33   0   100M  0 part  
├─sdc2                8:34   0 144.9G  0 part  /run/media/logan/9E80926180923FA7
└─sdc3                8:35   0  87.9G  0 part  /run/media/logan/FAMILY_DOCS
sr0                  11:0    1  1024M  0 rom

In this case, my laptop’s internal drive is /dev/sda, my backup drive is /dev/sdb, and my source drive (from the old PC) is /dev/sdc. Note, there are three partitions on /dev/sdc and I want to backup all of them.



I was planning on using dd, which does block-level copying. This means I can copy all the files on the drive, as well as the filesystem itself, to a single image file (similar to an ISO file for a DVD). The typical dd command looks something like below, using an input file (if) and output file (of).

sudo dd if=/dev/sdc of=/run/media/logan/Passport/20141126_old_downstairs.img

Be careful when using dd! It’s sometimes called disk destroyer, as you can easily wipe the wrong drive if you have your if and of mixed up.


Unfortunately, dd doesn’t show a progress bar or its current speed, so I’m going to be using pv as well.

sudo pacman -S pv

Now I can do something like this, and pipe dd through pv.

sudo dd if=/dev/sdc | pv | of=/run/media/logan/Passport/20141126_old_downstairs.img


However, according to the manual, pv can be used to create an image file itself (without using dd).

sudo pv -p -r -t -e -EE /dev/sdc > /run/media/logan/Passport/20141126_old_downstairs.img

-p = shows progress bar
-r = shows current transfer rate (average transfer rate is -a)
-t = shows timer
-e = shows ETA
-EE = skips errors

Below is what you’ll see when you use pv in the form above.

[logan@Arch ~]$ sudo pv -p -r -t -e -EE /dev/sdc > /run/media/logan/Passport/20141126_old_downstairs.img
00:17 [31.3MiB/s] [>                                 ]  0% ETA 0:36:22


The downside to dd or pv is that since they’re doing block-level operations, they aren’t aware of free space. The image you make will be exactly the size of the hard drive you’re backing up. To save space, you could use gzip to compress the image.

sudo pv -p -r -t -e -EE /dev/sdc | gzip -v > /run/media/logan/Passport/20141126_old_downstairs.img.gz



To restore the image to a blank drive, we’ll simply reverse the backup command.

sudo pv -p -r -t -e -EE /run/media/logan/Passport/20141126_old_downstairs.img > /dev/sdc


Mount and browse

If you don’t want to restore the image to a disk just to retrieve a file, you can mount the image on your machine and browse through the filesystem. Since we know our image has three partitions, we need to find out where the partitions begin and end. We can do that using parted.

sudo pacman -S parted

Use parted to list all the partitions on your image file.

sudo parted /run/media/logan/Passport/20141126_old_downstairs.img unit B print

From this output, you can see where each partition begins and ends.

Model:  (file)
Disk /run/media/logan/Passport/20141126_old_downstairs.img: 250059350016B
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start          End            Size           Type     File system  Flags
 1      1048576B       105906175B     104857600B     primary  ntfs         boot
 2      105906176B     155685224447B  155579318272B  primary  ntfs
 3      155686273024B  250057064447B  94370791424B   primary  ntfs

Now you can mount each partition based on its beginning location.

sudo mkdir /mnt/partition1
sudo mkdir /mnt/partition2
sudo mount -o loop,ro,offset=105906176 /run/media/logan/Passport/20141126_old_downstairs.img /mnt/partition1
sudo mount -o loop,ro,offset=155686273024 /run/media/logan/Passport/20141126_old_downstairs.img /mnt/partition2

Then, browse to /mnt and you should be able to access the partitions of your old drive to retrieve any files.

drwxrwxrwx  1 root root 8192 Nov  7 22:13 partition1
drwxrwxrwx  1 root root 4096 Nov  7 22:13 partition2


Useful stuff!



Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.