How does MongoDB implement remote automatic backup detail in Linux
- 2020-11-20 06:23:52
- OfStack
preface
Read on 1 article take pain - MongoDB learning and clustering of the old projects know that recently took over a pet project of stepmother no one database maintenance of the project, DBA with all sorts of reason to push to take off temporarily don't pick up, in the face of streaking no backup of the database, my heart is very anxious, so spend a little time to get the production environment of the automatic backup to make up.
Without further ado, let's take a look at the details
1 some preparation
Since they are all backed up, to be on the safe side, the backup and library will not be placed on the same server, so I applied for a server from operation and maintenance and installed mongo at the same time. If you do not know how to install mongo, you can see my last article.
After installation, first test whether the target mongodb can be accessed remotely to the bin directory where mongo was installed
./mongo 10.100.1.101:27017 # The target mongo the ip And the port
Then create the necessary directories, such as where to put the backup files.
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 Is a replica set node. There can be more than one
# -d testdb Is the name of the library to be backed up, do not fill in the default copy set under all
# --out Save the path
At this point, the backup of mongo has been implemented, and now all you need to do is automate it.
Write a script
Automatic timing backup is achieved through the crontab command. But the premise is that we need to write a script that runs regularly. First, let's create a new script:
vi /home/local/mongod_bak.sh
Then write the corresponding script, script on the corresponding comments, for your reference, here mainly do 3 actions, the first is to backup, and then the backup of the file compression, and then only keep the last 7 days of the file.
#!/bin/bash
sourcepath='/home/local/mongodb/bin' #mongodb The file path
targetpath='/home/local/mongodb_bak' # Backup path
nowtime=$(date +%Y-%m-%d-%H)
replicationname='test' # A copy of the set of
dbname='testdb' # The library
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 permission, and can execute the test manually:
chmod +x /home/local/mongod_bak.sh
Timing task
Finally, add the execution plan, modify /etc/crontab
crontab -e
Add the execution script and save it.
30 1 * * * /home/local/mongod_bak.sh # Every morning 1 point 30 Perform a backup
Here is a brief introduction to crontab.
The crontab command is commonly found in the operating systems of Unix and class Unix and is used to set instructions that are executed periodically. This command reads instructions from a standard input device and stores them in an crontab file for later reading and execution.
Typically, crontab stores instructions that are activated by the daemon, and crond often runs in the background, checking every minute to see if any scheduled jobs need to be executed. This type of job 1 is commonly known as cron jobs.
1 Some common commands can refer to the following:
# Start the service
/sbin/service crond start
# Close the service
/sbin/service crond stop
# Restart the service
/sbin/service crond restart
# Reload configuration
/sbin/service crond reload
# To view crontab Service status
service crond status
# Manual start crontab service
service crond start
# To view crontab Whether the service has been set to boot, execute the command:
ntsysv
# Add boot to start automatically :
chkconfig --level 35 crond on
# list crontab file
crontab -l
# The editor crontab file
crontab -e
# delete crontab file
$ crontab -r
# Recover lost crontab file
# Let's say you're on your own $HOME And in the directory 1 A backup, so you can copy it to /var/spool/cron/<username> , including <username > Is the user name
# Or use the following command, where, <filename> Are you in $HOME The file name of the copy in the directory
crontab <filename>
conclusion
Slow work makes careful work. Some things are difficult and troublesome at first, but when you calm down and study carefully, it is easy to understand. After all, you are not the first one to step on the potholes, so study hard.