GNU Linux


HOWTO - How I do my backups AND get it restored again


There should be a blue line here

Table of Contents

There should be a blue line here

1. HOWTO Backup/Restore with RAID and LVM

This is how I do it.

To backup is simplicity itself, as it is Linux and not M$ Windows we just need to tar/zip up the files and it's job done! I use this script and run it with sudo backup.sh.

Code:
#!/bin/bash 
 
#################################################### 
# 
# Setup some environment variables to save time later 
# if I needto change destination or type of backup, Just change the varaible 
# once and all the commands change. Saves making mistakes  
# 
#################################################### 
DEST=/data          # destination dir path, where the data is going to be stored 
MACH=`hostname`     # Find hostname of the machine being backed up 
DIR=/backup_$MACH   # subdirectory where the backup is to be stored 
 
TAR_CMD="sudo tar"  # the command to create the tar file 
TAR_OPT=zcvpf       # command line switches for tar command 
TAR_EXT=.tgz           # file extension we we knowe thatit is a tar gzip file. 
 
TAR_EXCL="--exclude-from=/home/richard/bin/exclude-from" 
 
# if the mounted drive where the backup file will be stored does not exist, mount it 
if [ ! -d $DEST$DIR ] ; then 
   sudo mount -a 
   mkdir $DEST$DIR 
fi 
 
#################################################### 
# 
# copy some files I may need. it just keeps them handy. 
# they also come in handy when playing around on another machine. (Good reference material) 
# 
#################################################### 
cd /home/richard/bin 
cp *_${MACH}.sh exclude-from $DEST$DIR 
 
cd /etc 
cp fstab exports samba/smb.conf $DEST$DIR 
 
cd /boot/grub 
cp menu.lst $DEST$DIR 
 
#################################################### 
# 
# Now to start the Backup process 
# 
#################################################### 
cd / 
 
FILE=root 
$TAR_CMD $TAR_OPT $DEST$DIR/${MACH}_$FILE$TAR_EXT $TAR_EXCL \ 
           ./bin ./cdrom* ./etc ./initrd* ./lib* ./root ./sbin ./srv ./vmlinuz* & 
 
for FILE in home boot var usr 
do 
   $TAR_CMD $TAR_OPT $DEST$DIR/${MACH}_$FILE$TAR_EXT ./$FILE $TAR_EXCL $BACK 
done 
 
cd /data 
for FILE in popfile wine www 
do 
   if [ -d $FILE ] ; then 
      $TAR_CMD $TAR_OPT $DEST$DIR/${MACH}_$FILE$TAR_EXT $TAR_EXCL  ./$FILE 
   fi 
done 
 
cd $DEST$DIR 
for FILE in root boot home usr var popfile wine www 
do 
  if [ -e ${MACH}_$FILE$TAR_EXT ]; then 
     sudo chown richard:richard ${MACH}_$FILE$TAR_EXT 
  fi 
done 
 
 
echo All Done... 

Line-by-line description:

directory you only have to change in one place. Saves unnecessary problems if you miss an occupancies.

That is all there is to the backup. You will need to change which directories you backup and where you put the generated files.

There should be a blue line here

2. HOWTO restore my backup

The restore is a little bit tricky. So pay attention.

I am assuming we are putting back the backups to empty disks. The first problem is how do we get booted up. I use the system rescue CD. It has everything I need and it's updated regularly. Home page for SystemRescueCD

Place the CD into your CD/DVD player, you can boot from it can't you? (You may needto tweak some BIOS settings if you cannot.

Once presented with the command line to boot up the SystemRecuseCD I normally use something like this

Example
rescue64 dolvm2 

After a minutes or two your machine will spring into life. If you want a pretty Xwindows GUI interface use

Example
startx 

2.1. Partition the blank disks.

Use gparted to create blank partitions as necessary. Remember for LVM logical partitions do not put a filesystem on the partition, Yet!

For my system, 3x250Gb hard drives, dual booting with that other OS. I always put the MS-doz on the first partition on the drive and make it a Primary. So my three drives looked like this when this was written.

With the partitions all created we can create the LVM stuff. From the above i will have three partition all with around 209 Gb of free space. Now to put these all together as one striped partition (RAID0) of around 627Gb.

2.2. Create the physical volumes

