backup

Syntax

backup(backupDir, dbPath|sqlObj, [force=false], [parallel=false], [snapshot=true], [tableName], [partition], [keyPath])

Details

Back up all or specified partitions of a distributed table. Return an integer indicating the number of partitions that have been backed up successfully. It must be executed by a logged-in user.

Note:

  • For a database created with chunkGranularity='DATABASE', it can only be backed up with SQL statements.

  • If backupDir is an AWS S3 directory, the configuration file of the data node must specify preloadModules=plugins::awss3, and configure s3AccessKeyId, s3SecretAccessKey and s3Region. These parameters are optional. If not configured, the system will pass empty credentials to the AWS plugin, which can automatically retrieve the authentication information. See AWS account for details.

The following table compares the 2 types of backup:

Feature copying files (specifying parameter dbPath) SQL statements (specifying parameter sqlObj)
back up an entire database √ ×
data consistency fully guaranteed partially guaranteed
incremental backup of modified and added partitions √ √
incremental backup of deleted partitions √ ×
resume data transmission from a breakpoint √ ×
back up data with filter conditions × √
performance lower memory usage higher memory usage

Parameters

backupDir is a string indicating the directory to save the backup. For an AWS S3 directory, it must begin with s3://.

dbPath is a string indicating the database path. If specified, back up the database by copying partitions. If backupDir is an AWS S3 directory, only dbPath can be specified.

sqlObj is metacode of SQL statements indicating the data to be backed up. If specified, only the queried data is backed up.

force (optional) is a Boolean value. True means to perform a full backup, otherwise to perform an incremental backup.

parallel (optional) is a Boolean value indicating whether to back up partitions in parallel. The default value is false.

The following parameters only take effect when dbPath is specified:

snapshot (optional) is a Boolean value indicating whether to synchronize the deletion of tables/partitions to the backup database. It only takes effect when the parameter partition is empty. The default value is true, indicating the system will remove the deleted table/partitions from the backup files.

tableName (optional) is a STRING scalar or vector indicating the name of table to be backed up. If unspecified, all tables of the database are backed up.

partition (optional) indicates the partitions to be backed up. It can be:

  • a STRING scalar or vector indicating the path(s) to one or multiple partitions of a database, and each path starts with "/". Note that for a compo-partitioned database, the path must include all partition levels.

  • filter condition(s). A filter condition can be a scalar or vector where each element represents a partition.

    • For a single-level partitioning database, it is a scalar.

    • For a compo-partitioned database, it is a tuple composed of filter conditions with each element for a partition level. If a partition level has no filter condition, the corresponding element in the tuple is empty.

  • unspecified to indicate all partitions.

keyPath (optional, Linux only) is a STRING scalar that specifies the path to the TDE key file used for backup encryption. If left empty, no encryption will be applied.

Returns

An integer scalar.

Notes

Backups created by copying partition files with dbPath and backups of SQL query results created with sqlObj must use different backupDir directories. If a directory already contains backup files created with one method, using the other method in the same directory fails.

Examples

Run the following examples in order. Each backup scenario uses a separate subdirectory to avoid mixing backup methods or including data from earlier backups. Example 5 reuses its directory to demonstrate the snapshot parameter.

Create a DFS database dfs://compoDB

n=1000000
ID=rand(100, n)
dates=2017.08.07..2017.08.11
date=rand(dates, n)
x=rand(10.0, n)
t=table(ID, date, x)

dbDate = database(, VALUE, 2017.08.07..2017.08.11)
dbID=database(, RANGE, 0 50 100);
db = database("dfs://compoDB", COMPO, [dbDate, dbID])
pt = db.createPartitionedTable(t, `pt, `date`ID)
pt.append!(t)

Example 1. Back up the table pt.

backup(backupDir="/home/DolphinDB/backup/sql_all", sqlObj=<select * from loadTable("dfs://compoDB","pt")>, force=true);
// output: 10

Example 2. Back up the partitions of table pt with date>2017.08.10.

backup(backupDir="/home/DolphinDB/backup/sql_filtered", sqlObj=<select * from loadTable("dfs://compoDB","pt") where date>2017.08.10>, force=true);
// output: 2

Example 3. Back up data in the date partition 2017.08.09 of table pt.

The database created above is a composite partitioned database: the first level is a VALUE partition on date, and the second level is a RANGE partition on ID. Here partition specifies the date value 2017.08.09, so backup directly matches the first-level partition 20170809 and backs up all its second-level partitions, i.e., /20170809/0_50 and /20170809/50_100. It does not filter rows by date during backup.

backup(backupDir="/home/DolphinDB/backup/partition_date", dbPath="dfs://compoDB", force=true, tableName=`pt, partition=2017.08.09)
// output: 2

Example 4. Back up tables in a database.

(1) Back up tables

// Create the second table under database dfs://compoDB
pt1 = db.createPartitionedTable(t, `pt1, `date`ID)
pt1.append!(t)

// Back up both tables in the database
backup(backupDir="/home/DolphinDB/backup/database_all",dbPath="dfs://compoDB",force=true);
// output: 20

(2) Back up specified partitions

// Back up five specified partitions of table pt
partitions=["/20170807/0_50","/20170808/0_50","/20170809/0_50","/20170810/0_50","/20170811/0_50"]
backup(backupDir="/home/DolphinDB/backup/partition_paths",dbPath="dfs://compoDB",force=true,tableName=`pt,partition=partitions);
// output: 5

(3) Back up partitions with filter conditions. Note that for a range domain, any value within the range can be specified to indicate the entire partition.

// Back up partition 50_100 under the first-level partition 20170807
partitions=[2017.08.07,50]
backup(backupDir="/home/DolphinDB/backup/partition_filter_one",dbPath="dfs://compoDB",force=true,tableName=`pt,partition=partitions);
// output: 1
// Back up partition 0_50 under all first-level partitions
partitions=[,[0]]
backup(backupDir="/home/DolphinDB/backup/partition_filter_all",dbPath="dfs://compoDB",force=true,tableName=`pt,partition=partitions);
// output: 5

Example 5. Demonstrate the snapshot parameter.

// First back up all 10 partitions of table pt
backup(backupDir="/home/DolphinDB/backup/snapshot",dbPath="dfs://compoDB",force=true,tableName=`pt);
// output: 10

// Delete partition "/20170811/0_50" from table pt
db.dropPartition("/20170811/0_50",`pt)
// Back up again with snapshot=false
backup(backupDir="/home/DolphinDB/backup/snapshot",dbPath="dfs://compoDB",force=true,snapshot=false,tableName=`pt);
// output: 9
// Restore all 10 partitions, including /20170811/0_50, which is retained in the backup
restoredPartitions = restore(backupDir="/home/DolphinDB/backup/snapshot",dbPath="dfs://compoDB",tableName=`pt,partition="%",force=true)
restoredPartitions.size()
// output: 10

// Delete partition "/20170811/0_50" from table pt again
db.dropPartition("/20170811/0_50",`pt)

// Back up again with snapshot=true
backup(backupDir="/home/DolphinDB/backup/snapshot",dbPath="dfs://compoDB",force=true,snapshot=true,tableName=`pt);
// output: 9
// Restore only 9 partitions, excluding /20170811/0_50, which has been removed from the backup
restoredPartitions = restore(backupDir="/home/DolphinDB/backup/snapshot",dbPath="dfs://compoDB",tableName=`pt,partition="%",force=true)
restoredPartitions.size()
// output: 9

Related functions: backupDB, backupTable, restore, migrate