Howto – Backup your files automatically to a second hard disk in Linux

This “Howto” only really applies to a single workstation and is a quick and easy solution to backing up your own files, if you were backing up a server with numerous users, you would of course backup to another machine preferably at another physical location on the network.

Fitting the disk

Internal ATA hard drive. The ATA bus can usually handle only two devices per bus. If you are attaching two devices to the same bus, you need to configure one as master and one as slave. This is done with jumpers on the hard disks. If you have several disks, check the jumper settings on all disks. Check the top of the hard disks for information. (The third option is to use cable select, but it won’t be discussed here.)

Next you need to open the computer case, if you have not yet done so. There are usually several bays inside your computer where you can attach the hard drive. You will need screws to secure the drive to the bay. Then connect the hard disk to the ribbon cable and to the power cable. The power cable will fit only in one way. Usually the ribbon cable will have a red side, which is supposed to connect next to the power connector on the hard-disk. Then close the case.

Boot the computer. during boot, check the BIOS settings to see that the new drive is recognized correctly. If it is then continue.

To find out if your linux distribution recognizes your new drive use the following command.

billy@linux:~$ cat /proc/partitions
major minor #blocks name

8 0 488386584 sda
8 1 482335528 sda1
8 2 1 sda2
8 5 6048441 sda5
8 16 488386584 sdb
billy@linux:~$

Creating a primary partition using fdisk

You’ll notice above that I have sda which is my master drive and now sdb which is my new hard drive or (Slave Drive). Usually these disks would be labelled hda and hdb, but in my case I’m using SATA and not ATA disk drives.

Run fdisk to create a partition table, in my case this is going to be a backup hard drive that I’m only going to using to backup my own home directory, nothing more, so I’m create a new primary partition that will use the whole 500Gig of disk space.

[Make sure you fdisk the correct disk, in fact make sure you have a backup of all you files on an external disk or tape before you start doing this, you get it wrong then your on your own!]

billy@linux:~$ sudo fdisk /dev/sdb
Password:
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content wont be recoverable.

The number of cylinders for this disk is set to 60801.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0×0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-60801, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-60801, default 60801):
Using default value 60801

Here, I’m just typing p while in the fdisk command to display the newly created primary partition.

Command (m for help): p

Disk /dev/sdb: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 60801 488384001 83 Linux

Finally, we use the w command to write the changes to disk and exit fdisk.

Command (m for help): w

Formatting a partition in Linux

sudo mkfs -t ext3 /dev/sdb1
Password:

You should get output similar to this.

