# Backup of hibernated system using installCD stops hibernate?

## as.gentoo

Hello,

I tried to take a backup of a system that was hibernated. I hoped that it works like this:

1) hibernate system

2) boot system using the Gentoo installCD / SystemRescueCD and do the backup with dd

3) boot system as usual, using the hibernate data (from swap partition) to continue right before point (1)

This unfortunately doesn't work since the system does a "cold" reboot at point (3) as if it was shutdown -h before.

Is this connected to ACPI states? Those are reset once the system booted, right?

Swap is not written to at point (2) and I think that the kernel command line is not changed either.

If it is as I guess - is there any way to set the state back by adding something to the kernel command line or manipulate /sys in the initramfs to make it work?

If I'm wrong, what is the problem then?

Thanks in advance!

----------

## khayyam

as.gentoo ...

Firstly, it depends on what method you're using to 'hibernate', the in kernel SWSUSP will invalidate an image if you attempt to boot another kernel, and as booting results in a change in ACPI mode a boot of a CD will result in this happening. With tuxonice (suspend2) as long as the image is available then it should be resumable (though I've never tested doing so).

Secondly, backing up your disk via dd while hibernated is probably not a good idea, there are various tmpfs, procfs, sysfs, directories, locks, fifo's, etc, that will effectively get copied and so be on any resulting use of that backup. Also, dd is not really a suitable backup tool, you will end up copying empty space for no real reason.

best ... khay

----------

## as.gentoo

Hello khayyam.

 *khayyam wrote:*   

> as.gentoo ...
> 
> Firstly, it depends on what method you're using to 'hibernate', the in kernel SWSUSP will invalidate an image if you attempt to boot another kernel, and as booting results in a change in ACPI mode a boot of a CD will result in this happening. With tuxonice (suspend2) as long as the image is available then it should be resumable (though I've never tested doing so).

 I'm using the "default" S2D. 

Shouldn't it be possible to tell the kernel to use the image, no matter what happened before? Or setting the ACPI state before the kernel checks it, maybe by grub?

 *Quote:*   

> Secondly, backing up your disk via dd while hibernated is probably not a good idea, there are various tmpfs, procfs, sysfs, directories, locks, fifo's, etc, that will effectively get copied and so be on any resulting use of that backup.

 I usually have many programs running holding data in a editable state or just displaying data, e.g. several LibreOffice documents, PDF documents, emacs, browsers having filled forms as well as terminals containing stuff like   tail -f <logfile>*   etc.

I like hibernate because everything is there as if you never powered the system down. On a desktop system it's hard reconstruct that state when you called shutdown.

As well I'd like to have a quick and easy way to restore the system to a recent working state (incl. permissions, users…) when an update results in a heavily messed system. Potentially candidates are updating gcc, the kernel as wel as a number of qt/kde-packages. Just to make sure, I don't mean ebuilds that fail - in 99% that mess results from (me) doing something wrong in the update process.

So I wish I could just freeze the complete systems state and restore everything in a case of a catastrophe caused by the user, by hardware or software.

I'm going to create a new installation which uses ZFS as filesystem - it supports backing up "partitions" while the system continues running. This new installation w/ ZFS is another scenario.

 *Quote:*   

> Also, dd is not really a suitable backup tool, you will end up copying empty space for no real reason.

 

I'd pipe dd output to xz. Something like

```
dd if=/dev/disk/by-label/<name> | xz - > ${HOSTNAME}_$(date -I)_<name>.xz
```

What do you suggest? 

Is there something like ext4-mksapshot?

----------

## khayyam

 *as.gentoo wrote:*   

> Shouldn't it be possible to tell the kernel to use the image, no matter what happened before? Or setting the ACPI state before the kernel checks it, maybe by grub?

 

