View previous topic :: View next topic |
Author |
Message |
lakicsv n00b
Joined: 19 Apr 2002 Posts: 51 Location: UK, Surrey
|
Posted: Tue Dec 31, 2002 4:35 pm Post subject: "Clone" your Gentoo root partition |
|
|
Dear Gentooers,
Often times there are upgrades or changes which can potentially render you nicely configured system unsusable. If this disaster happened, it is a very good feeling to have the EXACT copy of your original root partition backed up and ready to use! I wrote a bash script which will clone your gentoo partition on another partition, so you are safe to do anything advetureous...
Why is the script? Isn't "cp -a / /mnt/clone" would be enough? Unfortunately, this would require to unmount separate partitions, like /home, and could cause problems with /dev partition which gentoo uses. Certain special files would also be left out. So, after a reading a long discussion on gentoo user list, I chosen the possibly safest solution (at least what I tried) and wrote a scrip around it to make it easy to use.
I have two (same sized) partitions for gentoo root. I also have /home on a separate partiton and I have one separate partition containing /usr/local (symlinked), /opt (symlinked), /var/tmp/portage (PORTAGE_TMPDIR set in make.conf), /usr/portage/distfiles (DISTDIR set in make.conf). Before I do something dangerous, I run the script to duplicate my root partition, and with this setup I leave out some bulky stuff (copying your distfiles) which normally is not changed that much.
The script has a "pretend mode", so you can see what will happen without any danger...
Here is the help taken from the script (you can also display it with "gent-clone -h")
"############## gent-clone help #####################"
This script will clone your gentoo root partition to "
another partition. Usage:"
gent-clone mode [destination mount point]"
where mode = "-c" Clones current root partition to a destination partition'
"-p" Prentend cloning; useful to see what would happen without any changes'
"-h" Prints this message...'
'Destination mount point should be given as /dir1/dir2 or /dir format, if it is not supplied, then the program uses internal defaults (can be changed at the beginning of the script). A partition should be attached to the destination directory or the mount point listed in /etc/fstab before using the script! Normally, the /etc/fstab file on your root partition will be edited by the script: A new adjusted /etc/fstab is created and copied over - this contains your destination partition mounted as root. If you do not want this behavior, set the FSTAB_FLAG variable from 1 to 0 by editing the first part of this script.
Examples:
gent-clone -c /mnt/clone
-->clone you root partititon to the partition mounted on /mnt/clone '
gent-clone -c
-->clone your current root partition to your predefinied'
partition (set this up by editing the first part of the script)
gent-clone -p
--> Pretend mode: Prints out what will happen when you issue a -c argument'
I call this file as gent-clone. Copy it to an empty file and save it.
I hope someone finds this helpful, I certainly used it regularly...
Cheers: Viktor
Code: |
#!/bin/bash
# This script will clone the root partition of your gentoo system to a selected
# partition and create the appropriate fstab file for the cloned partition
#################################################################################
# Change these variables according to your prefs, for CLONE_DIR always use a path
# like /dir1/dir2, never something like /dir or /dir1/dir2/dir3...
# CLONE_DIR either have to be defined in /etc/fstab, or mounted...
# BIND_DIR is a directory to mount - bind your root partition (it is needed for your
# gentoo system which uses devfs - otherwise /dev will not be correctly copied...
# If you do not want to change the fstab file for your cloned partition, set
# FSTAB_FLAG to 0...
BIND_DIR=/mnt/oldroot
CLONE_DIR=/mnt/clone
FSTAB_FLAG=1
#################################################################################
# Do not change these:
SOURCE_DIR=/
CREATE_BIND_DIR=0
MOUNT_CLONE_DIR=0
###################### Subroutines ##############################################
#_____________________________________________________________________________
Check_params()
# Checks whether your destination partition is mounted or exist in /etc/fstab
# and checks for the need to create mount points
{
SOURCE_PART=`grep "$SOURCE_DIR\ " /etc/mtab | awk '{ print $1 }'`
grep "$CLONE_DIR\ " /etc/mtab &> /dev/null
if [ $? -ne 0 ]
then
grep "^[\ \t]*.*$CLONE_DIR\ " /etc/fstab &> /dev/null
if [ $? -ne 0 ]
then
echo
echo $CLONE_DIR is not listed in /etc/fstab as a valid mount point. Please either
echo list it in your fstab, so it can be automatically mounted, or mount a partition
echo on it !
echo
Usage
exit 1
else
CLONED_PART=`grep "^[\ \t]*.*$CLONE_DIR" /etc/fstab | awk '{ print $1 }'`
if [ ! -d $CLONE_DIR ]
then
mkdir $CLONE_DIR
fi
MOUNT_CLONE_DIR=1
fi
else CLONED_PART=`grep "$CLONE_DIR" /etc/mtab | awk '{ print $1 }'`
fi
if [ ! -d $BIND_DIR ]
then
CREATE_BIND_DIR=1
fi
}
#_____________________________________________________________________________
Usage() # Prints some help
{
echo
echo "############## gent-clone help #####################"
echo
echo "This script will clone your gentoo root partition to "
echo "another partition. Usage:"
echo
echo "gent-clone mode [destination mount point]"
echo
echo 'where mode = "-c" Clones current root partition to a destination partition'
echo ' = "-p" Prentend cloning; useful to see what would happen without any changes'
echo ' = "-h" Prints this message...'
echo
echo 'Destination mount point should be given as /dir1/dir2 or /dir format, if it is not supplied, '
echo 'then the program uses internal defaults (can be changed at the beginning of the script).'
echo 'A partition should be attached to the destination directory or the mount point listed in'
echo '/etc/fstab before using the script! '
echo
echo 'Normally, the /etc/fstab file on your root partition will be edited by the script:'
echo 'A new adjusted /etc/fstab is created and copied over - this contains your destination'
echo 'partition mounted as root. If you do not want this behavior, set the FSTAB_FLAG variable'
echo 'from 1 to 0 by editing the first part of this script.'
echo
echo
echo 'examples: gent-clone -c /mnt/clone - clone you root partititon to the partition mounted on /mnt/clone '
echo ' : gent-clone -c - clone your current root partition to your predefinied'
echo ' partition (set this up by editing the first part of the script)'
echo ' : gent-clone -p - Pretend mode: Prints out what will happen when you issue a -c argument'
}
#_______________________________________________________________________
Config() # Create /etc/fstab for cloned partition
{
cp /etc/fstab /etc/fstab.cloned
SLICE_CLONED=`echo $CLONED_PART | cut -d "/" -f3`
SLICE_SOURCE=`echo $SOURCE_PART | cut -d "/" -f3`
SED_FROM='^[\ \t]*\/dev\/'$SLICE_CLONED
SED_TO='\/dev\/clonepart'
cat /etc/fstab.cloned | sed -e "s/$SED_FROM/$SED_TO/" > /etc/fstab.cloned
SED_FROM='^[\ \t]*\/dev\/'$SLICE_SOURCE
SED_TO='\/dev\/rootpart'
cat /etc/fstab.cloned | sed -e "s/$SED_FROM/$SED_TO/" > /etc/fstab.cloned
SED_FROM='^\/dev\/clonepart'
SED_TO='\/dev\/'$SLICE_SOURCE
cat /etc/fstab.cloned | sed -e "s/$SED_FROM/$SED_TO/" > /etc/fstab.cloned
SED_FROM='^\/dev\/rootpart'
SED_TO='\/dev\/'$SLICE_CLONED
cat /etc/fstab.cloned | sed -e "s/$SED_FROM/$SED_TO/" > /etc/fstab.cloned
cp /etc/fstab.cloned $CLONE_DIR/etc/fstab
}
#_________________________________________________________________________
Clone() # Copies $SOURCE_PART to CLONED_PART
{
if [ $CREATE_BIND_DIR -eq "1" ]
then
mkdir $BIND_DIR
fi
if [ $MOUNT_CLONE_DIR -eq "1" ]
then
mount $CLONE_DIR
fi
mount --bind $SOURCE_DIR $BIND_DIR
rm -rf $CLONE_DIR/*
cd $BIND_DIR
find -mount -print | cpio -pdm $CLONE_DIR
if [ $FSTAB_FLAG -eq "1" ]
then
Config
fi
echo
echo "All done. Partition $SOURCE_PART was cloned to partition $CLONED_PART."
}
#____________________________________________________
Report() # What will happen; Used in pretend mode...
{
echo
if [ $CREATE_BIND_DIR -eq "1" ]
then
echo Creating $BIND_DIR...
fi
if [ $MOUNT_CLONE_DIR -eq "1" ]
then
echo Mounting $CLONED_PART on $CLONE_DIR...
fi
echo 'Mounting '$SOURCE_DIR' ('$SOURCE_PART') with -bind on '$BIND_DIR'...'
echo 'Deleting all files in '$CLONE_DIR' ('$CLONED_PART')...'
echo 'Changing dir to '$BIND_DIR'...'
echo 'Copying everything in '$BIND_DIR' ('$SOURCE_PART') to '$CLONE_DIR' ('$CLONED_PART')...'
echo
if [ $FSTAB_FLAG -eq "1" ]
then
echo "Creating and copying a modified fstab, where $CLONED_PART is mounted as / ..."
fi
}
######################### Main Program Module ##################################
case "$#" in
0)
Usage
exit 1
;;
1)
case "$1" in
-p)
Check_params
Report
exit 0
;;
-c)
Check_params
Clone
exit 0
;;
-h)
Usage
exit 0
;;
*)
echo
echo "Unknown parameter..."
echo
Usage
exit 1
;;
esac
;;
2)
case "$1" in
-p)
CLONE_DIR=$2
Check_params
Report
exit 0
;;
-c)
CLONE_DIR=$2
Check_params
Clone
exit 0
;;
-h)
echo
echo 'The "-h" parameter cannot be used with additional arguments...'
echo
Usage
exit 1
;;
*)
echo
echo "Unknown parameter..."
echo
Usage
exit 1
;;
esac
;;
*)
echo
echo 'This 2 is the maximum number of params!'
echo
Usage
exit 1
;;
esac
############### End #####################################
|
|
|
Back to top |
|
|
pilla Bodhisattva
Joined: 07 Aug 2002 Posts: 7729 Location: Underworld
|
Posted: Tue Dec 31, 2002 6:18 pm Post subject: |
|
|
Works for me. Just don't forget to verify the fstab in the new partition before booting. _________________ "I'm just very selective about the reality I choose to accept." -- Calvin |
|
Back to top |
|
|
lakicsv n00b
Joined: 19 Apr 2002 Posts: 51 Location: UK, Surrey
|
Posted: Tue Dec 31, 2002 9:14 pm Post subject: |
|
|
What do you mean by verifying? The script creates and copies the new fstab file where the destination partition is mounted as root...
Didn't it wok for you?
Viktor |
|
Back to top |
|
|
pilla Bodhisattva
Joined: 07 Aug 2002 Posts: 7729 Location: Underworld
|
Posted: Tue Dec 31, 2002 9:43 pm Post subject: |
|
|
The partition wasn't in the fstab (which is allowed by your script). Anyway, you never know.... _________________ "I'm just very selective about the reality I choose to accept." -- Calvin |
|
Back to top |
|
|
kram n00b
Joined: 29 Nov 2002 Posts: 35
|
Posted: Wed Jan 01, 2003 1:32 am Post subject: |
|
|
I do this:
cp -ax / /mnt/clone
chroot /mnt/clone /bin/bash
emerge baselayout
and then that partition is bootable, works perfect for me ... is their a reason why this script is better than my method? |
|
Back to top |
|
|
lakicsv n00b
Joined: 19 Apr 2002 Posts: 51 Location: UK, Surrey
|
Posted: Wed Jan 01, 2003 10:08 am Post subject: |
|
|
I could think of a couple of things:
1. /dev does not get copied (since it is mounted as a dev file system). Doing an emerge baselayout (I think this is supposed to create your dev as well) does not give you an EXACT replica of your root dir...
2. If you have your DISTDIR in a separate partition, you will run into problems: emerge baselayout will fail since in the chroot only one partition is mounted...
3. You have to manually edit /etc/fstab to reflect the changes when you boot your new partition
4. Convenience features: the script will mount your dirs for you, checks against mistakes
My script is intended to deal with all this, making this cloning as automatic as possible. If you dedicate a partition, you can even do it from a cronjob if you want (But I rather do it when it is needed, i.e. before major upgrades: I usually do non-security related upgrades weekly on my workstation - desktop machine)
Cheers: Viktor |
|
Back to top |
|
|
c_kuzmanic Guru
Joined: 18 Apr 2002 Posts: 488 Location: Los Angeles , California
|
Posted: Tue Jan 21, 2003 9:34 pm Post subject: |
|
|
Great script - saved me some work, thanks a million:)
My root partition is on a raid0 array, I should have no problem using your script though, correct ? |
|
Back to top |
|
|
BlackBart Apprentice
Joined: 07 Oct 2002 Posts: 252
|
Posted: Wed Jan 22, 2003 3:33 am Post subject: |
|
|
could you add support to instead of coppying it to compress everything into a file? |
|
Back to top |
|
|
Dimitri Guru
Joined: 24 Jul 2002 Posts: 373 Location: Niederbayern/Germany
|
|
Back to top |
|
|
water Guru
Joined: 19 Jun 2002 Posts: 387 Location: Zierikzee, The Netherlands
|
Posted: Wed Jan 22, 2003 9:50 am Post subject: |
|
|
I have made a backup using Mandrake (also somewhere on my harddisk). Not very beautifull, but it works. _________________ Groeten uit Holland |
|
Back to top |
|
|
Freak_NL Apprentice
Joined: 27 Jun 2002 Posts: 261 Location: The Netherlands
|
Posted: Thu Jan 23, 2003 8:55 pm Post subject: |
|
|
An alternative way to do backup your partition into a compressed file is this:
Code: | tar -X tar.exclude -cvjpf backup.tar.bz2 /* |
With this being an example tar.exclude file:
Code: | /mnt
/tmp
/usr/src
/usr/portage/distfiles
/var/tmp
/proc |
Be sure to put the path for the backup tar.bz2 archive somewhere on a different partition then your / partition.
Also, I've found Linux Live-CD's (like Knoppix for instance) ideal for restoring backups. |
|
Back to top |
|
|
kappax Apprentice
Joined: 30 Aug 2002 Posts: 273 Location: The Moon
|
Posted: Sun Feb 02, 2003 6:09 am Post subject: |
|
|
much much simpler way would be.
mount drive to wich would hold the backup
Code: |
mount /dev/hdc3 /backup
|
then copy files over
Code: |
rsync -va --deleate --exclude=/proc/* --exclude=/dev/* --exclude=/backuo/* / /baclup
|
then have some code to change the fstab, and add the entry in to your grub config. all done.
then later you can add the -u option to rsync and just update what has changed. _________________ My Box
glxgears - 4083.400 FPS
OS: GNU/Linux
Distro: Gentoo
kernel: 2.6.0-test9-mm2
----------------------
vi makes me :wq in word pad |
|
Back to top |
|
|
lakicsv n00b
Joined: 19 Apr 2002 Posts: 51 Location: UK, Surrey
|
Posted: Sun Feb 02, 2003 8:11 am Post subject: |
|
|
If you look at my original code (I started this thread), you will realize that the whole script could be done in two actual lines in it. 99% of it is checking the parameters, printing out docs making it as automatic as possible.
So by the time your one-liner would do similar things that would be just as complicated and not "much much simpler"...
Just my 2 cents...
Viktor |
|
Back to top |
|
|
garo Bodhisattva
Joined: 15 Jul 2002 Posts: 860 Location: Edegem,BELGIUM
|
Posted: Mon Feb 03, 2003 10:27 am Post subject: |
|
|
i just reboot with a live cd (i like knoppix), and do :
Code: | mount /dev/hda3 /mnt/hda3 && dd if=/dev/hda2 | bzip2 > /mnt/hda3/backuphda2.bz2 |
hda2 is my "/" and hda3 is a partition i use for my backups _________________ My favorite links this month:
- Surf Random
- Web-based SSH
- Stop Spam |
|
Back to top |
|
|
modal Apprentice
Joined: 02 Oct 2002 Posts: 277
|
Posted: Sun Feb 09, 2003 5:39 am Post subject: |
|
|
Or, you could just use EVMS to make a RAID 1 configuration on separate partitions, and constantly have it backed up...although, this script is great if you are going to do something daring like...shhh, emerge crazy-.9-alpha!!
|
|
Back to top |
|
|
dalu Guru
Joined: 20 Jan 2003 Posts: 530
|
Posted: Sun Feb 09, 2003 7:12 pm Post subject: |
|
|
your script doesn't work for me, it prints some errors.
Code: |
./copydrive: line 21: : command not found
./copydrive: line 177: syntax error near unexpected token `)'
./copydrive: line 177: ` -p) '
|
i named it copydrive |
|
Back to top |
|
|
iplayfast l33t
Joined: 08 Jul 2002 Posts: 642 Location: Cambridge On,CA
|
Posted: Tue Feb 11, 2003 5:17 am Post subject: |
|
|
Ok, all you hotshots. I've got a question. If I've got two computers networked together. One fully loaded with disks (that I'm not going to take apart) the other with a 5gb virgin disk waiting to be filled...
can I use knoppix on both computers and somehow ssh from one to the other and copy the root file system? (I've tried but failed miserably )
Or is there a better way? |
|
Back to top |
|
|
Delphiki Guru
Joined: 04 Oct 2002 Posts: 337 Location: A2
|
Posted: Tue Feb 11, 2003 6:08 am Post subject: |
|
|
Here's how I do this (and I've had to do it a couple times).
find / -xdev | cpio -pvdm /destdir
Works just fine for me. _________________ Excellent.. |
|
Back to top |
|
|
iplayfast l33t
Joined: 08 Jul 2002 Posts: 642 Location: Cambridge On,CA
|
Posted: Wed Feb 12, 2003 2:34 am Post subject: |
|
|
Delphiki wrote: |
find / -xdev | cpio -pvdm /destdir
|
I think I'm being slow. Let's suppose I've got two computers booted under knoppix. I ssh from one to the other and they are talking.
On the virgin computer ssh'd to the gentoo computer, I mount something like Code: | mount /dev/hda2 /mnt/srcdir |
On the virgin computer I Code: | mount /dev/hda2 /mnt/destdir |
Now where does this find come into play? |
|
Back to top |
|
|
Delphiki Guru
Joined: 04 Oct 2002 Posts: 337 Location: A2
|
Posted: Wed Feb 12, 2003 7:15 pm Post subject: |
|
|
Quote: | Now where does this find come into play? |
find /mnt/srcdir -xdev | cpio -pvdm /mnt/destdir
I wasn't specifically answering your question, but rather an alternative to the script posted, but this should work as long as you've got both drives mounted. _________________ Excellent.. |
|
Back to top |
|
|
iplayfast l33t
Joined: 08 Jul 2002 Posts: 642 Location: Cambridge On,CA
|
Posted: Wed Feb 12, 2003 7:29 pm Post subject: |
|
|
Delphiki wrote: |
I wasn't specifically answering your question, but rather an alternative to the script posted, but this should work as long as you've got both drives mounted. |
Ooops. Oh well, as long as I have you on the line, how can you mount a drive across ssh then? So that on one computer I've got
/mnt/srcdir
/mnt/destdir
Is that possible? |
|
Back to top |
|
|
Delphiki Guru
Joined: 04 Oct 2002 Posts: 337 Location: A2
|
Posted: Wed Feb 12, 2003 10:26 pm Post subject: |
|
|
Accross ssh? I don't believe so. You would need Samba, NFS, or something along those lines for that. Knoppix might be able to do these but I wouldn't know as I've never used it. _________________ Excellent.. |
|
Back to top |
|
|
iplayfast l33t
Joined: 08 Jul 2002 Posts: 642 Location: Cambridge On,CA
|
Posted: Thu Feb 13, 2003 4:32 am Post subject: How to backup from one machine to another |
|
|
Found it. Much simpler then I expected.
The situation:
Machine one is a working gentoo system.
Machine two is an empty box.
A knoppix cd is available.
step 1.
Clean off any unneeded files.
My list is:
/var/tmp/portage/work/*
/usr/portage/distfiles/*
There's probably more, but that gave me 1 gig that I didn't have to transfer.
step 2.
boot machine 2 with the knoppix cd.
open a terminal.
Code: | sudo su
passwd abc
cd /mnt
mount /dev/hda2 hda2
ifconfig
|
If all is well you will see the ip address of your tcp port. In my case it was 192.168.1.101
Step 3.
on machine 1 (the gentoo box) open a terminal
Code: |
su -
password
cd /
tar lcz . | ssh -l root 192.168.1.101 'tar xz -C/mnt/hda2/'
|
you will be asked for root's password on machine 1 (abc)
once this is finished, you have transferred the / directory. (I'll leave the /boot directory as an exercise
To explain what this does (for the unfamiliar)
tar lcz . | ssh -l root 192.168.1.101 'tar xz -C/mnt/hda2/'
the first part creates a tar file which will be spewn out to wherever. Stdout (the console) is the default, but with the beauty of pipes we can put it elsewhere.
The parameters lcz are:
l=local file system only. (no need to worry about /proc, /dev /mnt)
c is create a new archive,
z is compress.
this all gets piped to ssh
Code: | | ssh -l root 192.168.1.101 |
ssh is the transport mechanisum. It asks for the password and then executes
Code: | 'tar xz -C/mnt/hda2/' |
which extracts the archive spewing (I like that word) out of ssh, uncompresses it, and puts it into /mnt/hda2 (which we mounted in step 2).
It's also possible to open up another terminal and ssh to machine 1 to take a gander at how things are going. |
|
Back to top |
|
|
delta407 Bodhisattva
Joined: 23 Apr 2002 Posts: 2876 Location: Chicago, IL
|
Posted: Fri Feb 14, 2003 3:38 pm Post subject: |
|
|
Delphiki wrote: | Accross ssh? I don't believe so. | SSH can do marvelous things. I've transparently linked three disparate networks over SSH -- `ssh` gives you a bidirectional means of communication (stdin, stdout) so you can run pretty much anything (including pppd).
Again, SSH can do marvelous things. _________________ I don't believe in witty sigs. |
|
Back to top |
|
|
john Doe n00b
Joined: 28 Jan 2003 Posts: 31
|
Posted: Fri Feb 21, 2003 5:53 pm Post subject: |
|
|
hi guys, i have some problems with this script. (well i have the same error even with rsync metod listed above)
i have my gentoo root partition on /dev/hdd7, i installed gentoo here cause i wanted just to give it a try, but right now i've seen thet gentoo rocks :p, so i want to move to hda1 (hda is faster than hdd)
ok, i've run this script. All files seems to be copied to hda1, i've modified fstab to match new root on hda1 and grub.conf to add newroot entry:
Code: |
root (hd0,0)
kernel=/boot/bzImage <parameters here>
initrd=/boot/initrd
|
Btw, When i try to boot hda1 partition, it instead boot hdd7... i mean that when i arrive on login if i type #mount i see:
/dev/hdd7 on / type reiserfs (rw,noatime,notail)
...
instead of /dev/hda1 as expected. I've found no way to boot hda1.
I've tried even to phisically unplug hdd drive, and when i boot (from a grub boot floppy-disk) hda1 it gives me an error on VFS. I don't remeber exactly which.
Some hints?
Thanks, and sorry for my poor english
john |
|
Back to top |
|
|
|