MongoDB data backup script and log cut script

Source: Internet
Author: User
Tags mongodb mongodb client

MongoDB data backup script and log cut script

1. Login MONGO
MONGO--port=27018

2. Create an Administrative user:

db.createUser(...  {...  user: "root",...    pwd: "Zytest2233",...     roles: [ { role: "root", db: "admin" } ]...  }...  )

3. Turn on authentication:

[[email protected] scripts]# cat /usr/local/mongodb/mongod.cnf logpath=/data/mongodb/logs/mongodb.loglogappend = true#fork and run in backgroundfork = trueport = 27018dbpath=/data/mongodb/data#location of pidfilepidfilepath=/data/mongodb/mongod.pidauth = true#keyFile = /tmp/mongo-keyfile#nohttpinterface=true#replSet=shard1

4.mongodb Log cut by day
MongoDB log is written by default in a file, long time the file is too large, with cron scheduling task + script can be implemented by date to store log

[Email protected] scripts]# cat/data/scripts/rotatelog.sh

#!/bin/bash#mongodb client  use admincd /usr/local/mongodb/binMongoDB=‘./mongo 127.0.0.1:27018‘$MongoDB <<EOFuse admindb.auth("root","Zytest2233")db.runCommand( { logRotate : 1 } )exit;EOF

Then use CRONTAB-E open to add the following, and save:
59 23 * * * /bin/sh /data/scripts/rotatelog.sh

[Email protected] scripts]# ll/data/mongodb/logs/
Total 76

-rw-r--r--. 1 root root   982 Feb  2 15:27 mongodb.log-rw-r--r--. 1 root root 53353 Feb  2 15:14 mongodb.log.2018-02-02T07-18-39-rw-r--r--. 1 root root  6163 Feb  2 15:21 mongodb.log.2018-02-02T07-22-02-rw-r--r--. 1 root root  1849 Feb  2 15:27 mongodb.log.2018-02-02T07-27-02

5.mongodb Database backup:
Specify multiple databases for backup scripts:
Script 1:
[Email protected] ~]# cat/data/backup/mongobackup/test.sh

mongo 127.0.0.1:27018/admin -u root -p Zytest2233  << EOFshow dbsexit;EOF

Script 2:
[email protected] ~]# cat mongoback.sh

#/bin/bashsource /etc/profileuser="root"pass="Zytest2233"port="28018"Cmd="/usr/local/mongodb/bin/mongodump"Bakdir=/data/backup/mongobackupdbs="$(/bin/sh /data/backup/mongobackup/test.sh|sed ‘1,2d‘|grep -v "local"|awk ‘{ print $1 }‘|xargs -n4)"Dump="$Cmd -uroot -p$pass --port=$port -d"for n in $dbsdo$Dump $n -o $Bakdir done

MongoDB data backup script and log cut script

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.