# Backup and Restore Database

This chapter introduces how to store data with TOPIO and manage the database.

After the miner joined the network, TOPIO will synchronize the data on the chain automatically.

The db command is shown in the following table.

Command Description
topio db backup Backup miner's local database.
topio db listBackups List all backups.
topio db restore Restore database.
topio db compact Defragment data.
topio db prune Clean up historical invalid data.

# Database Directory

WARNING

It is recommended that you do not modify the directory name and file name under the database directory, otherwise some of the TOPIO functions will not be able to be used properly!

TOPIO database directory shown as below.

Snap52
  • db: Store all the data on the chain.
  • log: Store the logs during TOPIO running.
  • keystore: Store keystore file.

# Backup Database

Note:

  • The first backup of the database is a full backup. For subsequent incremental backups, please use the first backup path. If you use a different path than the first backup, it may make your backup database unavailable!
  • It is recommended that you backup the testnet network database and the mainnet database in different directories.

Request

topio db backup 

Request Parameters

Parameter Name Required Default Value Type Description
backupdir Yes - String Database backup directory.

Options

Option Name Value Type Description
-h, --help - - Check the command's help information.
-d, --dir - String Database source directory, the default is the current data directory.

Request Sample

In the following request sample, the database source directory is /home/lily/Topnetwork, and the database backup directory is /home/cathy.

topio db backup -d /home/lily/Topnetwork /home/cathy

Response Schema

  • Successful

    Database backup operating successfully.
    DBversion : 1
    
  • Failed

    Backup failed
    Error: the /home/db or /home/pdb does not exist.
    

# List Backups

Request

topio db listBackups

Request Parameters

Parameter Name Required Default Value Type Description
backupdir Yes - String Database backup target directory.

Options

Option Name Value Type Description
-h, --help - - Check the command's help information.

Request Sample

Check the backed up database version in the /home/cathy directory.

topio db listBackups /home/cathy

Response Schema

  • Successful

    [root@localhost topio-0.0.0.0-debug]# topio db listBackups /home/cathy
    DBversion:1,timestamp:2020-06-05 07:04:02.
    DBversion:2,timestamp:2020-06-05 07:07:29.
    
  • Failed

    None.

# Restore Database

Execute the command topio DB list_dbVersion to retrieve all database versions under the backup directory before restoring the database.

Select a version and restore to an empty directory. If no version is specified, the latest version is restored by default.

Request

topio db restore

Request Parameters

Parameter Name Required Default Value Type Description
backupdir Yes - String The backup directory of the database.

Options

Option Name Value Type Description
-h, --help - - Check the command's help information.
-d, --dir - String The target directory for the database restore, and must be empty.
-D, --DBversion - Integer The backed up database version, if not specified, restores the latest version by default.

Request Sample

Restore the database version 2 in the path /home/cathy to the path /home/peter.

topio db restore -d /home/peter /home/cathy -D 2

Response Schema

  • Successful

    Database restore operating successfully.
    
  • Failed

    Restore failed
    Error: The target dir for restore is not empty, please input an empty one.
    

# Defragment Data

Reduce DB footprint by defragmenting data, which takes 5 - 10 minutes to execute.

Request

topio db compact

Request Parameters

none.

Request sample

Follow the steps below to defragment data:

  1. Before defragmenting data, you need to run the following command to stop the TOPIO process:

    ps -ef |grep topio|grep -v 'grep'|awk '{print $2}'|xargs kill -9
    
  2. Perform data defragmentation:

    topio db compact
    
  3. Restart TOPIO.

    topio node startnode
    

Response Sample

After data defragmentation is complete, the following information is returned:

xdb_impl::open(),Options.compression=4,bottommost_compression=4
xdb_impl::close,db_root_dir=/chain/db_v3
compact database ok.

# Clean Up Invalid Data

Clean up historical invalid data before a specified checkpoint, the execution takes 3 - 5 minutes.

Only applicable to Advance, Validator, Edge miners.

Request

topio db prune

Request Parameters

none.

Request Sample

Follow the steps below to perform invalid data cleanup:

  1. Before cleaning invalid data, you need to run the following command to stop the TOPIO process:

    ps -ef |grep topio|grep -v 'grep'|awk '{print $2}'|xargs kill -9
    
  2. Perform invalid data cleanup:

    topio db prune
    
  3. Restart TOPIO.

    topio node startnode
    

Response Sample

  • Successful

    init db...
    xdb_impl::open(),Options.compression=4,bottommost_compression=4
    start prune db()...
    total 43637 units in db
    start prune unit account!
    start table account!
    xdb_impl::close,db_root_dir=/chain/db_v3
    prune database ok.
    
  • Exception

    If the following information is returned, it means that you have not set the default account yet, you need to use [topio wallet setDefaultAccount](/Tools/TOPIO/CliUsage/wallet/#set default account) to set the default account.

    please set default account.