Reformatting external USB hard drive for Linux
I use two external hard drives. One because the hard drive on my computer is too small, the other as backup of both my compters main drive and my first external disk. I went through a certain amount of pain setting up the first external hard drive, and thought I'd keep some notes this time round. Hopefully they'll be useful to others. Let me know if there's something wrong. Watch out: following these steps will totally wipe your external disk !!
My system
- Dell C400 40Gb, 512Mb RAM, 1.2GHz
- Dual boot Ubuntu Linux 5.10 (Breezy) and Windows XP
- Digital Western External USB Hard Drive 160Gb
- Toshiba External USB Hard Drive 320Gb. (This is the one to which the notes below apply.)
Partitioning and re-formatting
My Toshiba drive was pre-formatted with NTFS, but I wanted ext3, so I had to format the disk.
localhost kernel: [4454042.652000] Attached scsi disk sdb at scsi4, channel 0, id 0, lun 0
This tells you your machine recognised the new disk and assigned it the device /dev/sdb, which we'll need later.
sudo fdisk /dev/sdb
Type p to see the partitions already on the disk. If there are any you don't
want (in my case an NTFS partition), type d to delete it. Then type n to write a new partition. I
just wanted on primary partition going from the first to the last block, so accepted all default values. Before really executing these commands,
have another look to check all is right by typing p. I get the following output
Command (m for help): p
Disk /dev/sdb1: 320.0 GB, 320070288384 bytes
255 heads, 63 sectors/track, 38912 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1p1 1 38912 312560608+ 83 Linux
which means that actually the device is /dev/sdb1. If all is right, type w to commit the changes
and re-write the partition table.
sudo mkfs.ext3 /dev/sdb1
which installs an ext3 file system with journal (you might find places saying this is still preliminary, but it's the best choice, no point
going for ext2). This will take a while. If it takes ages (hours and more), check whether you might have specified the wrong
device, eg /dev/sdb instead of /dev/sdb1. If all is right, you should get an output like this
...
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
Mounting
Make a new directory where you want to mount the new file, eg
sudo mkdir /backup
Then add the following line to the file /etc/fstab:
/dev/sdb1 /backup ext3 rw,user,noauto 0 0
and finally mount the new drive by hitting mount /backup. You may not be able to write to this disk as normal user as root may own
it, but just doing chown user:user /backup will do the trick.
Backups
I use cron to backup the harddisk of my computer and my other external hard disk to the huge Toshiba disk. To check if cron is
running, type ps -ax | grep cron, but it should run in the background on most linux distributions I think. I want my files to be
backed up daily, so I made one file dobackups with this content
#!/bin/sh
That is, my (user -- you'll need to put your user name in here) entire home directory /home/user and the external hard disk /lin get copied over to the new disk backup every day. Instead of using cp, I use rsync, which does lots of clever things to only copy the differences between files, rather than all the files.
rsync -avuzCr --delete /home/user/ /backup/main/
rsync -avuzCr --delete /lin/ /backup/lin/
To make sure
this doesn't happen while you're working, have a look at the /etc/crontab file, which should contain a line like this
To get cron to do this write another file mycrontab with this content
# /home/user/crontab: my crontab file
The first digits (00) tell cron at what minute past the hour to run things, the second digit (5) tells it at what time to run things. In my case, the
backup will happen every day at 5.00am. To get cron to use this file and actually run the dobackups file
SHELL=/bin/sh
# m h dom mon dow user command
00 5 * * * /home/user/bin/dobackups
#make sure there is blanck line at the end
crontab -u user mycrontab