banner
小鱼

小鱼's Blog

MongoDB Backup


image

# Install MongoDump#

The latest version of MongoDB no longer includes the mongodump backup tool.

You need to manually download it.

First, go to the official website and select the corresponding version of the MongoDB Command Line Database Tools.

Download MongoDB Command Line Database Tools | MongoDB


image

Click copy link to copy the download link.

Then, install the compressed package on the server.

wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2004-x86_64-100.7.4.tgz

And extract it.

tar -zxvf mongodb-database-tools-ubuntu2004-x86_64-100.7.4.tgz

Rename it to mongodb-database-tools.

mv mongodb-database-tools-ubuntu2004-x86_64-100.7.4 mongodb-database-tools

Then go to the mongodb-database-tools/bin directory.

Move the mongodump file to the bin directory where MongoDB is located.

mv mongodump /usr/local/mongodb/bin/

# Backup#

Now you can use mongodump for backup operations.

For example,

mongodump --uri="mongodb://root:root@localhost:27017/XXX"

This will generate a dump directory in the current directory, which contains the backup file of the XXX collection.

# Scheduled Backup#

## Edit the execution script#

Edit the backup command into an sh file, for example, command.sh

#!/bin/bash

# Get the current time
current_time=$(date +"%Y%m%d%H%M%S")

mongodump --uri="mongodb://XXX:[email protected]:27017/XXX"
mongodump --uri="mongodb://XXX:[email protected]:27017/XXX"
mongodump --uri="mongodb://XXX:[email protected]:27017/XXX"

# Rename the file
mv dump dump_$current_time

And grant executable permissions

chmod +x ./command.sh

## Scheduled Backup#

Use crontab for scheduled execution

crontab -e

Add the following to the file

# Execute the mongodb backup script every day at 3 AM
0 3 * * * /home/mongodbDump/command.sh

To view all scheduled tasks for the current user, use

crontab -l
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.