Ghosting The Machine

Want to support HowtoForge? Become a subscriber!
 
Submitted by linuxscooter (Contact Author) (Forums) on Mon, 2010-07-12 18:07. :: Linux | Backup | Other

Ghosting The Machine

This is a short but potentially extremely handy guide to ghosting one Linux box to another (or simply making a full backup of a desktop/server). Credit goes to 'topdog' for this.

You might have a small office where you customise one desktop just how you like it and need to roll this out to N other PC's or simply want a backup of a server or desktop to another machine or even to an image file.

The main tool here is netcat which is extremely powerful and has a multitude of other great uses that won't be covered here.

Target Machine:

** Boot to linux rescue mode with networking (CentOS works fine)

Initiate netcat to listen on port 30 - # nc -l -p <portnumber> | dd of=/dev/sda (assuming the hard drive is sda and not hda):

# nc -l -p 30 | dd of=/dev/sda

Source Machine:

Dump the contents of the disk to the target PC - #dd if=/dev/sda | nc <ipaddresstarget> <portnumber>

# dd if=/dev/sda | nc 192.168.0.20 30

Then to check that traffic is flowing, on the source go to another terminal (ALT/F2) and dump the tcp data on the NIC (assuming it's eth0):

tcpdump -tnli eth0 port 30

If you just want a backup image you could change the above output on the taget to:

# nc -l -p 30 | dd of=mybackup.img

That's it. Naturally the target PC/disk cannot be smaller than the source:) I hope this saves someone a lot of time.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Marc (not registered) on Sun, 2014-04-06 18:41.

If the target system has a disk of different size, with bigger partitions for example, you can use:

 http://positon.org/clone-a-linux-system-install-to-another-computer

Submitted by Anonymous (not registered) on Mon, 2011-06-20 20:35.
Can i send the ghost image to another system through the internet?? What if the back up server is not on my LAN??
Submitted by DodgeThis (registered user) on Thu, 2011-03-17 23:47.

Can I use this to make a copy of all OS from one server to another without shutting down/umount the source? Is this safe to do dd if=/dev/sda on a running source server ? I have some critical servers that i need to replicate without downtime.

This is a very very very handy How to!  Thanks :)

Submitted by yesmat (registered user) on Mon, 2011-03-07 02:26.

Can nc be used to ghost/backup/image a live server?

Thanks

Submitted by Anonymous (not registered) on Mon, 2010-08-02 03:18.

What about using clonezilla and drbl?  just do the initial image creation, not the push from the server to the clients.

 or pxe boot into clonezilla live, plug in a usb drive and go from there.

Submitted by Lawrence D'Oliveiro (not registered) on Sat, 2010-07-17 09:57.

My tool of choice for bulk backup/restore and transfer of files is rsync. It does file-level backups, not image backups. But Linux is such that you can do a file-level transfer of an OS installation and still have it work. Only people who spend too much time with Windows think that an image backup is necessary for this!

Advantages of file versus image backups:

* Automatic support for all filesystem types that your kernel will support.

* Ability to transfer files between different filesystem types (as well as different disk/partition sizes, depending purely on available space).

* Ability to confirm the copy was successful just by running the command again; in this case, rsync will do an incremental transfer, and if it reports nothing to do, then you know you got everything.

* Fast incremental transfers over a network .

Submitted by hypatia (not registered) on Wed, 2010-07-28 16:11.

Your right, but...

You'll have to install grub into the boot sector of the new box, I usually do this with a command like:

dd if=/dev/hda bs=512 count=1 | {netcat}

 Or, you could chroot the the mount point on the new box and run grub-install.

Submitted by giochi gratis (not registered) on Mon, 2010-07-19 08:33.
Ability to transfer files between different filesystem types (as well as different disk/partition sizes, depending purely on available space).
Submitted by Anonymous (not registered) on Thu, 2010-07-15 10:59.

If one ghost a Debian linux from a server to a harddisk, will this work if the harddisk be simply plug into another server, regardless of configuration and make, since the mother board, CPU, RAM and other components may be diffrent?

Thanks

Submitted by Anonymous (not registered) on Fri, 2010-07-16 19:18.

