# Data Export and Import in C

> As explained in the Data Export and Import page, eXtremeDB provides a number of methods to export and import a database snapshot or class. These methods are described in the sections below. Note that, in addition to data export/import operations, eXtremeDB provides backup and restore capability for in-memory and persistent databases. (Please view the Database Backup and Restore page for further details.)

> ## Database snapshots

> To stream an image of an in-memory database (transient objects) to a specified file eXtremeDB provides the function `mco_db_save()`:

>         MCO_RET	mco_db_save(	/*IN*/ void * stream_handle,
>                     /*IN*/ mco_stream_write output_stream_writer,
>                     /*IN*/ mco_db_h db);

> Used together with `mco_db_load()` an application can implement robust backup and restore capability:

>         MCO_RET	mco_db_load(	/*IN*/ void *stream_handle,
>                     /*IN*/ mco_stream_read input_stream_reader,
>                     /*IN*/ const char * dbname,
>                     /*IN*/ mco_dictionary_h dict,
>                     /*IN*/ mco_device_t *devices,
>                     /*IN*/ uint2 n_devices,
>                     /*IN*/ mco_db_params_t * db_params );

> With release 7.1, eXtremeDB has simplified the `mco_db_save()` and `mco_db_load()` APIs and the former variants `mco_inmem_save()`, `mco_inmem_load()`, `mco_disk_save()`, `mco_disk_load()` and `mco_disk_load_file()` have been deprecated.

> The simplified version of `mco_db_save()` and `mco_db_load()` allow the application to stream database contents to and from a persistent media file, socket, or pipe, with `CRC` checking, “binary schema evolution” (`BSE`) support and buffering to allow interruption and restart of the streaming process.

> ### Exporting in JSON format

> It is possible to export the entire database contents in JSON format using the `mco_db_json_export()` function.

> ## Saving and Loading individual classes

> It is sometimes desirable to export/ import only data for an individual database class. The following APIs are provided for this purpose:

>         MCO_RET mco_class_save( uint2 class_code, void* stream_handle,
>                       mco_stream_write output_stream_writer,
>                       mco_db_h db );

>         MCO_RET mco_class_load( uint2 class_code, void* stream_handle,
>                       mco_stream_read input_stream_reader,
>                       mco_db_h db );

> Note that there is no transaction handle for these APIs, but rather the `mco_db_h` parameter. This is because a transaction is opened implicitly for the save or load operation. For these operations calculation of `CRC` and Binary Schema Evolution (`BSE`) are always enabled.

> These APIs are compatible with `mco_db_save()` and `mco_db_load()`. This means that the application can save the entire database image with `mco_db_save()` and later load (or reload) the content of a particular class from the full image. Or it is possible to save the content of only one class with `mco_class_save()` and then use this image to create a database with `mco_db_load()`.

> The `class_code` parameter of `mco_class_load()` can be set to 0. In this case the class is determined automatically from the image file. (If it is impossible to determine the class from the image file, the error code `MCO_E_ILLEGAL_PARAM` is returned.)

> ### Exporting in JSON format

> The objects of an individual class can be exported in JSON format using the generated `classname_json_get()` functions.

> ## Saving the database metadata

> It is possible to save the database Metadata (data layout information in xSQL config file format (JSON) suitable for loading into xSQL in order to connect to the database as a client application. To save the metadata from a C application use the following APIs:

>         MCO_RET mco_db_save_metadata(void* stream_handle, mco_stream_write output_stream_writer,
>                             mco_db_h db, mco_bool save_defaults);

> ## Saving the database schema

> It is possible to save the database schema to an external file. From a C application use the following API function:

>         MCO_RET mco_db_save_dictionary(void* stream_handle, mco_stream_write output_stream_writer,
>                             mco_db_h db);

> ## XML export and import APIs

> eXtremeDB provides a set of C APIs to manage export and import of database data in XML format. In addition to the common use of XML interfaces for porting data, these XML interface functions can be used, for instance, in concert with the eXtremeDB event notifications, to cause live data to be shared between eXtremeDB and other systems when an object of interest changes in the database. The XML interfaces can also be used to facilitate simple schema evolution by exporting the database to XML, adding/dropping fields, indexes, and classes, and importing the saved XML into the new database.

> Please see the XML APIs page for a detailed explanation of XML usage in eXtremeDB.

> ## Saving and Loading the Cache for Persistent Databases

> Some C applications with persistent or hybrid databases may need to save the contents of the cache. (See the Cache Management page for further details.) The `mco_disk_save_cache()` function is provided to save the current contents of the persistent database cache to the specified file:

>         MCO_RET mco_disk_save_cache( mco_db_h con, char const* file_path );

> The `mco_disk_load_cache()` function loads the persistent database cache from the specified file.

>         MCO_RET mco_disk_load_cache(	mco_db_h db, const char* file_path );

> For a detailed discussion of caching behavior see the Persistent Database I/O page.