How to implement remote automatic backup of MongoDB in Linux

How to implement remote automatic backup of MongoDB in Linux

Preface

After reading the previous article about the pain of taking over an old project - learning and building a MongoDB cluster, I learned that I recently took over a project run by a stepmother. No one was maintaining the database of the project, and the DBA refused to take over for the time being for various reasons. Faced with the naked database without backup, I was very anxious, so I spent some time to set up automatic backup of the production environment.

Let’s take a look at the detailed introduction.

Some preparation

Since everything has been backed up, for the sake of safety, the backup and the library are not placed on the same server, so I applied for a server from the operation and maintenance, and installed mongo at the same time. If you don’t know how to install mongo, you can read my previous article.

After installation, first test whether you can remotely access the target mongodb, go to the bin directory where mongo is installed

./mongo 10.100.1.101:27017 #target mongo's IP and port

Then create some necessary directories, such as which directory to put the backup files in.

Next, test using mongodump to back up the database:

./bin/mongodump --host test/10.100.1.101:27017,10.100.1.102:27017 -d testdb --out /data/temp

# test is the replica set name# 10.100.1.101:27017,10.100.1.102:27017 are replica set nodes. You can have multiple nodes# -d testdb is the name of the database to be backed up. If not filled in, all nodes in the default replica set# --out save path

At this point, mongo backup has been implemented, and now all we need to do is make it automatic.

Writing a Script

Automatic scheduled backup is actually achieved through the crontab command. But the premise is that we need to write a script that runs regularly. First, we create a new script:

vi /home/local/mongod_bak.sh

Then write the corresponding script. There are corresponding comments on the script for your reference. There are three main actions here. The first is to back up, then compress the backup file, and then only keep the files in the last 7 days.

#!/bin/bash
sourcepath='/home/local/mongodb/bin' #mongodb file path targetpath='/home/local/mongodb_bak' #backup path nowtime=$(date +%Y-%m-%d-%H)
replicationname='test' #Replica set name dbname='testdb' #Database name port='27017' #Port ip1='10.100.1.101' #ip
ip2='10.100.1.102'

echo "=============== start backup ${nowtime} ==============="
start()
{
 ${sourcepath}/mongodump --host ${replicationname}/${ip1}:${port},${ip2}:${port} -d ${dbname} --out ${targetpath}/${nowtime}
}
execute()
{
 start
 if [ $? -eq 0 ]
 then
 echo "back successfully!"
 else
 echo "back failure!"
 fi
}
 
if [ ! -d "${targetpath}/${nowtime}/" ]
then
 mkdir ${targetpath}/${nowtime}
fi
execute
echo "=============== back end ${nowtime} ==============="

echo "=============== start zip ${nowtime} =============="
zip -r ${targetpath}/${nowtime}.zip ${targetpath}/${nowtime}
rm -rf ${targetpath}/${nowtime}
echo "=============== zip end ${nowtime} =============="

echo "=============== start delete seven days ago back ${nowtime} ==============="
find ${targetpath} -type f -mtime +7 -name "*" -exec rm -rf {} \; 
echo "=============== delete end ${nowtime} ==============="

After writing, give the file executable permissions and manually execute the test:

chmod +x /home/local/mongod_bak.sh

Scheduled tasks

The last step is to add an execution plan and modify /etc/crontab

crontab -e

Add the execution script and save it.

30 1 * * * /home/local/mongod_bak.sh #Indicates that the backup is performed at 1:30 am every day

Here is a brief introduction to crontab.

The crontab command is commonly found in Unix and Unix-like operating systems and is used to set instructions to be executed periodically. This command reads instructions from the standard input device and stores them in the crontab file for later reading and execution.

Usually, the instructions stored in crontab are activated by the daemon process. Crond often runs in the background, checking every minute whether there are scheduled jobs to be executed. These jobs are generally called cron jobs.

Some common commands can be found below:

#Start service /sbin/service crond start 

#Shut down the service /sbin/service crond stop 

#Restart service /sbin/service crond restart 

#Reload configuration /sbin/service crond reload 

#View crontab service status service crond status 

#Manually start the crontab service service crond start 

#Check whether the crontab service has been set to start at boot time and execute the command:
ntsysv

#Add automatic startup:
chkconfig --level 35 crond on

# List crontab files crontab -l

#Edit crontab file crontab -e

#Delete crontab file $ crontab -r

#Restore the lost crontab file #Assuming you have a backup in your $HOME directory, you can copy it to /var/spool/cron/<username>, where <username> is your username. #Or use the following command, where <filename> is the file name of your copy in the $HOME directory crontab <filename>

Summarize

Slow work produces fine work. Some things may seem difficult and troublesome at first, but when you calm down and study them carefully, they are still easy to understand. After all, you are not the first one to fall into the trap, so you should study hard.

Well, the above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Batch script for automatically backing up MongoDB under Windows
  • Detailed explanation of automatic backup of MongoDB database under Linux
  • MongoDB achieves full process record of automatic backup

<<:  Detailed explanation of Vue's self-implementation of dispatch and broadcast (dispatch and broadcast)

>>:  Install mysql5.7.13 using RPM in CentOS 7

Recommend

Detailed explanation of mysql transaction management operations

This article describes the MySQL transaction mana...

Detailed steps for smooth transition from MySQL to MariaDB

1. Introduction to MariaDB and MySQL 1. Introduct...

Vue+Openlayer realizes the dragging and rotation deformation effect of graphics

Table of contents Preface Related Materials Achie...

A brief discussion on the $notify points of element

My original intention was to encapsulate the $not...

MySQL Packet for query is too large problem and solution

Problem description: Error message: Caused by: co...

JavaScript to achieve the idea of ​​​​snake game

The implementation idea of ​​the javascript game ...

Sample code using the element calendar component in Vue

First look at the effect diagram: The complete co...

JavaScript implements cool mouse tailing effects

After watching this, I guarantee that you have ha...

Quickly learn MySQL basics

Table of contents Understanding SQL Understanding...

A brief analysis of the usage of USING and HAVING in MySQL

This article uses examples to illustrate the usag...

HTML page jump and parameter transfer issues

HTML page jump: window.open(url, "", &q...

Example code of vue custom component to implement v-model two-way binding data

In the project, you will encounter custom public ...

WeChat Mini Program to Implement Electronic Signature

This article shares the specific code for impleme...