# Shared Memory Segment Emergency Recovery

> Sometimes when a process gets killed or crashes unexpectedly a database shared memory segment may become locked and inaccessible by other processes (either processes that are already running or newly launched processes) yet there may be a need to rescue valuable data remaining in this in-memory database.

> The `shdump` utility can be used to create an emergency backup for such a segment.

> - `shdump` should be used as "a last resort" only, when all other options are exhausted and right before the final cleanup/restart of "frozen" application(s). This tool should never be combined with regular database operations. (Please refer to Backup and restore if you need to backup data routinely.)
> - It is not guaranteed that data will be saved completely or that all transactions will be completed.
> - On the Windows platform at least one process holding the affected shared memory segment should remain alive (even if stuck/non-operational).

> ## Rescue procedure overview

> 1. Dump the database segment into a file with shdump.
> 2. Create a temporary database and load the file into it, then make a regular backup from this temporary database. This step can be performed on a different machine, so that the affected shared memory segment may be left in memory.
> 3. Restore the backup into a regular eXtremeDB instance, check the data and use it normally.

> ## Usage

>         shdump [-dr] [-s size] [-c max_connections] [-a address] [-p page_size] [ -n dbname] segment_name);

> - `size` is the size of the memory segment in bytes. Suffix-modifiers 'k', 'm', 'g' meaning kilobytes, megabytes and gigabytes can be appended.
> - `address` is an address to which to restore segment (in HEX format: `0xHHHHH`)
> - `page_size` is database page size. Default value is 512 bytes. Used only when restoring database
> - `dbname` is the name of resulting database. Used only for restore

> ## Example

> 1. Dump step

>            shdump  -s 128M segmentName

>    This will create file `segmentName.dmp` with size of 128Mb and save the contents of the shared memory segment named "`segmentName`".

> 2. Backup step

>            shdump -a 0x20000000 -r -p 4096 -n kbss_stds -s 128m -c 256 segmentName

>    This command will load the segment dump file "`segmentName.dmp`" into memory, open an IM database using that segment and create backupdb.bak file with the regular eXtremeDB backup.

>    This file is suitable for loading by xsql with the `-image` option or with the mco_db_load() eXtremeDB C API call.

> 3. Optional XSQL check step

>    Assume that database parameters are described in the corresponding `xsql.cfg` configuration file:

>            xsql  -c xsql.cfg -image backupdb.bak