System image of running Debian system?

62,296

Solution 1

dd is good if you don't mind generating an image file that's exactly the size of your raw disk. rsync is good if you want easy file-level access. But the standard means of backing up a filesystem is dump and restore (aptitude install dump).

For a device like the Raspberry Pi, I'd back up as follows, assuming an MS-DOS partition table and that the disk is /dev/sda:

  • dd if=/dev/sda of=sda-boot-sectors.img count=2048 to get an image of the boot section of the disk.
  • fdisk -lu /dev/sda >sda-partition-table.txt for later human reference.
  • dump -0af sda1-root-level0.dump /dev/sda1 and repeat for each partition you want to back up.

You can then compress the lot and leave it somewhere safe. To restore, you'd use dd to restore the partition table and boot sectors, reload the partition table, make the new filesystem(s), and use cd /mnt/new-filesystem; restore -rf /some-location/sda1-root-level0.dump.

The upsides:

  • dump gets a complete copy of the filesystem, including ACLs, extended attributes, ownership, sparse files, special filesystem attributes — everything is dumped as-is.
  • It'll only copy the blocks you need, ignoring unused ones.
  • It's standard unix tool and readable by a lot of other unix tools.

The downsides:

  • it will produce corrupted snapshots if the file system is written to during the dump
  • it's more difficult to mount the backed up image (which you can do with disk images) or get to individual files (which you can get with rsync backups).
  • It's filesystem type-specific. You can dump an ext3 filesystem and restore it in an ext4 one, but you can'd dump any type of filesystem. Most mature filesystems have their ownd dump versions. The standard Debian dump does ext2, ext3 and ext4. If you use a Flash-specific filesystem, your options may be different.

Solution 2

For Debian, you really only need to back up your data and configuration files.

To back up existing state:

  • Back up package selections with dpkg --get-selections > dpkg_selections.
  • Back up the debconf database with sudo debconf-get-selections > debconf_selections.

To apply this to a new system:

  • First apply debconf selections with sudo debconf-set-selections < debconf_selections.
  • Apply package selections with dpkg --set-selections < dpkg_selections.
  • Install packages with apt-get dselect-upgrade.

Your data should primarily be in /home and /var (e.g., /var/lib/mysql for MySQL, /var/www for Apache, etc.). You should be able to figure out which applications are important to you.

Configuration will be primarily in /etc. Again, it shouldn't be difficult to pick out what's important for you.

Solution 3

Use dd for the boot-sector and rsync -aHS for the files. You have to exclude "virtual" filesystems like /proc /sys and any RAM-disks (tmpfs).

If you want to keep the partitioning as well, you can dump that with sfdisk or recreate it with parted.

Solution 4

I don't know, if this is good to do, when the system is running, but this is what I do.

sudo dd if=/dev/mmcblk0 of=/path/to/backup/directory/backup.img bs=1M

as I said, maybe there is a better solution, in that case I would like to know it as well!

Solution 5

I have tried so much backup and restore software and was never happy. This is what I do now: I have a second Debian installed on a spare computer (same MB, NIC card, etc). Every evening I rsync from machine A to machine B. There are some files that I hold back (/etc/network/interfaces, /etc/hosts, /etc/hostname) since I don't want conflicts of the two running systems. Actually, I do have copies of them in another folder. I also disable some services that I don't need to run on machine B (postfix, mysql, etc). I have a script written on machine B that will basically turn it into machine A (replace those files that I held back), restart the NIC and enable the services that were disabled. Of course machine A needs to be off when I run the script or there will be havoc. I test machine B monthly by turning off machine A, running the script, and doing some tests to make sure it is up to date and running properly. It works like a charm!

Share:
62,296

Related videos on Youtube

Nik
Author by

Nik

Updated on September 18, 2022

Comments

  • Nik
    Nik over 1 year

    Is it possible to backup a running Debian system to some kind of image file, that could simply be written back to another hard disk using dd in case of failures of the original (backed up) Debian system?

    I am especially looking for a tool directly available in the repositories, as I have Debian Wheezy running on a raspberry pi and therefore, I need support for the ARM architecture, which is given quite reliable for packages of the Debian repositories.

    • Admin
      Admin almost 9 years
      I wouldn't recommend trying to get a block-for-block copy from rw-mounted filesystem. Smallest write can corrupt it and you think you have a workable backup. So mount -o remount,ro the drive you are copying if you opt for dd,dump or similar. Copying withing filesystem with rsync or any other method including cp is safe for live system.
  • Nik
    Nik almost 12 years
    Thanks, sounds very interesting, did you already tset this in the past? How do I create a complete image out of these parts, just use a loopkack device? (Yes, want to keep partitioning and so on. Want an image ready to be written to a new SD card / disk without further fiddling)
  • Nik
    Nik almost 12 years
    Interesting project though, thank you anyway :-)
  • Nils
    Nils almost 12 years
    @stefan.at.wpf I just mixed up some of my solutions for cloning VMs, creating physical servers (autoyast/kistart) making a raid1-mirror out of a non-mirrored system and the usual way to replicate (static) parts of the filesystem for cluster nodes. I do not use this myselv. My method would be to autoinstall new server (including a backup-client) and then restore via backup the contents of the old server to the new server.
  • pandita
    pandita over 8 years
    To get debconf-get-selection run apt-get install debconf-utils
  • imz -- Ivan Zakharyaschev
    imz -- Ivan Zakharyaschev over 8 years
    It's not stated in the manpage for dump that the filesystem shouldn't be written to. I mean, this sounds like a sensible restriction, but it's a pity that it's not mentioned in the manpage, so that someone may make this error...
  • imz -- Ivan Zakharyaschev
    imz -- Ivan Zakharyaschev over 8 years
    Thanks to your post, I have learned about this tool (dump/restore). I'm going to use it to make a copy of a filesystem with something like: mkfs.ext4 /dev/sda1; mount /dev/sda1 /mnt/disk; cd /mnt/disk; dump -0af - /dev/sda5 | restore -rf -
  • Allison
    Allison over 6 years
    This is literally ignoring the most important part of the question. The question is asking how to do a live image of a system and by the time you're moving disks around you had better have shut the system down.
  • Seamus
    Seamus about 4 years
    Does systemback require use of the GUI to create a restore point? Or can it all be done from the CLI?