mke2fs 1.40-WIP (14-Nov-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
61063168 inodes, 122096000 blocks
6104800 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
3727 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

Creating a mount point and mounting the filesystem

I usually mount file systems of the /mnt directory, so first I’m going to create the directory, which I’m going to call backup (original or what?)

sudo mkdir /mnt/backup
Password:

Next I have to fire up my favourite editor called nano (you can of course use your favourite) and add the entry in the file system table.

sudo nano /etc/fstab
Password:

I’ll add the following to the file /etc/fstab being careful not to delete or change any of the setting that are already in there.

# /dev/sdb1
/dev/sdb1       /mnt/backup     ext3    user,noauto     0       0

The user option allows non root users to mount the filesystem, the noauto option ensures that the filesystem isn’t mounted at boot.

Test to see if you can mount the filesystem by typing the following

mount /mnt/backup
cat  /etc/mtab

You should get a list of all mounted filesystems similar (not exactly the same) to the list below:

billy@linux:~$ cat /etc/mtab
/dev/sda1 / ext3 rw,errors=remount-ro 0 0
proc /proc proc rw,noexec,nosuid,nodev 0 0
/sys /sys sysfs rw,noexec,nosuid,nodev 0 0
varrun /var/run tmpfs rw,noexec,nosuid,nodev,mode=0755 0 0
varlock /var/lock tmpfs rw,noexec,nosuid,nodev,mode=1777 0 0
procbususb /proc/bus/usb usbfs rw 0 0
udev /dev tmpfs rw,mode=0755 0 0
devshm /dev/shm tmpfs rw 0 0
devpts /dev/pts devpts rw,gid=5,mode=620 0 0
lrm /lib/modules/2.6.20-16-generic/volatile tmpfs rw 0 0
binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw 0 0
/dev/sdb1 /mnt/backup ext3 rw,noexec,nosuid,nodev,user=billy 0 0

Notice the last entry above /dev/sdb1 being the backup hardisk. To unmount the filesystem type the following:

umount /mnt/backup

Make sure that rsync is installed on your machine

Type rsync at the prompt, you should get the following:

rsync(1)                                                              rsync(1)

NAME
rsync – faster, flexible replacement for rcp

SYNOPSIS
rsync [OPTION]… SRC [SRC]… DEST

rsync [OPTION]… SRC [SRC][USER@]HOST:DEST

rsync [OPTION]… SRC [SRC][USER@]HOST::DEST

rsync [OPTION]… SRC [SRC]… rsync://[USER@]HOST[ :P ORT]/DEST

rsync [OPTION]… SRC

rsync [OPTION][USER@]HOST:SRC [DEST]

rsync [OPTION][USER@]HOST::SRC [DEST]

rsync [OPTION]… rsync://[USER@]HOST[ :P ORT]/SRC [DEST]

Which is the man or manual page, if you don’t have it you will need to install (this isn’t covered in the Howto, but if you are using Ubuntu or a derivative of Debian, and are connected to the internet then try the following command to install.

sudo apt-get install rsync

Create a bash file to mount the drive, backup files and unmount the drive.

I’ve created a bash file (based on the examples provided on the rsync website) to mount the hard drive, back up everything in the /home directory, then unmount the drive.

#!/bin/sh
#——————————————————-#
#       Filename backup.sh                              #
#                                                       #
#       Backs up the home directory to a second Hard    #
#       Disk, Remember to add the second hard disk to   #
#       /etc/fstab to enable it to work.                #
#       Also remember to use the "user,noauto"          #
#       options in fstab to enable it to work.          #
#       http://en.wikipedia.org/wiki/Fstab              #
#                                                       #
#       Located in /home/billy/                         #
#       Created on 07/03/2008 by Billy Dickson          #
#                                                       #
#       Base on a script found at the rsync website.    #
#——————————————————-#
# Exit on error
set -o errexit

export PATH=/usr/local/bin:/usr/bin:/bin

LIST="home"

for d in $LIST; do
mount /mnt/backup/
rsync -axv –delete /$d/ /mnt/backup/$d/
umount /mnt/backup/
done

Tuesday 25th May 2010

The above is now not used but kept as a reminder to myself, feel free to use it if you want it.

I now rsync my files to a second PC running linux which I’ve called orion. You first need to set up ssh keys for the host machine and backup machine before the script below will work.

# Exit on error
set -o errexit
export PATH=/usr/local/bin:/usr/bin:/bin

# Old command line below that was used when /mnt/backup/billy was mounted as a NFS drive.
# rsync -axv –delete /home/billy/ /mnt/backup/billy/

# New new command line below that is used to ssh to host orion and backup up there.
# Set up a secure shell key on this workstation and copied to orion.
rsync -avz –delete /home/billy/ billy@orion:/home/backup/billy

Creating a crontab to backup you files

Here’s a quick refresher on the crontab syntax

crontab: usage error: file name must be specified for replace
usage:  crontab [-u user] file
crontab [-u user] { -e | -l | -r }
(default operation is replace, per 1003.2)
-e      (edit user‘s crontab)
-l      (list user’
s crontab)
-r      (delete user‘s crontab)

Now I want to create a crontab that runs at 11:35am every day and backs up my files using rsync (I tend not to have my workstation on at night, you can of course change it to suit).

Create a crontab

crontab -e

What I’m doing here is calling the bash script that I wrote earlier, which copies all the files from the home directory to the new hardisk, you will of course change the syntax to suit your own file and directory structure. The crontab itself also created a log file called backup.log that can be checked to ensure that alls gone well.

# m h  dom mon dow   command
35 11 * * * $HOME/backup.sh > $HOME/backup.log

For Info: The “@gt;” should be a > (greater than) symbol for some reason it doesn’t want to show that.

More rsync examples are available on the rsync website here.

About billy

Senior IT Technician working in Edinburgh, Scotland.
This entry was posted in Linux, Personal and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>