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 #####################"

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...

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)

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"...

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!!

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 )

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

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..

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

Code:

tar lcz .

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.

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).

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:

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.