Can I clone a root file system of a running server via ssh?

9,060

Solution 1

If you want a backup all all the data then you can log in on the server via ssh and then simply copy all files. E.g. with scp or with rsync.

You can even copy everything including OS files if you use
rsync -zvr --exclude /dev/ / destination_computer_name_or_ip

-r: Recursive
-v: verbose
-z: Enables compression

Note this will not copy the boot record. To do that you want to copy the whole disk, and you want to do it when the disk is not mounted. (Read: Shutdown the machine you want to utterly_full_backup and boot from a liveCD or boot from another partition.)

Then use dd to read the whole drive.

Example code:

PC to store the backup on:
nc -l 4242 | gunzip | cat > my_full_disk_backup_of_PC_named_foo

And on the PC to back up:
dd if=/dev/sda of=- bs=1M | gzip | nc -p 4242 name_of_the_destination

dd will read from the disk. The whole disk, including boot sectors and empty sectors.
In the example we set it up for the first disk as indicated by sda. Adjust to sdb for the second disk, sdc for a third disk etc etc.

We output to std out, indicated by the -.

bs=1M sets a block size. You do not need this, but without there will be many small reads and huge overhead. Setting this to some value larger than 512 bytes or 4k will speed things up.

Next we pipe | the output though gzip to compress it. This assumes that your CPU will be a lot faster than your network. You can skip the gzip on the source and the gunzip on the destination, in which case you will send the raw data rather then compressed data over the network.

Last is nc or netcat. It accepts the input from the previous pipe and puts it on the network, toward port 4242 on a computer named name_of_the_destination.


On the receiving end we do the reverse:

Listen via nc -l for input on port 4242,

un-gzip if needed,

And finally write it to a file.


Important note:

You can do this while booted from the disk you are backing up. But there is no guarantee that the filesystem will stay the same during a backup. Thus only try that if you can boot with the disk as read-only. (e.g. purely using a ram disk)

Solution 2

what i would do is a lot similar to Hennes

i would create an iso file, download it, put it on disk or usb and upload a copy to dropbox just in case.

mkisofs -V LABEL -r DIRECTORY | gzip > backup.iso.gz

My friend hosts on rackspace, he told me that rackspace users have the option to create an iso file automaticaly, and if they were migrating to a new server, they could store the old backup on the old cloud server using rsync or whatever and shut it down, they still pay but its cheap.

I don't know if your hosting offers an automated backup system, or if you're paying for support. it's worth to ask them and maybe let them do it, if it's your first time.

Share:
9,060

Related videos on Youtube

AAlvz
Author by

AAlvz

SOreadytohelp

Updated on September 18, 2022

Comments

  • AAlvz
    AAlvz over 1 year

    I want to make a backup of a running server. Every config files and everything without turning it off.

    How can I accomplish this? Is it ok to try copying it all? Because I had problems with the root directory.

    I don't want to make a lot of experimentation also because it has an important configurations.

    I'm trying to make all this via ssh. How can I do this backup?

    Any Ideas?

  • AAlvz
    AAlvz over 11 years
    Thanks a lot! I'll try this.. but the last part sounds a little complicated.. should i try to backup the boot part shutting everything off? .. To make it completely safe...
  • ganesh
    ganesh over 11 years
    It depends on your goals. Just a backup of all files, including data, configuration files and OS files can be done with rsync or scp. That usually suffices. If you want to keep more copies around (e.g. a weekly backup) then look into backup software and or use rsync. Rsync can backup only the changes. If you want a full disk emergency recovery option then use the dd method. If the harddisk fails then you can replace it with a new (equal sized or larger) drives and restore/reinstall on from the image you made with the dd command. Or you can already write it to a disk and keep that as a spare.
  • ganesh
    ganesh over 11 years
    To do that, use nc -l 4242 | gunzip | dd of=/dev/sdb on the destination computer. (assume the spare disk on position 2). Note that a backup of the OS is nice, but you really want to set up something to regularly backup the data. Else if things crash and you restore a year old (disk) backup then you have a year old data.
  • MDMoore313
    MDMoore313 over 11 years
    If your source disk is written to while the copy is running then your destination copy will end up being inconsistent/corrupt.
  • ganesh
    ganesh over 11 years
    Correct. Hence the advice to boot from a live CD or only to do that if the filesystem is read-only.
  • davidgo
    davidgo over 11 years
    IF your system is using LVM, and does not have all the space allocated (or you can add more space temporarily to the LVM), you can take a snapshot of the root partition and back up the snapshot using LVM while the system is live. You will need to do an fsck on restore of the new system, but it will be in a consistent state.
  • ganesh
    ganesh over 11 years
    Aye. I guess the real proper answer would have been a long one which included configuration management, off-site backups, disaster recovery procedures, possible on site RAID etc etc. All good things for the OP to read, and quite possibly overkill.