We need to initialise the free partitions on the three disks so they can be used by LVM. It is a little like formatting a drive. (All data will be lost, although after using gparted they are most probably blank anyway.

Use the following to see a list of all block devices and remind yourself of the device name for the partitions to use.

Code:
sudo lvmdiskscan 

Code:
sudo pvcreate /dev/sda6 /dev/sdb5 /dev/sdc5 

You can use the following commands to see the physical volumes once they havw been created.

Code:
sudo pvscan 
sudo pvdisplay 

2.3. Create a Volume group

We want to create one big new LMV volume group from the three physical volumes we just initialised above. to create a volume group called vg01 out of the physical volumes /dev/sda6 /dev/sdb5 /dev/sdc5. You may need to change the device names. Run the command below and you should have an Uncle Bob. *grin*

Code:
sudo vgcreate vg01 /dev/sda6 /dev/sdb5 /dev/sdc5 

To see the volume group you just created use these commands

Code:
sudo vgs 
sudo vgdisplay 

Sometimes when you boot from the SystemRescureCD it does not make the LVM volume group available. This is not a big problem as you can do it yourself with the following command

Code:
sudo vgchange -a y vg01 

2.4. Create the logical volumes (within the Volume group)

Now for the good part where we create the logical volumes that we will then put a file system on and mount as root, usr home etc.


Note: It is really very simple to make LVM logical volumes bigger and if you use reisersfs you can do it while the file system is mounted and even in use at the time. (Although it's better not to be copying data to it)

To create a root partition of

Code:
sudo lvcreate -L 5G -i2 -I32 -n lv_root vg01 

Repeat for the other logical volumes These are what I usually start with and go from there.

Size Striping Block Name VolumeG
5G -i2 -I32 -n lv_usr vg01
5G -i2 -I32 -n lv_var vg01
5G -i2 -I32 -n lv_home vg01
50G -i2 -I32 - n lv_data vg01

There are some nice commands to display the logical volumes just to make sure. The first command will show how name stripes you are using.

Code:
sudo lvs -o +stripes 
sudo lvdisplay 

and also to see logical groups and how they are setup.

Code:
sudo lvs 
sudo lvdisplay 

As with volume groups the logical volumes sometines end up unavailable this can be fixed with the following.

Code:
sudo lvchange -a y /dev/vg01/lv_root 

2.5. Finally putting a file sytem on.

Almost there we need to put a file system on the plogical volumes we just created. I like to use a lable for each partition. I find it easier to work out which partition is which in the fstab file. Which do you prefer

Code:
LABEL=root   ... 
UUID=34a54a33-3b43-f53d45345 ... 

To format the logical partitions

Code:
sudo reisersfs -lable root /dev/vg01/lv_root 
sudo reisersfs -lable home /dev/vg01/lv_home 
sudo reisersfs -lable var  /dev/vg01/lv_var 
sudo reisersfs -lable usr  /dev/vg01/lv_usr 
sudo reisersfs -lable data /dev/vg01/lv_data 

2.6. Mounting the logical partitions

Mount the partitions so that you can get to each mount point to restore the data as necessary. In the root partition make the following as they are mount points or are not backed up. Also tmp needs the sticky bit set.

Code:
sudo mkdir -p /mnt/root /mnt/home /mnt/usr /mnt/var /mnt/data 
sudo mount /dev/vg01/lv_root  /mnt/root 
sudo mount /dev/vg01/lv_home  /mnt/home 
sudo mount /dev/vg01/lv_usr   /mnt/usr 
sudo mount /dev/vg01/lv_var   /mnt/var 
sudo mount /dev/vg01/lv_data  /mnt/data 
 
cd /mnt/root 
sudo mkdir -p boot data dev home mnt opt proc sys tmp usr var /run var/lock /media/c_win  /media/d_win  /media/e_win 
sudo chmod ag+w,a+t tmp 

2.7. Restore the data back to the partitions

For the root partition you need to be in /mnt/root when you issue the untar command. For all other partitions you chould cd to /mnt and then untar the data.

For root only
cd /mnt/root 
sudo tar zxpf /opt/linux32_data/linux60_root.bz2 

for all other partitions
cd /mnt 
sudo tar zxpf /opt/linux32_data/linux60_usr.bz2 

Check that fstab is correct and pointing at the right place for each mount point. Do the same for /boot/grub/menu.lst

2.8. Now to put grub back on.

I am going to give you the commands and then I will explain them later.

Grub settings for:
linux32 it is 'root (hd1,0)'  & 'setup (hd0)'. 
linux60 it is 'root (hd0,1)'  & 'setup (hd0)'. 

Ok, now to explain, what is going on. You need to open a grub shell, this will get you a "grub>" prompt.

Code:
sudo grub 

At grub>. enter these commands to find where the grub stage1 files is stored.

Code:
find /boot/grub/stage1 

This will return a location. If you have more than one, select the installation that you want to provide the grub files. Next, THIS IS IMPORTANT, whatever was returned for the find command use it in the next line (you are still at grub>. when you enter the next 3 commands)


Note: When setting root (XXXX) or setup (XXX) (hd0,0) means the first drive and the first partition. (hd2,1) means the third drive and the second partition). For setup (hd0) with no drive means the MBR and not a boot sector of a drive.

Set root to point to where you grub stage1 files are and setup will write the boot sector files to either the MBR or the boot sector you choose. Again use the value from the find command i.e. if find returned (hd0,1) then you would enter root (hd0,1)

Code:
root (hd1,0) 

Next enter the command to install grub to the mbr

Code:
setup (hd0) 

Finally exit the grub shell

Reboot.

There should be a blue line here

3. HOWTO Back up with Partimage for backing up Windows

The documentation is really bad for Partimage, only tells you the obvious stuff :-(

Boot with the SystemRescue disk then run net-setup eth0 to setup the network.

Code:
net-setup:    eth0 
Ip:           192.168.0.7 
Gateway:      192.168.0.1 
Names server: 192.168.0.5 
Bcast:        192.168.0.255 
Mask:         255.255.255.0 
DNS:          212.69.36.3 & 212.69.40.3 

ifconfig to check everything is sorted out.

Try ping 192.168.0.5 just to check we have them talking ok. Make a directory to mount the remote directory where we are going to store the image and then mount the remote directory.

Code:
mkdir /backup 
mount 192.168.0.5:/data -t nfs /backup 

save image to /backup/disk_image/some_name no need for an extension as .000 , .001 will be added as the image is created.

press f5, f5 enter description if required, ok the experimental thingy. and you are off and imaging.

Partimage is really very good, better than Ghost4linux which I found to be unreliable and painfully slow very much like the commercial program with a similar name. Partimage produces smaller images but is a little slower than Acronis.