In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
The purpose of this article is to share with you the content of an example of MongoDB implementing remote automatic backup in Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
If the backup is done, to be on the safe side, the backup and the library will not be on the same server, so I applied for a server from the operation and maintenance staff and installed mongo at the same time.
After the installation, first test whether the target mongodb can be accessed remotely to the bin directory where mongo is installed
. / mongo 10.100.1.101virtual 27017 # ip and port of the target mongo
Then create some necessary directories, such as the directory where the backup files are placed.
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 name of the replica set # 10.100.1.101testdb 27017, 10.100.1.102 testdb 27017 is the replica set node. There can be more than one #-d testdb is the library name to be backed up. Do not enter all #-out save paths under the default replica set.
At this point, the backup of mongo has been implemented, and now what needs to be done is automatic.
Write a script
Automatic scheduled backup is actually achieved through the crontab command. But only if 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, there are corresponding comments on the script for your reference, here mainly do three actions, the first is to back up, and then compress the backed up files, and then only retain the files of the last 7 days.
#! / bin/bashsourcepath='/home/local/mongodb/bin' # mongodb file path nowtime=$ (date +% Y-%m-%d-%H) replicationname='test' # replica set name dbname='testdb' # library name port='27017' # port ip1='10.100.1.101' # ipip2='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} fiexecuteecho "= 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 executable permission to the file, and you can execute the test manually:
Chmod + x / home/local/mongod_bak.sh
Scheduled task
Finally, add the execution plan and modify / etc/crontab
Crontab-e
Add the execution script and save it.
301 * / home/local/mongod_bak.sh # means that backups are performed at 1: 30 a.m. Every day
Here is a brief introduction to crontab.
Crontab commands are common in Unix and Unix-like operating systems and are used to set instructions that are executed periodically. The command reads instructions from a standard input device and stores them in a crontab file for later reading and execution.
Typically, instructions stored in crontab are activated by daemons, and crond is often run in the background, checking every minute to see if there are scheduled jobs to be executed. This type of assignment is commonly referred to as cron jobs.
You can refer to some common commands as follows:
# Startup service / sbin/service crond start # shutdown 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 crontab service service crond start # check whether crontab service is set to boot Execute command: ntsysv# join boot auto start: chkconfig-- level 35 crond on# list crontab file crontab-l# edit crontab file crontab-e# delete crontab file $crontab-r # recover lost crontab file # suppose you have a backup in your $HOME directory, you can copy it to / var/spool/cron/, where is the user name # or use the following command It's the file name crontab that you copied in the $HOME directory. Thank you for reading! This is the end of this article on "an example of MongoDB remote automatic backup in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.