Linux Partitions & Second Hard Drive with Back-up Script
Updated: 2013-04, Ubuntu 12.04 LTS.
Summary: This page shows methods that can be used to partition a hard drive and add a second hard disk to a computer running Ubuntu linux. You MUST MAKE BACKUPS of any important files whenever you change the partitioning on a hard drive. You can easily wipe your drive with no chance of recovery.
Note: If you are creating a new computer and have two hard drives, I have found it to be easier to get the operating system installed with only one hard drive and then add the second hard drive after powering down your new system.
Install The Software:
Since the latest Ubuntu distribution comes with a very nice disk utility, the only software you need to install is gnome-schedule, to easily schedule cron jobs. There is another disk info and partitioning program, called gparted if you want to compare them.
me@myhost$ sudo apt-get install gnome-schedule gparted
WARNING: REMEMBER that deleting /removing a partition will ERASE ALL DATA on the partition! There is no "undo".
To find out the details about your storage divices, go to System --> Amdinistration --> Disk Utility. Normally, it is best to decide on your partioning scheme BEFORE you install an operating system. Here are thumbnails of both the Disk Utility and the gparted programs. Click for a closer look.
If you want to change partitions, you must first unmount them. Here is some important gparted documentation. I believe gparted is used from the Ubuntu live CD, for example, if you want to create a custom partition scheme, such as having a separate "/home" partition. You can switch which hard drive you are looking at by selecting from the drop-down menu on the right. Before creating new partitions, remove the old one(s) first, as a separate step.
Mount Second Hard Drive by editing /etc/fstab:
WARNING: You can seriously mess up access to your system if you ruin the fstab (Filesystem Table) file. Make sure you make a backup FIRST and know how to revert to it.
On a home system it is easy enough to edit the /etc/fstab file using gedit:
me@myhost$ sudo gedit /etc/fstab
I suggest you follow the suggestions in the /etc/fstab file and make your changes robust by getting a UUID number. In a separate terminal, the command for drive /dev/sda1 (see above) would be:
me@myhost$ sudo blkid /dev/sda1
Enter the data using the spacing of the lines above it. There is and explanation of the /etc/fstab file at the Fstab Wiki. In my example, the "/", "/home", and "swap" partitions are all on the same disk, but have different UUID numbers generated at installation. For my 2 backup disks, I manually enterd the names "/bu300" and "/bu250" after I had created these directories (folders) at the top level. This file will associate the physical disks to these mount points.
# /etc/fstab: static file system information.
#[PARTIAL FILE: Columns are as follows: File System Name, Mount Point, Filesystem Type, Options, Dump, and Pass*.
# / was on /dev/sda1 during installation
UUID=71537a2a-2c85-40b1-9969-6b73ead86456 / ext4 errors=remount-ro 0 1
# /home was on /dev/sda3 during installation
UUID=edbed3cc-dd0a-404c-898e-551eb41a1bb3 /home ext4 defaults 0 2
# swap was on /dev/sda5 during installation
UUID=99e91184-e43b-4889-8bfb-ae86017ed5ec none swap sw 0 0
# Two backup drives 300GB and 250GB. Get UUID with "blkid" command.
UUID=ab5830c2-6591-48f9-854e-ee702ccdda5b /bu300 ext4 defaults 0 2
UUID=43ff7d13-ece5-4459-9a0c-8421d86d51a0 /bu250 ext4 defaults 0 2
There is a graphical program (GUI) that may help you do this called Storage Device Manager (search in Synaptic). I have successfully used it with a single backup drive, but now use the direct method described above.
Create & Run A Backup Script Using rsync:
Give yourself permissions if necessary on your /backup drive:
me@myhost$ sudo chown -R USER_NAME /backup
me@myhost$ sudo chgrp -R USER_NAME /backup
The program rsync efficiently synchronizes two folders by only modifying the files in the final directory that have changed since the last synchronization. The -av options DO NOT delete files, though, they only update changed files and add new ones. You need to eventually "clean up" the /backup folder of stuff you REALLY want to delete.
Below is an sample backup script that matches the default files in a Ubuntu Linux installation. In this example the only mount point created was /backup for a single backup hard drive. You need to substitute your user name for USER_NAME. I create and extra folder called "Voice" for lectures, foreign language CDs, etc. You need to delete the comment sign "#" to use.
rsync -av /home/USER_NAME/Documents/ /backup/Documents
rsync -av /home/USER_NAME/Downloads/ /backup/Downloads
rsync -av /home/USER_NAME/Music/ /backup/Music
rsync -av /home/USER_NAME/Pictures/ /backup/Pictures
rsync -av /home/USER_NAME/Public/ /backup/Public
rsync -av /home/USER_NAME/Videos/ /backup/Videos
#rsync -av /home/USER_NAME/Voice/ /backup/Voice
You can create a cron job easily using the GUI tool gnome-schedule (pictured below). If you named your backup script above "backup.sh" in your home directory, then the "Command" box in Schedule would be "/home/USER_NAME/backup.sh", or "./backup.sh". Schedule it for once a day when you know your computer will be on and CHECK IT OFTEN to be sure your files are getting backed up.