Test HTTP Requests Tools Blog PHP Quiz API Log In With Github
Test HTTP Requests Tools Blog PHP Quiz API Log In With Github
« Return to the tutorials list
We have updated the website and our policies to make sure your privacy rights and security are respected.
Click here to learn more about the way our website handles your data.

Remove this message.

How to backup Ubuntu and restore it using "dd"

Daniel Gheorghe Difficulty: 20 / 50 Tweet

DD is a Unix and Unix-like utility that is primarily used to convert and copy files. One really useful use-case for dd is that it can "pack*" entire partitions into one single image file, thus allowing users to recover entire disks by "unpacking*" and restoring a file-system in case of hardware failure.

To create a full backup for a partition the command you need to run is straight-forward. The size of the resulting image file will be the same as the size of your partition so make sure you have enough free space on the drive you are placing your backup... also, please avoid placing your image file on the same drive that you're backing up.

First, let's identify which partition we should backup: user@x:~$ df

The output of df (above) will look similar to this:
df output example
You will know that which partition is your primary one by looking at the "Mounted on" column... and identifying the entry that is "Mounted on" "/".

Now you're ready to backup your file-system

        user@x:~$ sudo dd if=/dev/sda1 of=/backup/location/filename.img

The backup process will take a while so sit tight and let it to its job. Once it finishes it will show you a report with X records in, X records out, X seconds etc....

Setting this up as a cron-job

You probably don't want to run this manually simply because running backups should be a scheduled process, so I suggest you set this up as a weekly or even daily cron job. To enable this run: sudo crontab -e** and add the line below making sure you modify the paths and time to fit your needs.

        @weekly dd if=/dev/sda1 of=/backup/location/filename.img >> /var/log/backup.log


In case you need to restore the image you've just created ... which I hope never happens :) ... all you need to do is to boot your system using a live Ubuntu cd or bootable usb stick and run the dd command in reverse pointing to a newly created partition, on a new hard-drive.

        user@x:~$ sudo dd if=/backup/location/filename.img of=/dev/sda1

I also made a short video showing the above

* Really... dd is not an archive manager, but I didn't find a more suitable word for what actually happens.
** You need to run sudo crontab -e because you need root privileges