# Data Export and Import in C#

> ## Database backup

> The eXtremeDB database backup and restore functionality is implemented by the Connection class methods `CreateBackup()` and `RestoreBackup()`. Please see SDK sample IncBackup for implementation details.

> ### CreateBackup()

> The `CreateBackup()` method has two forms:

>         public virtual void CreateBackup(String fileName, String label, Database.BackupType type);

>         public virtual void CreateBackup(String fileName, String label, Database.BackupType type,
>                             int compressionLevel, String cipher);

> where the backup `type` and `compressionLevel` are defined as follows:

>         public enum BackupType {
>             Auto, Snapshot, Incremental
>         };
>         public const int BACKUP_FLAG_COMPRESSED = 1;
>         public const int BACKUP_FLAG_ENCRYPTED  = 2;

> ### RestoreBackup()

> Similarly the `RestoreBackup()` method can be called with or without an encryption key:

>         public virtual void RestoreBackup(String fileName, String label);

>         public void RestoreBackup(String fileName, String label, String cipher);

> ### ListBackup()

> In addition the method `ListBackup()` can be called to retrieve information from a backup file in the following structure:

>         public virtual Database.BackupInfo [] ListBackup(String fileName);

>         public static class BackupInfo
>         {
>             public int        ProtocolVersion;
>             public BackupType Type;
>             public int        Flags;
>             public int        BackupNo;
>             public long       Timestamp;
>             public long       Size;
>             public long       Offset;
>             public int        Crc;
>             public long T ransNo;
>             public int        MemPageSize;
>             public int        DiskPageSize;
>             public long       NPagesTotal;
>             public String     DbName;
>             public String     Label;
>         }

> ### Additional backup parameters

> Further backup parameters can be specified in the Database.Parameters used for the `Open()` method:

>         /// <summary
>         ///  size of backup counters array, bytes, power of two, ignored if disk_max_database_size set. Default is 0 - disable backup feature
>         /// </summary>
>         public long BackupMapSize;

>         /// <summary>
>         ///  number of pages for last exclusive pass of backup procedure, set to zero to disable treshold. Default is 0
>         /// </summary>
>         public int BackupMinPages;

>         /// <summary>
>         /// Max number of passes before exclusive pass of backup procedure. Default is 10
>         /// </summary>
>         public int BackupMaxPasses;

>         /// <summary>
>         /// Name of a file will be used to store backup temporary data on mco_db_close() call.
>         /// Optional, set to null for default "<persistent-storage>.bmap" file locate at the same location as persistent storage file
>         /// </summary>
>         public String BackupMapFile;

> (These parameters correspond to `backup_map_size`, `backup_min_pages`, `backup_max_passes`, `backup_map_filename` in `struct mco_db_params_t` in the C API.)

> ## Database snapshots

> To save an image of a database to a specified file, the Connection class provides method `SaveSnapshot()`. This snapshot can later be loaded by the Database method `Open()` if the corresponding file path is specified in Database.Parameters property `DatabaseSnapshotFilePath`. (A file is always created, or overwritten if it exists). The method returns `true` if the snapshot was successfully saved, `false` if the specified file can not be opened. The method signature is:

>         public bool SaveSnapshot(String databaseSnapshotFilePath)

> ## Saving and Loading individual classes

> It is sometimes desirable to export / import only data for an individual database class. The Connection class provides the following methods for this purpose:

>         public bool SaveClass(String filePath, Type cls)

>         public bool LoadClass(String filePath, Type cls, bool doMerge)

> Note that if the `doMerge` argument is `false`, the existing data content of this class is cleared before loading from the image file. If `true` the loaded objects are "added" to the class. The methods return `true` if the objects were successfully saved or loaded, `false` if the specified file can not be opened.

> ## 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. The Connection class provides method `SaveMetadata()`. The method signature is:

>         public void SaveMetadata(String databaseMetadataFilePath, boolean saveDefaults)

> ## Saving the database schema

> It is possible to save the database schema to an external file. The Connection class provides method `SaveDictionary()`.

>         public void SaveDictionary(String databaseDictionaryFilePath)