Docker mongoDB container backup and recovery

Docker mongoDB container backup and recovery

word:

phonetic symbolstranslate
dump[dʌmp]: [dang' mu' pu]dump; dump; dump; dump
deny | denied[dɪˈnaɪ] :[di' nai]deny; deny; deny
directory[dəˈrektəri] :[di' ruai 'ke ' te 'rui]Table of contents

1. Data backup:

1. 1 Basic grammar:

>mongodump -h dbhost -d dbname -o dbdirectory

Example:

1. 2 Examples:

docker exec -it mymongo mongodump -h localhost -u root -p 123456 -o /tmp/test
parametereffectRemark
mongodumpbackup command
**-h **The address of the server where MongoDB is located, for example: 127.0.0.1, of course you can also specify the port number: 127.0.0.1:27017
-dThe database instance that needs to be backed up, for example: test
-oBackup data storage location, for example: c:\data\dump, of course, this directory needs to be created in advance, after the backup is completed, the system will automatically create a test directory under the dump directory, which stores the backup data of the database instance

1. 3 Attention!!

The content of the backup is in the container;

**Do not try to find the contents of the backup in the host directory, **

Need docker exec -it [container] bash to enter the container to find

2. Restore data:

2.1 Basic syntax:

>mongorestore -h <hostname><:port> -d dbname <path>
parametereffectRemark
**mongorestore **restore command
-hThe address of the server where MongoDB is located, for example: 127.0.0.1, of course you can also specify the port number: 127.0.0.1:27017
-dThe database instance that needs to be restored, for example: test, of course, the name can also be different from the backup time, such as test2
The last parameter of mongorestore sets the location of the backup data, for example: c:\data\dump\testcannot be specified at the same time and the --dir option, --dir can also set the backup directory
–dirSpecify the backup directorycannot be specified at the same time and the --dir option.

2.2 Examples:

docker exec -it mymongo mongorestore -h localhost -u root -p 123456 --dir /tmp/test

cannot be specified at the same time and the --dir option

docker exec -it mymongo mongorestore -h localhost -u root -p 123456 /tmp/test

3. Example:

3.1 Host and container structure directory

3.1.1 Host directory

├── /
|   ├── mongoDB
|       ├── sh
|            ├── dump.sh
|            ├── dump [folder]
|            	   ├── admin_202211041423.tar.gz
|            	   ├── config_202211041435.tar.gz
|            	   ├── water_202211041423.tar.gz
|            	   ├── shop_202211041423.tar.gz

3.1.2 Directory in the mongoDB container:

├── /
|   ├── dump
|       ├── list
|  			├── admin_202211041423.tar.gz
|            ├── config_202211041435.tar.gz
|            ├── water_202211041423.tar.gz
|       ├── tmp
|  			├── admin_202211041423
|            ├── config_202211041435
|            ├── water_202211041423

3. 2 steps:

3. 2.1 Create directory

Build the directory tree in the host and container in 3.1 in advance; otherwise, the script execution output: No such file or directory

dump.sh script description:

dump.sh code snippeteffectRemark
date +%Y%m%d%H%M202211011749 [What time is the year, month and day]
for DBNAME in ${DB_NAME_ARR[@]}All databases in the ${DB_NAME_ARR[@]} array${arr1[@]} or ${arr1[*]} means all elements of the array
# dump.sh execution file
MONGODB_CONTAINER_NAME=yours mongoDB container name
DUMP="docker exec -it ${MONGODB_CONTAINER_NAME} "
# Temporary backup path (note: the path inside the docker container)
OUT_DIR=/dump/tmp
# Compressed backup storage path (note: the path inside the docker container)
TAR_DIR=/dump/list
# Current system time Equivalent: DATE=$(date +%Y%m%d%H%M)
DATE=`date +%Y%m%d%H%M`
# database account
DB_USER=user
# database password
DB_PASS=password
# Database name, multiple separated by spaces
DB_NAME_ARR=("admin config") 
# Represents a hold within 24 hours
DAYS=1


# The final saved database backup file
for DBNAME in ${DB_NAME_ARR[@]}
do
    # Delete the contents of the temporary directory
    rm -rf $OUT_DIR/*
    
    # File name: database name_time
    FILE_NAME="${DBNAME}_${DATE}"
    # Temporary directory for exported data
    TARGET_DIR="$OUT_DIR/$FILE_NAME"
    mkdir -p $TARGET_DIR
    # The temporary directory packs the compressed files
    TARGZ_FILE="${FILE_NAME}.tar.gz"
    # Directory to store compressed files
    TARGET_FILE="$TAR_DIR/$TARGZ_FILE"
    
    # Execute the export command
    # $DUMP -h 127.0.0.1:27017 -u $DB_USER -p $DB_PASS -d $DBNAME -o $OUT_DIR/$DATE
    $DUMP mongodump -h 43.143.147.51:27017 -d $DBNAME -o $TARGET_DIR
    
    # Judgment export results
    flag=`echo $?`
    if [ $flag == "0" ];then
            echo "database $DBNAME success backup to $TARGET_DIR"
    else
            echo "database $DBNAME backup fail!"
    fi
    
    # The compressed format is .tar.gz format
    # -P tar uses relative paths to compress and package by default, and requires (-P) to allow the use of absolute paths 
    # -v output verbose log 
    $DUMP tar -zcPf $TARGET_FILE $TARGET_DIR/$DBNAME
    
    # Copy the compressed file in the container to the dump directory of the same level as the backup script dump.sh on the host
    docker cp ${MONGODB_CONTAINER_NAME}:$TARGET_FILE $PWD/dump

done

# Delete the backup file from $DAYS days ago; [ Delete the expired file in the dump folder in the same directory as the dump.sh script copied from the container to the host ]
find $PWD/dump -mtime $DAYS -delete

Tags: Database Docker MongoDB

Posted by wmbetts on Tue, 06 Dec 2022 01:57:38 +0300