26

I have a system running as a server that has a failing harddrive. While all important data is on a RAID and backed up and all that, I don't have an image of the system itself. There is no specific need as I can do the install of course, but I still want to try to do a hotcopy first before just going down the restore path. I know there are some downsides to this as a process, but there I don't think there are much downsides to trying it as a first resort.

  • OS: Ubuntu 12.04.4 LTS
  • Headless
  • I'm not hoping to install too much new software as the disk is allready failing :)
  • The system is running. I'm scared stopping it increases the chance of the disk not comming back up. This means dd might be out?
  • The new disk is not the same size (it's twice as big) as the old one further complicating the dd issue.

My idea was to

  • hotplug the new drive in the system
  • make a filesystem
  • mount it in /mnt/somedir
  • rsync the files
  • some fstab magic
  • some booting magic

The questions I still have are:

What would be a good rsync command? I was planning on:

rsync -aAXx  / /mnt/somedir/ 
   --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found}

(I'm skipping some more dir's, e.g. my mounted raid's etc)

Where the options are:

-a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
-A, --acls                  preserve ACLs (implies -p)
-X, --xattrs                preserve extended attributes
-x, --one-file-system       don't cross filesystem boundaries

I'm specifically skipping -H, -v and --progress to speed up the process.
Would that work for Ubuntu? I'm not sure if Ubuntu uses any hardlinks, but I don't think I need the option, do I ?

After this run I could reboot (maybe with a live usb drive) and re-run the rsync if the old disk still starts. This would fix any non-readable/changed files because the system was running I suppose.


How to fix booting?
Then my plan would be to change the UUID for / in my fstab (still have to google how to find the uuid), and do some magic so the system actually boots from the new disk

Have I forgotten something or have I planned something specifically stupid?

4 Answers 4

20

You can grab the UUID for all block devices with the blkid command. (You want the one that just says UUID, not PARTUUID)

The rsync options I use are -avhPHAXx.

I don't think -v or --progress will speed anything up unless you're on a very slow console/tty.

Using -x eliminates the need for all of your exclusions assuming they are all on different filesystems (on my system, all except lost+found are).

The only frequently used program that I know of that uses hard links (at least on my system) is git, so that's why I add the -H option. The only problem I think you would have by not using -H is that it will take up a little more space.

As for the bootloader, if you are using GRUB2 with MBR, then the command I use is grub-install /dev/sda (replace sda with the correct drive for you). That should make the new drive bootable. If you're using a different bootloader or UEFI, then I would check google for how to get the new drive booting properly. Just remember that /boot on the new drive will need to be on the same partition as it currently is (assuming you're not using a UUID for /boot also), otherwise you will need to modify fstab accordingly.

6
  • I don't believe I have them on separate systems I'm afraid, so the exclude seems prudent. I'm reading mixed stuff about the speed of - v, so I'm not sure what to do there ;). Git is a good point, I'm sure /home have some of that!
    – Nanne
    Jan 30, 2014 at 17:19
  • 1
    Well, /proc, /sys and most likely /dev are definitely different filesystems/mountpoints from /, so there is absolutely no reason to exclude those when you have -x. If you're not sure what is part of your root filesystem or not, just run mount to check. Anything that's in there won't need to be excluded manually.
    – bparker
    Jan 30, 2014 at 18:23
  • I'm not sure how and why, but the excludes were wrong anyway. They actuallywere in the dry-run I did, not sure why. (well, I copied the format from a semi-random place, so that might be the reason?). Anyway, with the -x firmly in place per your advice I didn't worry too much, only thing extra happened was that some unimportant stuff (old backups-of-backups for instance) have been 'saved' as well.
    – Nanne
    Jan 31, 2014 at 10:01
  • This saved my a$$. I cloned 12GB+ system on Linode to brand new node. After 5+ years later, this still works. Thanks guys!
    – retromuz
    May 30, 2019 at 0:48
  • So when partitioning the cloned filesystem ready to accept the clones, should my root partition have the boot flag set while I am keeping them insync, or do I set it just before putting the cloned drive into production. Otherwise I would assume the cloned drive is then bootable which is not really wanted. In my situation my clone drive is a disk on a separate server. And Rsync is used to keep it aligned with prod. Jun 10, 2019 at 2:30