as.gentoo ... as far as I know that isn't possible, its part of the uswsusp design, if you don't resume the image is invalidated (it's listed as one of "drawbacks" in the tuxonice-internals doc).

 *as.gentoo wrote:*   

> I usually have many programs running holding data in a editable state or just displaying data, e.g. several LibreOffice documents, PDF documents, emacs, browsers having filled forms as well as terminals containing stuff like   tail -f <logfile>*   etc. I like hibernate because everything is there as if you never powered the system down. On a desktop system it's hard reconstruct that state when you called shutdown.

 

Well, I would consider a "backup" the files, and not the state. I think this is possibly the first time I've seen such a thing suggested ... and I don't think I've seen a single tool (for any OS) designed to make a stateful backup.

 *as.gentoo wrote:*   

> So I wish I could just freeze the complete systems state and restore everything in a case of a catastrophe caused by the user, by hardware or software.

 

Again, I'd consider the files the target of a backup ... backing up and retsoring these is risky enough without attempting to include the state of the OS. 

 *as.gentoo wrote:*   

> 
> 
> ```
> dd if=/dev/disk/by-label/<name> | xz - > ${HOSTNAME}_$(date -I)_<name>.xz
> ```
> ...

 

That will compress the resulting image but you're still read/writing the entire partition. 

 *as.gentoo wrote:*   

> What do you suggest? Is there something like ext4-mksapshot?

 

I'm fairly sure ext4 can't be snapshoted ... generally for backups you use rsync, tar, or some wrapper for same.

best .. khay

----------

## frostschutz

You have to be aware that if you hibernate (suspend to disk) your system, the system is still running. The filesystems are still mounted. Etc.

If you boot something else, and this something else mounts your (already mounted) filesystems, and changes anything on disk... most Live CDs do all sort of things during boot, if you're booting off USB stick it might end up mounting all disks in search of its LiveCD data, it might enable all swaps partitions it finds, etc.... that already breaks the rules. Whatever you do with your disks (the ones in use by the hibernated system) must be strictly read-only but most Live CDs are not capable of that.

Making a backup using dd (without mounting) while hibernate is fine, but it's difficult to actually achieve.

If you are familiar with Initramfs you could just use your original kernel and a custom initramfs (guide in the gentoo wiki) to give you a busybox shell and dd from there. Since the Initramfs is provided by the bootloader it will not (unless otherwise instructed) change anything on your disks. If it's an Initramfs you create yourself, you're in full control.

Note that since the hibernated system is "still running", for a dd backup to be consistent you would have to dd the whole disk (including the bootloader, swap partition), and if you ever restore this backup you have to let it resume from hibernate, otherwise your backup might simply be inconsistent like a system after power loss. (Basically the same effect when your system does not manage to wake from hibernate and is forced to cold boot instead, which is bad.).

Hibernation is really dangerous in various ways, if you mess with it you can end up with complete data corruption.

----------

## as.gentoo

 *khayyam wrote:*   

> Well, I would consider a "backup" the files, and not the state. I think this is possibly the first time I've seen such a thing suggested ... and I don't think I've seen a single tool (for any OS) designed to make a stateful backup.

 Correct. Backup rather refers to files (or databases) - what I want is a "system image" that can be created at runtime or after hibernation.

 *khayyam wrote:*   

> I think this is possibly the first time I've seen such a thing suggested ... and I don't think I've seen a single tool (for any OS) designed to make a stateful backup.

 If I remember correctly it was possible to hibernate in Windows XP, then use "Acronis TrueImage" to copy your disks or partitions into a file and then resume from the hibernate.sys (or so). I have no means to check that now.

 *khayyam wrote:*   

>  *as.gentoo wrote:*   
> 
> ```
> dd if=/dev/disk/by-label/<name> | xz - > ${HOSTNAME}_$(date -I)_<name>.xz
> ```
> ...

 True. The man page of dd_rescue states that it can recognise empty blocks, so at least writing empty blocks back will be skipped. Maybe it can ask the FS somehow where free blocks are located so reading those blocks could be skipped too. When restoring from the image the content of unused blocks won't matter because the FS knows that it's not using these.

 *khayyam wrote:*   

>  *as.gentoo wrote:*   What do you suggest? Is there something like ext4-mksapshot? I'm fairly sure ext4 can't be snapshoted ... generally for backups you use rsync, tar, or some wrapper for same.

 Right. Many websites and books that are about backups suggest to backup files and create images of complete volumes / disks / partitions.

----------

## as.gentoo

 *frostschutz wrote:*   

> You have to be aware that if you hibernate (suspend to disk) your system, the system is still running. The filesystems are still mounted. Etc.

 I just found https://wiki.gentoo.org/wiki/Custom_Initramfs/Hibernation where that is described well.  :Wink: 

 *frostschutz wrote:*   

> If you boot something else, and this something else mounts your (already mounted) filesystems, and changes anything on disk... most Live CDs do all sort of things during boot,

 Hm, I see. At least the gentoo install CD doesn't mount disks automatically.

 *frostschutz wrote:*   

> If you are familiar with Initramfs you could just use your original kernel and a custom initramfs (guide in the gentoo wiki) to give you a busybox shell and dd from there.

 Somewhat. I use initramfs for some years now, if nothing else because I want /usr to be readonly which is not possible with the default setup.

```
busybox --install -s

exec /bin/sh
```

should provide the shell

 *frostschutz wrote:*   

> Since the initramfs is provided by the bootloader it will not (unless otherwise instructed) change anything on your disks. If it's an Initramfs you create yourself, you're in full control.

 So the process would be

1) hibernate

