Hacker Forums

HOWTO backup your thumb drives in Linux.


by The Uni-Hacker on Aug.19, 2008, under Misc

Today most Linux desktop computers support USB thumb drives available off-the-shelf and most Linux distributions have built in kernel support for them as well. Ubuntu Linux supports these memory sticks out of the box, and that’s what this guide will be written around, so assuming you are running Ubuntu Linux, lets get started by logging into a system account with access to raw hard drive partitions, usually the root account.

First, plug your memory stick into an available USB slot on your computer.

Now lets goto your console and find out which device the system assigned the USB stick.
Type “dmesg | more” which is the command you use to look at system information, scroll down until you see some information about USB devices. dmesg shows the latest information, and the dmesg log file is continuously updated and is not a static file.

In this case, and most cases, the USB stick was assigned to the /dev/sdb device. Notice the /dev/sdb1 and /dev/sdb2, this is showing that my USB memory stick has two partitions.

Now that we know where our USB stick is accessible to, lets make sure it’s not mounted. To check it it is mounted, type the df command.

As you can see under the “Filesystem” column, there is no device called /dev/sdb1 or /dev/sdb2, so we know our USB memory stick isn’t mounted.

Now for the fun part, we get to backup the two partitions. Remember, this makes a raw mirror of the device itself including the filesystem type, partition size and all the partition’s attributes. The nice thing is, this makes restoring ubber easy so long you as you get the same size memory sticks.

Type “dd if=/dev/sdb1 of=backup-sdb1.iso” to backup the first partition and go ahead and back up both partitions. Lets break down the command. dd was originally meant to be known as “disk duplicator”. The “if=” can be interpreted as the “input file” and the “of=” can be interpreted as the “output file”.

After backing up both partitions you should now have two files, backup-sdb1.iso and backup-sdb2.iso. Lets get a brand new memory stick to restore these files to, go ahead now, plug in the new memory stick.

So, your computer somehow magically erased your USB memory stick and now you want to restore it from the backups you made above. No problem, first, lets creating partitions. Remember, the partitions need to be the same size as your old partitions. So for a 2gig memory stick split into two partitions, that would be two 1 gig partitions.

First, make sure you’re logged in as root. Next, type “fdisk /dev/sdb” as root to open the disk partition utility. All commands from here will be from within the fdisk partition program. Now type a “p”. This will print any partitions on the USB stick. Remember, most USB sticks come pre-formatted with a VFAT partition, so we’ll need to delete it. Delete any partitions you see on you’re new memory stick by typing “d “ and the number of the partition you want to delete. “d 1” for a new memory stick (while in the fdisk utility).
Now lets create a new partition. Type “n” for new, select the default “First Cylinder”, and for the end blocks type +1024M. Now lets create another partition using the default “First Sylinder” and default “Last cylinder”.

Ok, so we created two partitions, lets hit a “p” and make sure they are there.

Yep, I now have two partitions /dev/sdb1 and /dev/sdb2….sweeeeeeet. So lets duplicate our backups to these partitions. Exit the fdisk program by typing “w” and hitting enter. This should save your partitions and exit the program, otherwise just hit “q” if “w” doesn’t get you out.

Simply type “dd if=backup-sdb2.iso of=/dev/sdb2” and wait for it to finish.

Now restore /dev/sdb1 the same way and you’re all set. If you get a “no space left on device” error, you created the wrong size partition, and your backup-sdb*.iso device sizes don’t match. The partitions have to be at least equal of size. Unplug the USB memory stick and re-plug it in. Ubuntu should auto mount the partitions for you so you can browse the files.

:
Click the Linux+ to view the picture in full size.
Linux+
Linux+
Linux+
Linux+
Linux+
Linux+
Linux+
Linux+
Linux+
Linux+
Linux+
Linux+

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...