Despite the best planning, it eventually happens. You start to run out of space on your linux system. While there are many different ways to copy files from one hard drive to another, some work better than others when transferring the full / directory. Below I will outline a method I have used about 50 times to transfer full systems and partitions from one machine to another.
Why this can be complicated
When you are copying a full linux system from one partition to another, there are a few issues you need to consider:
What to do
With all these in mind, the best method I've found for copying the / filesystem is as follows (assuming for the example the new drive is mounted at /mnt/temp):
find / -xdev -print0 | cpio -pa0V /mnt/temp
When you run this command, it will recursively copy everything on the / filesystem, without crossing over into other mounted partitions. It will properly handle any special files, and it will completely preserve permissions.
If you have other filesystems mounted on other partitions, you do something somewhat similar. Say I had /boot mounted on a different filesystem, and I wanted to back it up onto /mnt/temp:
cd boot; find ./ -xdev -print0 | cpio -pa0V /mnt/temp
You simply would mount each new partition you want to copy onto, to /mnt/temp and repeat this above operation for each of those. Upon execution, you will see a series of dots fly across the screen. Each dot represents a file that is being copied. I often will go to another terminal and run watch df
to watch the progress.
After all the filesystems have been copied, you probably won't actually have a bootloader. What I usually do, is go ahead and move the new drive into the place of the old one, and boot up on a Knoppix disc. Then I mount the new drive, say at /mnt/hda1, and then run grub-install --root-directory=/mnt/hda1 /dev/hda
to copy my new bootloader to the new hard drive. You could probably do something similar just from your old install.
Backup and Restore
You can also use this method for a backup and restore. For example, say you want to back up your current / on machine1 onto a different Linux machine, machine2, temporarily at /home/foo/drivebackup/ (to resize your current partition or perhaps to format to a new filesytem). First take the drive from machine2 to machine1, mount machine2's drive at /mnt/temp, and run:
find / -xdev -print0 | cpio -pa0V /mnt/temp/home/foo/drivebackup
Once you do whatever it is you need to do on machine1, take the hard drive to machine2 (as repartitioning or formatting would have blanked it obviously) mount that hard drive on /mnt/temp and then run:
cd /home/foo/drivebackup; find ./ -xdev -print0 | cpio -pa0V /mnt/temp/
This will copy all the data back onto your old drive.