(goto) MongoDB Common command scripting-automated operations

Source: Internet
Author: User
Tags mongodb client mongodb commands mongodump mongorestore

MongoDB Common Command Scripting-automated OPS writes the MONGODB commands commonly used in operations into shell scripts, greatly facilitates maintenance 1 set replica set #!/bin/bash
#mongodb Enter the client and use admin
Cd/usr/mongodb/bin
Mongodb= './mongo 192.168.1.7:37017 '
$MongoDB <<eof
Use admin
rsconf={
"_id": "Job001",
"Members": [
{
"_id": 0,
"Host": "192.168.1.7:37017"
}
]
}
Rs.initiate (rsconf)
Rs.add ("192.168.1.8:37017")
Rs.add ("192.168.1.9:37017")
Exit
Eof

Note: Write the above content to the ***.sh file chmod 777 ***.sh Assign permissions

These commands can be completed in Linux > sh ***.sh

Same below
2 Modifying the settings of a replica set

#!/bin/bash

#mongodb Enter the client and enter primary
Cd/usr/mongodb/bin
Mongodb= './mongo 192.168.1.7:37017 '
$MongoDB <<eof
Use Mongomodeljobresume
#rs. Status ()
Cfg=rs.conf ()
cfg.members[0].priority=99
Cfg.members[1].priority=50
Cfg.members[2].priority=30
Rs.reconfig (CFG)
Rs.conf ()
Exit

Eof

3 Turn a table to a temporary table and copy it back so that the full log is regenerated in the Oplog

(If the size of the oplog is large enough)

#!/bin/bash
#mongodb Enter the client and enter primary
Cd/usr/mongodb/bin
Mongodb= './mongo 192.168.1.7:37017 '
$MongoDB <<eof
Use Mongomodeljobresume
Rs.remove ("192.168.1.8:37017")
Rs.remove ("192.168.1.9:37017")
Db.tbJobResume.renameCollection ("Tbjobresumeold")
Db.tbJobResumeOld.copyTo ("Tbjobresume")
Rs.add ("192.168.1.8:37017")
Rs.add ("192.168.1.9:37017")
#db. Tbjobresumeold.drop ()
Exit
Eof

4 MongoDB Log by date MongoDB log default write in a file, time is long to view and so inconvenient, with cron scheduling task + script can be implemented by date log# rotatelog.sh generate this file with the following content chmod 777 rotelog.sh granting Permission #!/bin/bash
#mongodb Client Use Admin
Cd/usr/mongodb/bin
Mongodb= './mongo 192.168.1.7:37017 '
$MongoDB <<eof
Use admin
Db.runcommand ({logrotate:1})
Exit
EOF then opens with Crontab-e (no new) to add the following and save (the file path in/var/spool/cron/root/) * * */bin/sh/usr/mongodb/config/rotatelog.sh In this case, every night 23:59, the day's diary will be a * * * date. Log to save the original log file will be emptied 5 MongoDB database automatic backup Restore script

-----automatically back up MongoDB data and compress---


#!/bin/bash
Filename= ' Date +%y%m%d%h '
Backmongodbfile=mongodb$filename.tar.gz
cd/home/mongo/back/
/usr/mongodb/bin/mongodump-h 192.168.1.7-port 37017-d Mongodbagent-o mongodb_dump/
/usr/mongodb/bin/mongodump-h 192.168.1.7-port 37017-d Mongodbbg-o mongodb_dump/
/usr/mongodb/bin/mongodump-h 192.168.1.7-port 37017-d Mongomodelactor-o mongodb_dump/
Tar czf $backmongodbFile mongodb_dump/
RM MONGODB_DUMP-RF


-----automatically unzip and restore MONGODB data---


#!/bin/bash
Filename= ' 20150330013 '
Backmongodbfile=mongodb$filename.tar.gz
cd/home/mongo/back/
Tar zxvf $backmongodbFile
/usr/mongodb/bin/mongorestore-h 192.168.1.6-port 37017--drop-d mongodbagent mongodb_dump/mongodbagent
/usr/mongodb/bin/mongorestore-h 192.168.1.6-port 37017--drop-d MONGODBBG MONGODB_DUMP/MONGODBBG
/usr/mongodb/bin/mongorestore-h 192.168.1.6-port 37017--drop-d mongomodelactor mongodb_dump/MongoModelActor
RM MONGODB_DUMP-RF

(goto) MongoDB Common command scripting-automated operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.