©2015 - 2022 Chad’s Technoworks. Disclaimer and Terms of Use

Chad’s TechnoWorks My Journal On Technology

Information Technology

COUCHBASE BACKUP AND RECOVERY

COUCHBASE BACKUP


Couchbase provides a backup tool - cbbackup - that would allow us to create full and incremental backups of the entire cluster, or specific bucket, or data of a single node.

In this article, we'll focus mostly on a backup/recovery of an entire cluster which is the most common production implementation.


For a full documentation of cbbackup options and usage, please refer to the Couchbase Developer doc - cbbackup

For understanding the concepts of incremental backups, see Differential and Cumulative Backups doc.


From its documentation, I am re-posting the following important tips -


Tip: We recommended that cbbackup output be generated on a file system local to the server. Specifically, back up to a dedicated partition. A dedicated partition prevents the backup from filling the Couchbase Server data stores and operating system partitions.

Caution: Avoid routing the cbbackup output to remote share file systems (NFS). This is because cbbackup files are based on sqlite format which have issues being written to remote file systems.


The following is a brief description of the parameter options of the cbbackup command:


[couchbas@lxnode5 ~]$ cbbackup -h

Usage: cbbackup [options] source backup_dir


Online backup of a couchbase cluster or server node.


Examples:

   These are a full backup plus two incremental backups for a cluster.

       cbbackup http://HOST:8091 /backup-42

       cbbackup http://HOST:8091 /backup-42

       cbbackup http://HOST:8091 /backup-42


   These are a full backup plus two differentials and one accumulative for a single node.

       cbbackup couchbase://HOST:8091 /backup-43 [-m full] --single-node

       cbbackup couchbase://HOST:8091 /backup-43 [-m diff] --single-node

       cbbackup couchbase://HOST:8091 /backup-43 [-m diff] --single-node

       cbbackup couchbase://HOST:8091 /backup-43 -m accu --single-node


Note: A full backup task is always triggered for a new sink location

   no matter what backup mode is specified.