There are no guarantees, but I have noticed Linux does a good job of booting on a different machine configurations. What I have seen is that I can usually move a hard drive from one machine to another and Linux boots fine.

With Windows the installation ends up being pretty specific to the machine configuration it is installed on, though I have multiple times done "repair installations" that have allowed me to move a drive to a new PC and keep the old installation (it seems to mostly just update the drivers.)

 So to answer your question, with Linux (Debian) you probably would have good luck with this but I can't say for sure.

Submitted by linuxscooter (registered user) on Fri, 2010-07-16 11:34.

Hi,

Should work. I have ghosted to different hardware fine. Only sometimes there might be xorg issues which can be fixed.

Debian has awesome hardware dtection so should be fine:)

 

Cameron

Submitted by Paul Zarucki (not registered) on Thu, 2010-07-15 10:24.
Many thanks for this. So neat and simple!
Submitted by Anonymous (not registered) on Thu, 2010-07-15 07:34.
How can you compress it on the fly ?
Submitted by Anonymous (not registered) on Mon, 2010-07-19 19:20.

Assuming you have an ssh server running, which is more secure than opening a port to anyone:

If the target machine or network is the limiting factor:

dd if=/sourcepath/sourcefilename | gzip | ssh user@desthost "dd of=/targetpath/backup.img.gz"

If the source machine is the limiting factor:

dd if=/sourcepath/sourcefilename | ssh user@desthost "gzip > /targetpath/backup.img.gz"

If you are wanting to use netcat, just break up the commands above and use the netcat commands from the original post.

eg

dd if=/sourcepath/sourcefilename | gzip | nc 192.168.0.20 30 if the target or network are the limiting factors

or

nc -l -p 30 | gzip | dd of=mybackup.img.gz

only compress it at one end as compressing something twice is generally little gain over compressing once.

Submitted by Temporary Saint (not registered) on Wed, 2010-07-14 15:26.
This is pretty slick.  Simple and easy.  Question though - is the target PC's OS booted off the hard disk or a CD?
Submitted by linuxscooter (registered user) on Thu, 2010-07-15 09:48.

Hi,

Yes the target is booted from CD. Unless you just want the image file then no CD required of course. The target just needs nc which comes with most distros.

Cameron

Submitted by Anonymous (not registered) on Wed, 2010-07-14 14:33.

Why not just use mkcdrec?  Get a backup onto CD/DVD/network, etc. and allows you to do a bare-metal recovery as well?  Don't have to worry about size of disk, or machine.  As long as the machine is in the same "family", works fine.  Much better for roll-outs, IMHO.

 

http://sourceforge.net/projects/mkcdrec/files/

 and

http://mkcdrec.ota.be/

 

Submitted by Juergen (not registered) on Tue, 2010-07-13 05:42.

You could always throw in a pv, as in

dd if=/dev/sda | pv | nc 192.168.0.20

Submitted by igiron (not registered) on Thu, 2010-07-15 06:16.
What does the | pv | stand for? Thanks
Submitted by Anonymous (not registered) on Thu, 2010-07-15 12:28.
pv is a program that graphically displays the progress/status of data going through a pipe. install it and try "pv cat /dev/random > /dev/null"
Submitted by linuxscooter (registered user) on Tue, 2010-07-13 12:00.
Even better - thanks;)
Submitted by Mario Kerecki (not registered) on Wed, 2010-07-14 15:01.
I prefer Clonezilla for server/desktop backups. It can backup to usb drive, ssh server, nfs server or local disk. One of the best features is it can restore an image to a different size hardrive, bigger or smaller as long as the data fits. It also only copies "used space" whereas the dd command will copy the entire drive including empty space. Clonezilla is very easy to use, similar to the windoze Ghost program but open source and free :) To learn more checkout http://clonezilla.org cheers -m
Submitted by Paul D (not registered) on Wed, 2012-01-04 14:50.
If you zero out the unused space (aka  dd if=/dev/zero of=/loc/of/mounted/drive/to/be/cloned/zero; rm zero;) THEN compress the image through gzip, you will essentially be making a clone of just the used space. I've done this to drives that were 500gb with only 30gb being used and the resulting image was 24ish gbs. Works like a charm...