15

I just did this successfully (after a couple trys).

I used

sudo rsync -ahPHAXx --delete --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found} / /mnt

Then I reset my /mnt/etc/fstab file for the boot partition and my swap space.

Then I needed to reset GRUB

for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done
sudo chroot /mnt
sudo grub-install --recheck /dev/sdX
sudo update-grub
1
  • 2
    Note that this is only for MBR formatted disks.
    – Teque5
    Mar 30, 2018 at 16:13
2

Do not attempt to fix booting with this drive; my recommendation:

  1. Plug in a drive on another machine instead of risking a hotswap on the failing machine.
  2. rsync your non-system files across to the new disk.
  3. On a separate hard disk, create a boot, a minimal root, and a swap. Install the same operating system that you wish to clone from the ailing hard disk.
  4. Boot from this new disk (ideally on a separate machine, if you cannot risk downtime on the target host, otherwise you can boot the target host with this disk).
  5. Add the disk from step #2 to this new system, with the correct mount points. Now you have a clone of the system drive. You can (optionally) copy the partitions over to this new disk, but my recommendation is to keep the disk as a minimum requirement for booting your OS. As you have realized, having too many things on the primary partition makes recovery difficult. Make sure you copy the network configuration correctly as your server is headless.
  6. Simply replace the drive on the ailing system with this new disk pair (if you choose not to boot the target initially in step #4).
  7. Reboot.
4
  • I can risk downtime, it's a non-essential server. I was going for the hotswap (hot-add basically) because that copy would be much faster. There shouldn't be too many risks I thought, as we are talking SATA (II at least). Apart from that, what is the reason to suggest an extra boot disk? Wouldn't that complicate the matters for some allready installed software, homedirs etc? It sounds like more work which I'm trying to avoid :D
    – Nanne
    Jan 30, 2014 at 16:33
  • Its what I use in production; I mount /home, /etc, /var, /usr and /opt on external disks (actually on LVM), and / and /boot on a separate disk. This way I don't have to worry when the main disk fails I just swap it out without affecting my services :) Its a bit of work initially but saves you in the long run. Especially when you run out of space on a partition and need to add to a volume without downtime. Jan 30, 2014 at 16:56
  • While you make a good point, it's not a requirement I currently have, nor need :). There are several other discs in the system (about 7) and I have to draw the line somewhere ;D. So OS doesn't need splitting at the moment. I mean: I agree the system is good, just not something I'm looking for now ;D. So if I leave it all on 1 disk, the fact I'm copying external will make it seriously slow, wouldn't it?
    – Nanne
    Jan 30, 2014 at 17:13
  • Depends on your interface to the external, over ethernet it should be fast, over USB 2 faster, on USB 3 even faster. It won't make it very slow - of course don't copy directories that are being written to (like say, /tmp, which you don't need anyway) or anywhere pipes are being written to. Jan 30, 2014 at 18:38
0

I have spend many time on a Centos Freepbx distro to rsync and have a bootable new disk and finally after fixing fstab and grub UUID it don't work.

The last step to make it work is

dd if=/dev/sda of=/tmp/mbrsda.bak bs=512 count=1
dd if=/tmp/mbrsda.bak of=/dev/sdb bs=446 count=1
grub

When you are in the Grub CLI (i.e. at the grub> prompt):

device (hd0) /dev/sda
device (hd1) /dev/sdb
root (hd0,0) 
setup (hd0) 
root (hd1,0)
setup (hd1)
quit

Press enter and you can now shutdown, remove old disk and boot with new one.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .