Howto: rsync a system between drives

A long, long time ago, someone mentioned to me that it was possible to zip up an entire installation, and transplant it into another hard drive. I only halfway believed it at the time, since it seemed rather far-fetched and I was but a newb (I still am). But it sat in the back of my mind like an emergency exit, in the odd chance that I might need to use it … even if it sounded preposterous.

But you know what? You can do it. It works fine. In fact in some situations, it’s easier than actually installing a full system.

I wanted to move the entire installation from my Thinkpad to a different hard drive. I was using the 120Gb Western Digital Scorpio that fell out of the sky into my hands, but I have a faster-yet-smaller hard drive — a 7200rpm 60Gb Hitachi — that I would prefer to use. I don’t need 120Gb of storage, and the space would be better spent in an rtorrent slave or in an external case.

But the Scorpio had a Crux installation on it — a six-month old console-only installation, and not one I was keen on rebuilding just to swap drives. I like the idea that it’s at least a half a year old, and that it hasn’t needed any maintenance other than regular updates.

Cloning was not an option, since the cloning tools I am familiar with don’t seem able to “scale down” an image to fit the drive — Clonezilla, which is a gift from heaven in the category of backup tools, gives me errors if the partitions don’t match quite right. And technically I don’t have space to mirror the drive to, since the only other drive I have is my storage volume, which is where the real-life important stuff lives.

So I began to wonder if I could really unload all the files, then copy them back, and still have a working system. I read it on the Internet, so it must be true. …

And this time it is. Here’s how:

First, offload all the files, by directory. I usually have a three-way partition split, with a separate /boot, root and /home. (Why? Don’t interrupt.) Jump into a live environment (any live CD will do) and make three new folders on a destination drive — in my case, it was the storage volume — and label them hda1, hda3 and hda4 … although really, the names don’t matter.

Mount them so you can write to them, maybe putting them in /media/…/hda1, hda3 and hda4. Just to keep things clear in your mind, you might want to make a /media/destination and /media/source folder, to avoid accidentally putting the wrong stuff in the wrong place.

Next, rsync is your friend. You’re three quick steps away from mirroring all the files.

rsync -a --progress /media/source/boot/ /media/destination/hda1/
rsync -a --progress /media/source/home/ /media/destination/hda4/

Now the last one is a little funny.

rsync -a --progress /media/source/ /media/destination/hda3/ --exclude=proc --exclude=sys

I left out files in /proc and /sys since most of those are generated by the running system, on the fly. And those trailing slashes are important. rsync is a really fantastic tool — one of my favorites — but it’s very precise about what you ask for. If you leave off a slash, it will insert an entire directory inside another, rather than making the two folders identical. Which is what it’s supposed to do, but not what we want.

One other thing: If you’re like me and you build your own kernels, remember that the kernel source code might only be 60Mb or so, but once it’s decompressed, configured, compiled and finished, that darn tree can take up an easy 500Mb. Before you go transferring your entire installation to an external drive across USB1.1 😯 , you might want to take out some of the unwanted stuff. I am speaking from experience here. 🙄

Wait patiently while the text scrolls by. … And once that’s finished, then you can swap the drive and reverse the process. I used the same partition arrangement with a slightly smaller /home directory, then rsync’d everything back across. It took a while, but after it was done and all the files were in place, I did a filesystem check and reinstalled grub (I use the command-line interface to install grub. I’m cool that way :mrgreen: ) and rebooted.

And what to my wondering eyes should appear … but the same system I was using a few hours earlier, with all the stuff still in place, all the files and all the network connections and framebuffer support and so forth and so on, just like I left it on the other drive. No boot problems, no file inconsistencies, no error messages, nothing. It didn’t even know it was on a different drive.

So it’s true, you can actually copy an entire Linux installation between drives, and it’ll work. If you feel daring and want to try it for yourself you can give it a whirl, but make sure you save all the important stuff before you begin. Remember, you read it on the Internet, so it might be true. … 🙄

6 thoughts on “Howto: rsync a system between drives

  1. anon

    I have used rsync this way several times, works great. You need to be careful with trailing slashes like mentioned above. Before doing the actual sync I will do a –dry-run[-n], just in case there is a typo somewhere or did not use trail slashes correctly.

    Reply
  2. lefty.crupps

    While I’ve done this before across machines, the reinstallation of GRUB is always a headache. Sometimes it will work, some times it won’t, some times it’ll say it worked but it really won’t… live CDs haven’t been able to help me in this regard either. The SimplyMEPIS LiveCD has a tool to reinstall GRUB (on Debian-based systems at least) which has served me well in the past to reinstall the GRUB bootloader when GRUB was unable to install itself.

    Reply
  3. Pingback: Links 29/12/2009: Google Nexus One and Netbook Details Leak | Boycott Novell

  4. mardson

    Look into partimage. I believe it has pretty awesome understanding of underlying file systems, and will intelligently consume the media (i.e. you don’t end up wasting disk space like i used to with dd … i’m not the brightest 🙂 ). Also, you can gzip or bz2 the files on the fly; pretty cool stuff.

    Reply
  5. Pingback: A console-only Ubuntu system « Motho ke motho ka botho

  6. Anton Eliasson

    I’m not entirely sure what you mean by your statements about Clonezilla – you don’t have to work directly partition-partition, you may just as well copy and compress the entire partition into a file, then swap out the harddrives and reverse the process. You will then have a clone of it. Also I like to use the -h switch with rsync to output lower numbers and higher prefixes, but I guess you have your reasons not to

    Reply

Leave a reply to lefty.crupps Cancel reply