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