I want to do the following - Save the current state of my linux OS(centOS6.5). Do some changes (more specifically inject a fault and test some service to check how it behaves when that particular type of a fault occurs.) Then restore the system back to the saved state. I am not able to figure out how to do this on a linux system. I have a machine with centOS 6.5 with an ext4 filesystem.Also this is a physical server and not a virtual machine.

Any ideas or suggestions?

  • Physical server, or virtual machine ? – steve Aug 28 '15 at 18:33
  • 1
    I recommend to run your OS on virtualization software like VirtualBox. It nicely supports snapshots. – yaegashi Aug 28 '15 at 18:34
  • It is a physical server. I need to do this on multiple machines(physical servers) and cannot change the configuration to run my OS on a virtualization software. – bRuta Aug 28 '15 at 18:39
  • It usually needs an additional meta layer like virtualization to support system snapshots. It would be very hard to make physical servers behave themselves as if they were under control of it from firmware and bootloader level. – yaegashi Aug 28 '15 at 19:14

I would keep it simple and clone it. Boot a live system from USB (easiest is Ubuntu from a USB thumb drive, I find), then dump your hard disk to a different partition (or external hard drive etc.), e.g.

dd if=/dev/sda1 bs=64M of=/mnt/my_mounted_backup_drive/backup-sda1

where you need to replace /dev/sda1 with your root (/) partition. Do the same with other partitions (like the one for /boot, /boot/efi, /home) where applicable.

If you need to save space, you could do

dd if=/dev/sda1 bs=64M | gzip --fast | dd bs=32M of=/mnt/my_mounted_backup_drive/backup-sda1.gz

or, more complicated, much slower but saving a few more bytes,

mkdir -p /mnt/linux
mount -o ro /dev/sda1 /mnt/linux
cd /mnt/linux
tar cvJf /mnt/my_mounted_backup_drive/linux-backup.tar.xz .??* *

You can then restore the other way around, e.g.

mkdir -p /mnt/linux
mount /dev/sda1 /mnt/linux
cd /mnt/linux
tar xvJf /mnt/my_mounted_backup_drive/linux-backup.tar.xz


dd /mnt/my_mounted_backup_drive/backup-sda1.gz bs=32M | gzip --decompress | dd bs=64M of=/dev/sda1

(careful where you are writing your data, this deletes everything on /dev/sda1, so get it right the first time :-)

|improve this answer|||||
  • PS: If you expect something to go really wrong backup the partition table, like dd if=/dev/sda bs=64M count=2 of=/mnt/my_mounted_backup_drive/backup-sda. When restoring you need to be careful not to write too many blocks back to disk here - cannot say how many without looking at your partition layout. Restore up to (not including) the block where your first partition starts according to gdisk or fdisk. – Ned64 Aug 28 '15 at 19:35

You can use rsync to backup the entire system.

rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /* /path/to/backup/folder

There's a awesome article at Arch Linux Wiki about it

|improve this answer|||||
  • That's quite short to be the best option for me. Of course if you don't have rsync you can use dd wich i use to backup my usb drives. – m3nda Jun 10 '16 at 1:36

How about using rsync or rsnapshot to essentially backup the local machine? Once you test is done, simply restore the backup.



Similarly, you can use LVM:


|improve this answer|||||

You can use FSArchiver, which will save the contents of the file system to a compressed file and restore the filesystem when extracting the data.

Here is an example from the documentation:

fsarchiver savefs /mnt/backup/gentoo-rootfs.fsa /dev/sda1

You can also use multi-threaded compression, and restoring is straightforward:

fsarchiver restfs /mnt/backup/gentoo-rootfs.fsa id=0,dest=/dev/sda1
|improve this answer|||||

In order to go back to a previous state, you could mount the current file system to a new directory. This mounts the file system to a new root directory specified by the following command:

mount DIR DIR

To restore this state, change the root of the file system from the directory specified in the mount command.

umount DIR

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.