Tuesday, February 8, 2011

How to backup your hard drive or system with Linux

I created this how to for personal use but thought others might find it useful, all the tools used in this are free and should be available for any version of Linux.
A plain text version of this how to is available here.

Terminal commands listed in a section of text will also be listed below the section for easy copy paste and will be denoted as follows. You should replace the parts in [square brackets] to fit your system.

========================================
[commands here]
========================================
(in terminal in Ubuntu you can paste a command by pressing "ctrl + shift + v")


First I recommend booting your system with a live linux CD so that none of your system partitions will be in use and can be backed up.
(this should work with any live linux CD, but I know all the steps listed will work with Ubuntu.)

Once you have booted from the live CD open up a terminal window and type "fdisk -l" this will list all your connected hard drives, your hard drive will most likely appear as /dev/sda, or /dev/hda. Also under each hard drive there should be a list of partitions, you can use that list if you only want to backup a single partition not the whole hard drive.
========================================
sudo fdisk -l
========================================

You can also use the output of "frisk -l" to help speed up the backup, if you don't care about speed you can skip this step. If you want just the info you need on the hard drive you are backing up you can use this command, "fdisk -l /dev/[your drive] | grep Units"
Look in the output for where is says "Units = cylinders of [num1] * [num2] = [big_num] bytes" num2 is the number of bytes per block on the disk. You can improve the speed of the backup by increasing the block size by a multiple of 2 to 4, remember the number for later.
========================================
sudo fdisk -l /dev/[your drive] | grep Units
========================================



So if you want to back your full hard drive I suggest you use the following command,
"sudo dd if=/dev/[your hard] bs=[num2 x 2] conv=noerror,sync | gzip -c -9 > /[your backup folder]/drive_backup.gz",
the actual command will depend on your system but should look something like this:
"sudo dd if=/dev/sda bs=1024 conv=noerror,sync | gzip -c -9 > /HDD_backups/backup_sda.gz"

You could also use one of these other similar commands depending on your needs.

This command will copy from one hard drive to another.
sudo dd if=/dev/sdx of=/dev/sdy bs=1024 conv=noerror,sync

This command will copy to an uncompressed image file, which will be the same size as your hard drive.
sudo dd if=/dev/sdx of=/path/to/hard_drive.img bs=1024 conv=noerror,sync

(sdx could be sda, sdb, hda, or hdb etc.)

Full Hard Drive Backup
========================================
sudo dd if=/dev/[your drive] bs=[num2 x 2] conv=noerror,sync | gzip -c -9 > /[your backup folder]/drive_backup.gz
========================================


If you only need to backup a single partition the command is similar to the full hard drive backup, and will look something like this,
"sudo dd if=/dev/sda1 bs=1024 conv=noerror,sync | gzip -c -9 > /HDD_backups/backup_sda1.gz"
The number following sda should be the number of the partition you want to backup. Also you can modify any of the previously listed commands to let you just copy a single partition.

Single Partition Backup
========================================
sudo dd if=/dev/[your drive][part num] bs=[num2 x 2] conv=noerror,sync | gzip -c -9 > /[your backup folder]/part_backup.gz
========================================

To restore your compressed hard drive backup you'll want to run this command,
"sudo gzip -dc /HDD_backups/backup_sda.gz | dd of=/dev/sdx"
To restore your uncompressed hard drive backup you'll want to run this command, "sudo dd if=/path/to/hard_drive.img of=/dev/sdx bs=1024 conv=noerror,sync"

Full Hard Drive Restore
========================================
sudo gzip -dc /[your backup folder]/drive_backup.gz | dd of=/dev/[your drive]
========================================

Single Partition Restore
========================================
sudo gzip -dc /[your backup folder]/part_backup.gz | dd of=/dev/[your drive][part num]
========================================

Other backups you might want to do while you are booted from a live linux CD.

MBR backup + Partition Table

sudo dd if=/dev/sdx of=/path/to/MBR_Part_backup.img count=1 bs=512


MBR restore

sudo dd if=/path/to/MBR_Part_backup.img of=/dev/sdx

Add "count=1 bs=446" to exclude the partition table from being written to disk. If you want to manually restore the partition table.


Backup the Partition Layout

sudo sfdisk -d /dev/sda > /mirror/backup-sda.sf

When you create a whole disk backup, you don't have to worry about partitions. However, it can be handy to have this partition information (in case you need to mount a specific partition in the backup as a file using the exact offset).



This How To was put together by Tim the Enchanter (tim.the.enchanter88@gmail.com)

References used
- help and man pages for all the above commands
- http://mark.koli.ch/2009/05/howto-whole-disk-backups-with-dd-gzip-and-p7zip.html
- http://www.debianhelp.co.uk/ddcommand.htm
- http://wiki.linuxquestions.org/wiki/Dd