2) start booting, interrupt booting in order to use the dd(_rescue) provided by the the initramfs to create an image of the disks

3) continue booting (no fsck is done neither mount and nothing is written what shouldn't be changed) until the system fully resumed

 *frostschutz wrote:*   

> Note that since the hibernated system is "still running", for a dd backup to be consistent you would have to dd the whole disk (including the bootloader, swap partition)

 clear *frostschutz wrote:*   

> and if you ever restore this backup you have to let it resume from hibernate

 I see no problem with that - the system will boot, find the hibernation data and automatically resume. Why would that not happen? Do you mean that the resume should not be interrupted by power loss or unplugging a drive or accidentally hitting the reset button? People that have a UPS are lucky in the first case, in the latter cases I'd maybe deserve to have data corruption.

 *Quote:*   

> Hibernation is really dangerous in various ways, if you mess with it you can end up with complete data corruption.

 Thanks for the warning! So far it worked fine when I just hibernated and did nothing but resume then.Last edited by as.gentoo on Wed Mar 30, 2016 12:49 am; edited 1 time in total

----------

## frostschutz

 *as.gentoo wrote:*   

> I just found https://wiki.gentoo.org/wiki/Custom_Initramfs/Hibernation where that is described well. 

 

Yes, I have to find the time to actually finish that page ...  sigh

 *as.gentoo wrote:*   

> 
> 
> So the process would be
> 
> 1) hibernate
> ...

 

Yes, if you are using Initramfs in this fashion anyhow, that should work.

 *Quote:*   

> Why would that not happen?

 

I just mentioned for completeness sake, like if you decide to dd only the home partition.

----------

## as.gentoo

Still, there is something I don't understand. 

I need to do maintenance that can not be done while my system is running. So I

1) hibernate my system

2) boot and stop the boot process for using dd* (to make an image)

3) power down then (do not continue boot)

4) power on and boot from the UltimateBootCD (UBCD) or gentoo install cd and use WHDD to check the disks

5) power on … and do then what?

The ACPI state was set to "no hibernation" by the BIOS or by  the kernel of the UtlimateBootCD (?). How do I make my systems kernel use hibernate-data if the ACPI says "system was not hibernated after  the previous boot"?

BTW: Why would a DOS-"kernel" on the UBCD invalidate hibernation-data? Why would a linux (non-system) kernel alter foreign hibernation-data"? Is the data overwritten or a flag set on the SWAP partition that the hibernate data is invalid?

I guess when I hibernate the system and right after that boot and use the "backup image" to revert to the former state it would work. What if the kernel or a driver caused a crash so that the BIOS/ACPI does not contain "state: hibernated"? One target is having back a fully working system as quick as possible with one or two commands (dd_rescue), especially after suspectedly harmful operations. I wonder why the kernel can set ACPI data but a command issued by root or by the user can't. AFAIK there are tools that can set BIOS passwords and so on…

----------

## frostschutz

Thinking about it some more, it might not actually be possible. Well, I've never tried it, so I don't know what happens when you try to resume but your hardware changed. For example if you replaced your disk, you suddenly would have a different disk, with different identifier, serial number, different size etc. At best it would still use the same SATA port as before. I do not know if such changes will be accepted by a hibernated system. It might reject the new hardware the same way a human body would reject a heart transplant (without serious medication).

So it's possible that making an image during hibernation will be useless (in regards to ability of resuming from the image on different hardware afterwards).

So for maintenance it might be that you should have to shut down properly unless you can make the system believe the hardware didn't change.

I've never tried any of this so...

----------

## as.gentoo

I see. In that case I will try TuxOnIce - if the existence of hibernated data is enough…

Can anybody tell me about her/his experiences - good or bad - here?

----------

