# Data Export and Import in Java

> ## 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       transNo;
>             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:

>         /**
>         *  Size of backup counters array, bytes, power of two, ignored if disk_max_database_size set. Default is 0 - disable backup feature
>         */
>         public long        backupMapSize;

>         /**
>         *  Number of pages for last exclusive pass of backup procedure, set to zero to disable treshold. Default is 0
>         */
>         public int         backupMinPages;

>         /**
>         * Max number of passes before exclusive pass of backup procedure. Default is 10
>         */
>         public int         backupMaxPasses;

>         /**
>         * 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
>         */
>         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 an in-memory database (transient objects) 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 boolean 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 boolean saveClass(String filePath, Class cls)

>         public boolean loadClass(String filePath, Class cls, boolean 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 boolean 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 boolean saveDictionary(String databaseDictionaryFilePath)