Backup whole hard disk Linux

8,849

Solution 1

As already noted, there are a lot of options and which one is the best depends on your requirements.

Since your systems is on RAID1, you can simply replace one disk and let your mirror rebuild itself. The removed disk can be used as a backup copy.

In the past, for full system backups, I have used Ghost4Linux (aka G4L) and Mondo/Mindi. The former can save a partition image locally or remotely and the later can create bootable restore (+rescue) CDs or DVDs. Both have a ncurses gui, no need for command line usage.

But many times, I just copy the whole system. I mostly use rsync but here are some options that I think it's what you're looking for.

Let's assume you have two partitions, / and /var and you want a complete backup. All commands require root privileges.

mkdir /mnt/orig
mount / /mnt/orig -o bind
mount /var /mnt/orig/var -o bind

This will create a new mount point for rootfs that you can now copy without worrying for other mounted filesystems like /proc or /sys or network shares.

Let's assume you have a second disk formatted with an appropriate filesystem and mounted under /mnt/backup (or an NFS on the same mountpoint). It is advised that some services should stop running, for example the mysql service or mail server, to ensure integrity of the replicated data.

Each of the following commands should suffice.

cp -a /mnt/orig/* /mnt/backup/
rsync -a /mnt/orig/ /mnt/backup/
tar -C /mnt/orig -cf - ./ | tar -C /mnt/backup -xpf -

The last one is kind of overkill though. tar and rsync provide a --exclude parameter for skipping files based on pattern matching, e.g. --exclude=\*.log --exclude=var/tmp. The above commands do not use compression and you will have an exact replica of your system. You can also install grub and make the backup disk bootable.

If you have limited space on your backup device or a not compatible filesystem (NTFS) or it is a CIFS mounted share, you can create a compressed tar archive.

tar -C /mnt/orig -zcf /mnt/backup/mybackup_$(date -I).tar.gz ./

or create a squashfs image which you can later loop mount and browse its contents like a normal filesystem.

mksquashfs /mnt/orig/ /mnt/backup/mybackup.squashfs
mkdir /mnt/squash
mount /mnt/backup/mybackup.squashfs /mnt/squash -o loop

Also, getting a remote backup with ssh and tar or rsync. rsync requires root access to the remote server in order to preserve ownerships, permissions, device files etc. --numeric-ids is necessary for not mixing ownerships with the remote system's users.

tar -C /mnt/orig -zcf - ./ | ssh user@server 'cat -> mybackup_$(date -I).tgz'
rsync -aP -e ssh --numeric-ids /mnt/orig/ root@server:/path/to/backup/

In most cases, a full restore will require booting with another system, preferably a Live CD, then creating/rearranging your partition scheme, coping/extracting your data back and at the installing the boot loader again.

Solution 2

The native way of doing this is to create an LVM layer between your partitions and the disk. LVM has the capability to create snapshots which are basically instant point-in-time instances of the disk. The reason for this instead of just dd'ing the disk directly is that the contents of the disk may change while the dd is in progress. An LVM snapshot will not change once its created. Then after your backup is complete, destroy the snapshot.

So the layered topology would look something like:
DISK0 + DISK1
RAID-1
LVM
Partitions
Filesystems

You could also put LVM on top of each partition if you wanted to back up just a single partition. Either works

Solution 3

How about using Clonezilla http://clonezilla.org/ for this? Clonezilla LiveCD is very easy to use.

Solution 4

dd on the device is a way to clone a hard disk.

Share:
8,849
dcolumbus
Author by

dcolumbus

Updated on September 18, 2022

Comments

  • dcolumbus
    dcolumbus over 1 year

    I am looking for a method to backup a whole disk in case something is screwed up...

    I don't even know what to install on some special devices of the server, and every time it is not the same.

    Would cp -a for the / directory work?
    Is there any program that can copy all the data of the hard disk? (like an image of the hard disk).

    I know that this exists at least for Windows..

    The hard disk is on a RAID 1 though....

    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' about 13 years
      Yes, you can back up the whole hard disk, and there are many ways of doing this. What media are you backing up to? How often are you doing backups? How fast do you need recovery to be?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 13 years
    So is cat. There's nothing magical about dd, the magic is in /dev/sd*. And no, dd isn't faster.
  • dcolumbus
    dcolumbus about 13 years
    It is a solution but the image is too large..That means that it wont work at another disk... If i use cp for the files and dd for MBR would that work ?
  • Peter.O
    Peter.O about 13 years
    Abstractions galore! ...interesting... (and all sired by the humble punchcard :) ... +1 for explaining where LVM fits into the general picture
  • DeveloperACE
    DeveloperACE over 5 years
    Since your systems is on RAID1, you can simply replace one disk and let your mirror rebuild itself. The removed disk can be used as a backup copy. RAID is not a backup.