Options:

  -h, --help            show this help message and exit

  -b BUCKET_SOURCE, --bucket-source=BUCKET_SOURCE

                        single bucket from source to backup

  --single-node         use a single server node from the source only,

                        not all server nodes from the entire cluster;

                        this single server node is defined by the source URL

  -m MODE, --mode=MODE  backup mode: full, diff or accu [default:diff]

  -i ID, --id=ID        Transfer only items that match a vbucketID

  -k KEY, --key=KEY     Transfer only items with keys that match a regexp

  -n, --dry-run         No actual transfer; just validate parameters, files,

                        connectivity and configurations

  -u USERNAME, --username=USERNAME

                        REST username for source cluster or server node

  -p PASSWORD, --password=PASSWORD

                        REST password for source cluster or server node

  -s, --ssl             Transfer data with SSL enabled

  -t THREADS, --threads=THREADS

                        Number of concurrent workers threads performing the

                        transfer

  -v, --verbose         verbose logging; more -v's provide more verbosity. Max

                        is -vvv

  --silent              Reduce logging verbosity to only include errors

  -x EXTRA, --extra=EXTRA

                        Provide extra, uncommon config parameters;

                        comma-separated key=val(,key=val)* pairs


  Available extra config parameters (-x):

    backoff_cap=10 (Max backoff time during rebalance period);

    batch_max_bytes=400000 (Transfer this # of bytes per batch);

    batch_max_size=1000 (Transfer this # of documents per batch);

    cbb_max_mb=100000 (Split backup file on destination cluster if it

    exceeds MB); data_only=0 (For value 1, only transfer data from a

    backup file or cluster); dcp_consumer_queue_length=1000 (A DCP client

    needs a queue for incoming documents/messages. A large length is more

    efficient, but memory proportional to length*avg. doc size. Below

    length 150, performance degrades significantly.); design_doc_only=0

    (For value 1, transfer design documents only from a backup file or

    cluster); flow_control=1 (For value 0, disable flow control to improve

    throughput); max_retry=10 (Max number of sequential retries if

    transfer fails); mcd_compatible=1 (For value 0, display extended

    fields for stdout output.); nmv_retry=1 (0 or 1, where 1 retries

    transfer after a NOT_MY_VBUCKET message); recv_min_bytes=4096 (Amount

    of bytes for every TCP/IP call transferred); rehash=0 (For value 1,

    rehash the partition id's of each item; this is needed when

    transferring data between clusters with different number of

    partitions, such as when transferring data from an OSX server to a

    non-OSX cluster); report=5 (Number batches transferred before updating

    progress bar in console); report_full=2000 (Number batches transferred

    before emitting progress information in console); seqno=0 (By default,

    start seqno from beginning.); try_xwm=1 (Transfer documents with

    metadata. 0 should only be used if you transfer from 1.8.x to 1.8.x);

    uncompress=0 (For value 1, restore data in uncompressed mode)

[couchbas@lxnode5 ~]$



TEST BACKUP CONNECTION

The following is an example of simulating a backup without dumping data.

This is useful for checking the parameters supplied to the cbbackup command for validity prior to running the actual procedure or embedding to a script.


Response example of a wrong password supplied:

[couchbas@lxnode5 backup]$ cbbackup http://lxnode5.vlabs.net:8091 /app/couchbase/inst1/backup -u Administrator -p - -n

error: unable to access REST API: lxnode5.vlabs.net:8091/pools/default/buckets; please check source URL, server status, username (-u) and password (-p)

[couchbas@lxnode5 backup]$


Response example of invalid destination directory level:

[couchbas@lxnode5 backup]$ cbbackup http://lxnode5.vlabs.net:8091 /app/couchbase/backup/week1 -u Administrator -p mypass -n

error: unknown type of sink: /app/couchbase/backup/week1

[couchbas@lxnode5 backup]$


Response example of correct parameters:

[couchbas@lxnode5 backup]$ cbbackup http://lxnode5.vlabs.net:8091 /app/couchbase/inst1/backup -u Administrator -p mypass -n

done, but no data written due to dry-run

[couchbas@lxnode5 backup]$



FULL BACKUP OF CLUSTER

NOTE: If there's no -m parameter supplied to the command, the default will be diff mode. If there is no full backup exist to use as reference, the command will proceed to full backup.

 

example:

[couchbas@lxnode5 backup]$ cbbackup http://lxnode5.vlabs.net:8091 /app/couchbase/inst1/backup/test1 -u Administrator -p mypass

  [####################] 100.0% (4/estimated 4 msgs)

bucket: CRUD_Lab, msgs transferred...

       :                total |       last |    per sec

 byte  :                  668 |        668 |      336.8

.

bucket: default, msgs transferred...

       :                total |       last |    per sec

 byte  :                  668 |        668 |      443.1

  [####################] 100.0% (6/estimated 6 msgs)

bucket: syncGW_CRUD_Lab, msgs transferred...

       :                total |       last |    per sec

 byte  :                 2216 |       2216 |      818.7

  [####################] 100.0% (3/estimated 3 msgs)

bucket: sync_gateway, msgs transferred...

       :                total |       last |    per sec

 byte  :                 2312 |       2312 |     1309.6

  [####################] 100.0% (31569/estimated 31569 msgs)

bucket: travel-sample-dc2, msgs transferred...

       :                total |       last |    per sec

 byte  :             31052988 |   31052988 |   737589.0

done

[couchbas@lxnode5 backup]$


Couchbase creates a single backup file (data-????.cbb) for the data of each bucket - see sample files generated below.


Below is an example of generated backup files. Notice that there are directories created for each bucket that had been backup.


[couchbas@lxnode5 backup]$ cd test1

[couchbas@lxnode5 test1]$ ls -l

total 4

drwxr-xr-x. 3 couchbas nosql 4096 Feb 29 11:00 2016-02-29T170019Z

[couchbas@lxnode5 test1]$ cd *

[couchbas@lxnode5 2016-02-29T170019Z]$ ls -l

total 4

drwxr-xr-x. 7 couchbas nosql 4096 Feb 29 11:00 2016-02-29T170019Z-full

[couchbas@lxnode5 2016-02-29T170019Z]$ cd *

[couchbas@lxnode5 2016-02-29T170019Z-full]$ ls -l

total 20

drwxr-xr-x. 4 couchbas nosql 4096 Feb 29 11:00 bucket-CRUD_Lab

drwxr-xr-x. 2 couchbas nosql 4096 Feb 29 11:00 bucket-default

drwxr-xr-x. 3 couchbas nosql 4096 Feb 29 11:00 bucket-sync_gateway

drwxr-xr-x. 4 couchbas nosql 4096 Feb 29 11:00 bucket-syncGW_CRUD_Lab

drwxr-xr-x. 4 couchbas nosql 4096 Feb 29 11:01 bucket-travel-sample-dc2

[couchbas@lxnode5 2016-02-29T170019Z-full]$ cd bucket-travel-sample-dc2

[couchbas@lxnode5 bucket-travel-sample-dc2]$ ls -l

total 12

-rw-r--r--. 1 couchbas nosql  697 Feb 29 11:01 index.json

drwxr-xr-x. 2 couchbas nosql 4096 Feb 29 11:01 node-172.16.33.88%3A8091

drwxr-xr-x. 2 couchbas nosql 4096 Feb 29 11:01 node-172.16.33.89%3A8091

[couchbas@lxnode5 bucket-travel-sample-dc2]$ cd node-172.16.33.88%3A8091

[couchbas@lxnode5 node-172.16.33.88%3A8091]$ ls -l

total 18316

-rw-r--r--. 1 couchbas nosql 18714624 Feb 29 11:01 data-0000.cbb

-rw-r--r--. 1 couchbas nosql    15584 Feb 29 11:01 failover.json

-rw-r--r--. 1 couchbas nosql       12 Feb 29 11:00 meta.json

-rw-r--r--. 1 couchbas nosql     5522 Feb 29 11:01 seqno.json

-rw-r--r--. 1 couchbas nosql     8082 Feb 29 11:01 snapshot_markers.json

[couchbas@lxnode5 node-172.16.33.88%3A8091]$


If your data is of hundreds of gigabytes in size or even perhaps in terabytes, it is better to split-up the backup data file into smaller chunks that an OS File System can support.

You would use the parameter option -x cbb_max_mb=<value> to split the backup file. Normally you would size the backup files up to 2gb in order for the files to be compatible to be stored on 32-bit file systems, but if you have a 64-bit FS then going higher >2gb file size wouldn't be a problem.



Example of split backup files in 5MB size:


[couchbas@lxnode5 backup]$ cbbackup http://lxnode5.vlabs.net:8091 /app/couchbase/inst1/backup/test2 -u Administrator -p mypass -m full -x cbb_max_mb=5

  [####################] 100.0% (4/estimated 4 msgs)

bucket: CRUD_Lab, msgs transferred...

       :                total |       last |    per sec

 byte  :                  668 |        668 |      329.9

.

bucket: default, msgs transferred...

       :                total |       last |    per sec

 byte  :                  668 |        668 |      455.1

  [####################] 100.0% (6/estimated 6 msgs)

bucket: syncGW_CRUD_Lab, msgs transferred...

       :                total |       last |    per sec

 byte  :                 2216 |       2216 |      976.9

  [####################] 100.0% (3/estimated 3 msgs)

bucket: sync_gateway, msgs transferred...

       :                total |       last |    per sec

 byte  :                 2312 |       2312 |     1479.4

  [####################] 100.0% (31569/estimated 31569 msgs)

bucket: travel-sample-dc2, msgs transferred...

       :                total |       last |    per sec

 byte  :             31052988 |   31052988 |   624102.4

done

[couchbas@lxnode5 backup]$


[couchbas@lxnode5 backup]$ cd /app/couchbase/inst1/backup/test2

[couchbas@lxnode5 test2]$ ls -l

total 4

drwxr-xr-x. 3 couchbas nosql 4096 Feb 29 11:24 2016-02-29T172455Z

[couchbas@lxnode5 test2]$ cd *

[couchbas@lxnode5 2016-02-29T172455Z]$ ls -l

total 4

drwxr-xr-x. 7 couchbas nosql 4096 Feb 29 11:25 2016-02-29T172455Z-full

[couchbas@lxnode5 2016-02-29T172455Z]$ cd *

[couchbas@lxnode5 2016-02-29T172455Z-full]$ ls -l

total 20

drwxr-xr-x. 4 couchbas nosql 4096 Feb 29 11:24 bucket-CRUD_Lab

drwxr-xr-x. 2 couchbas nosql 4096 Feb 29 11:24 bucket-default

drwxr-xr-x. 3 couchbas nosql 4096 Feb 29 11:25 bucket-sync_gateway

drwxr-xr-x. 4 couchbas nosql 4096 Feb 29 11:25 bucket-syncGW_CRUD_Lab

drwxr-xr-x. 4 couchbas nosql 4096 Feb 29 11:25 bucket-travel-sample-dc2

[couchbas@lxnode5 2016-02-29T172455Z-full]$ cd bucket-travel-sample-dc2

[couchbas@lxnode5 bucket-travel-sample-dc2]$ ls -l

total 12

-rw-r--r--. 1 couchbas nosql  697 Feb 29 11:25 index.json

drwxr-xr-x. 2 couchbas nosql 4096 Feb 29 11:25 node-172.16.33.88%3A8091

drwxr-xr-x. 2 couchbas nosql 4096 Feb 29 11:25 node-172.16.33.89%3A8091

[couchbas@lxnode5 bucket-travel-sample-dc2]$ cd node-172.16.33.88%3A8091

[couchbas@lxnode5 node-172.16.33.88%3A8091]$ ls -l

total 18604

-rw-r--r--. 1 couchbas nosql 6880256 Feb 29 11:25 data-0000.cbb

-rw-r--r--. 1 couchbas nosql 6877184 Feb 29 11:25 data-0001.cbb

-rw-r--r--. 1 couchbas nosql 5250048 Feb 29 11:25 data-0002.cbb

-rw-r--r--. 1 couchbas nosql   15584 Feb 29 11:25 failover.json

-rw-r--r--. 1 couchbas nosql      12 Feb 29 11:25 meta.json

-rw-r--r--. 1 couchbas nosql    5522 Feb 29 11:25 seqno.json

-rw-r--r--. 1 couchbas nosql    8082 Feb 29 11:25 snapshot_markers.json

[couchbas@lxnode5 node-172.16.33.88%3A8091]$


Note the file size of the generated split files (data-????.cbb)




SAMPLE COUCHBASE BACKUP SCRIPT


#!/bin/bash

# File: couchbase_backup.sh

# Author: Chad Dimatulac

# Description: This is a sample backup for Couchbase.

#    A full backup kicks in at a designated day (i.e. Saturday) and will have a cumulative backup on succeeding days.

#    The script uses the BACKUP_PATH/current as its active directory path to store the backup for the week.

#    As the week cycles to the next full backup, the current dir data will be moved to archive and an empty

#    current dir will be available to the backup process.

# To run:  

# nohup ./couchbase_backup.sh &

#

LOG_DATE=`date +%Y%m%d%H%M`

DAY_OF_WEEK=`date +%a`

CB_HOME=/app/couchbase/inst1/opt/couchbase

PATH=$PATH:$CB_HOME/bin ; export PATH

CBBACKUP=$CB_HOME/bin/cbbackup

BACKUP_PATH=/app/couchbase/inst1/backup

CLUSTER_NODE_URL=http://lxnode5.vlabs.net:8091

RSTATUS=1


HNAME=`hostname`

RTUSER=`id |cut -d"(" -f2 | cut -d ")" -f1`

CBUSER=couchbas

DBU=Administrator

DBP=mypass

FBU_DAY=Sat


#-- check if a directory for backup exist

if [ ! -d $BACKUP_PATH ];

then

  mkdir -p $BACKUP_PATH

fi


#-- check if a directory for backup log exist

if [ ! -d $BACKUP_PATH/log ];

then

  mkdir -p $BACKUP_PATH/log

fi


#-- check required backup sub dir (current,archive)

if [ ! -d $BACKUP_PATH/current ];

then

  mkdir $BACKUP_PATH/current

fi  

if [ ! -d $BACKUP_PATH/archive ];

then

  mkdir $BACKUP_PATH/archive

fi  

   

#-- Select backup mode to use


if [ "$DAY_OF_WEEK" = "$FBU_DAY" ]

then

  #-- Full backup

  CBB_MODE=full

else

  #-- Cumulative backup

  FCOUNT=`find $BACKUP_PATH/current -name "*full" -print | wc -l`

  if [ $FCOUNT -eq 0 ];

  then

    CBB_MODE=full

  else

    CBB_MODE=accu

  fi

fi


#-- Initialize the log file.


CBB_LOG_FILE=${BACKUP_PATH}/log/couchbase_backup_${CBB_MODE}_${LOG_DATE}.log

 

echo >> $CBB_LOG_FILE

chmod 666 $CBB_LOG_FILE

echo Runtime Script: $0 >> $CBB_LOG_FILE

echo Runtime User: $RTUSER >> $CBB_LOG_FILE

echo PID: $$ >> $CBB_LOG_FILE

echo Hostname: $HNAME >> $CBB_LOG_FILE

echo Couchbase OS User: $CBUSER >> $CBB_LOG_FILE

echo ==== started on `date` ==== >> $CBB_LOG_FILE

echo >> $CBB_LOG_FILE


#-- Run the backup

if [ ! -f $BACKUP_PATH/.cbb_lock ];

then


   #-- set lock file

   echo $$ >> $BACKUP_PATH/.cbb_lock

   

   #-- perform backup

   if [ "$CBB_MODE" = "full" ]

   then

     mv $BACKUP_PATH/current $BACKUP_PATH/archive/cb_backup_$LOG_DATE

     mkdir $BACKUP_PATH/current

   fi

   echo Backup Mode: $CBB_MODE >> $CBB_LOG_FILE

   CMD_STR="$CBBACKUP $CLUSTER_NODE_URL $BACKUP_PATH/current -u $DBU -p $DBP -m $CBB_MODE -x cbb_max_mb=2048"


   if [ "$RTUSER" = "root" ]

   then

       su - $CBUSER -c "$CMD_STR" >> $CBB_LOG_FILE

       RSTATUS=$?  

   else

       /bin/bash -c "$CMD_STR" >> $CBB_LOG_FILE

       RSTATUS=$?

   fi


   #-- release lock file

   rm $BACKUP_PATH/.cbb_lock


else


   echo >> $CBB_LOG_FILE

   echo WARNING: Backup aborted. >> $CBB_LOG_FILE

   echo An existing backup process has held a lockout and may still be running, please check. >> $CBB_LOG_FILE

   echo PID: >> $CBB_LOG_FILE

   cat $BACKUP_PATH/.cbb_lock >> $CBB_LOG_FILE

   echo >> $CBB_LOG_FILE


fi


if [ "$RSTATUS" = "0" ]

then

    LOGMSG="Ended successfully"

else

    LOGMSG="Ended in error"   

fi


echo >> $CBB_LOG_FILE

echo Runtime Script: $0 >> $CBB_LOG_FILE

echo ==== $LOGMSG on `date` ==== >> $CBB_LOG_FILE

echo >> $CBB_LOG_FILE


#if [ $RSTATUS -ne 0 ]

#then

#cat $CBB_LOG_FILE | /bin/mailx -s "$HNAME: Couchbase database backup failed" dba_oncall@domain.com

#fi;


exit $RSTATUS


TABLE OF CONTENTS


Couchbase Backup

Test Backup Connection

Full Backup Of Cluster

Sample Couchbase Backup Script


Couchbase Restore



 1 2 >Next