Migrating Filesystems with rsync (updated with alternative dd command syntax)

I was answering a question in ask.fedoraproject.org, and I scavenged from the Internet this process to migrate an entire root directory tree structure to a new filesystem using the powerful and ever-useful rsync command.  I’m posting it here purely as an academic exercise and if anyone has comments or can try it out and report success or failure, that’d be awesome:

Now, let me preface this as such: I wouldn’t do this unless I REALLY had to. I’d rather have a cleanly-installed OS. But, if you must, this may work:

First, make yourself a bootable USB (or other media) of Fedora 20 so you can enter rescue mode and reinstall GRUB later in the process. http://docs.fedoraproject.org/en-US/Fedora/13/html/Installation_Guide/s1-rescuemode-boot.html provides instructions on how to do that.

After you’ve booted into your current system (not using the rescue disk; that’s for later), you can add a simple ext4 volume to the disk you intend to use for your new root filesystem using whatever interface is familiar to you. Get the UUIDs for your disks (note the values in the symbolic link files listed in /dev/disk/by-uuid) and be ready to map them to your mount locations in your /etc/fstab file on the new OS disk. Print your fstab file if you need to.

Once that’s done,

Mount the disk with the target filesystem somewhere in your directory tree:

# mkdir /mnt/newOS/
# mount /dev/mapper/diskname /mnt/newOS/

Copy everything from root into that directory, excluding the virtual filesystems /dev, /proc, and /sys, and the mount filesystem /mnt:

# rsync -aPEHv --exclude=/dev --exclude=/proc --exclude=/sys --exclude=/mnt --exclude=/var/cache/apt/archives/ / /mnt/newOS/

The -a option puts rsync in archive mode, which is just shorthand for the following options: The -r option, instructing the rsync operation to recurse directories. The -l option, instructing rsync to copy symbolic links as symbolic links The -p option, instructing rsync to preserve permissions The -t option, instructing rsync to preserve modification timestamps on the files The -o option, instructing rsync to preserve the owners of the files The -g option, instructing rsync to preserve the group owners of the files And the -D option, which is short hand for: The –devices option, instructing rsync to preserve device files The –specials option, instructing rsync to preserve special files

The -P option shows progress during the transfer operation.
The -H option tells rsync to look for hard-linked files in the source and link together the corresponding files on the destination. The -v option enables verbose logging.

Then make mountpoints in your filesystem for those virtual directories and /mnt, which you didn’t copy:

# mkdir /mnt/newOS/dev# mkdir /mnt/newOS/proc# mkdir /mnt/newOS/sys# mkdir /mnt/newOS/mnt

Finally, copy over some important and missed locations:

# cp -avp /dev/console /mnt/newOS/dev# cp -avp /dev/urandom /mnt/newOS/dev# cp -avp /dev/zero /mnt/newOS/dev/# cp -avp /dev/random /mnt/newOS/dev/

Update:  Another command that could be used to perform this copy operation is dd:

https://wiki.archlinux.org/index.php/Disk_Cloning#Using_dd

The syntax is much simpler and you don’t have to manually recreate the mount points and whatnot:

dd if=/dev/mapper/f20-lvm-root of=/dev/sdxn bs=4096 conv=notrunc,noerror,sync

Thanks to joshuar on ask.fedoraproject.org for the alternative solution!

Take the fstab information you gathered prior to this process and edit the newOS/etc/fstab file to change the device names to point to the devices you will be using with the new system.

Finally, power down the system, and install the disk to which you just copied your operating system. Uninstall the disk with your old OS (or clone it to back up and blow it away or whatever you need to do, as far as disk management is concerned).  Boot into your rescue disk with the new hard disk installed and reinstall grub (see the link above).

Now, I believe that should work, and it is what I would attempt. I will admit some laziness on my behalf in which I based it largely on my Internet search for rsync usage to migrate filesystems which led me to this guy‘s write-up of the process.

And if you read his blog entry, you’ll find that I’ve customized the process for your situation (and the more general task involved) but also that he uses the -E option in his rsync command whereas I do not in the one I’ve provided. That’s because this option causes rsync to preserve the executability (or non-executability) of regular files (A regular file is considered to be executable if at least one ’x’ is turned on in its permissions) when –perms is not enabled, but -a implies -p which is equivalent with –perms, so it’s unnecessary.

Let me know how it goes; everything in this process is theoretically reversible (you can reinstall the hard disk with your old OS if you need to and the rsync command is not destructive) so you should be safe, but if you’re extra-neurotic, you could back up what you have before-hand.

Advertisements
This entry was posted in Information Technology and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s