mongodb数据备份脚本和日志切割脚本
wjw555关注0人评论1353人阅读2018-02-02 17:33:50
mongodb数据备份脚本和日志切割脚本
1.登陆mongo mongo --port=27018
2.创建管理用户:
代码语言:javascript复制db.createUser(
... {
... user: "root",
... pwd: "Zytest2233",
... roles: [ { role: "root", db: "admin" } ]
... }
... )
3.开启认证:
代码语言:javascript复制[root@cacti scripts]# cat /usr/local/mongodb/mongod.cnf
logpath=/data/mongodb/logs/mongodb.log
logappend = true
#fork and run in background
fork = true
port = 27018
dbpath=/data/mongodb/data
#location of pidfile
pidfilepath=/data/mongodb/mongod.pid
auth = true
#keyFile = /tmp/mongo-keyfile
#nohttpinterface=true
#replSet=shard1
4.mongodb日志按天切割 mongodb日志默认写在一个文件,时间久了文件太大,用cron计划任务 脚本 可以实现按日期存放log
[root@cacti scripts]# cat /data/scripts/rotatelog.sh
代码语言:javascript复制#!/bin/bash
#mongodb client use admin
cd /usr/local/mongodb/bin
MongoDB='./mongo 127.0.0.1:27018'
$MongoDB <<EOF
use admin
db.auth("root","Zytest2233")
db.runCommand( { logRotate : 1 } )
exit;
EOF
再用 crontab -e 打开加入如下内容,并保存:
59 23 * * * /bin/sh /data/scripts/rotatelog.sh
[root@cacti scripts]# ll /data/mongodb/logs/ total 76
代码语言:javascript复制-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数据库备份: 指定多个数据库来进行备份脚本: 脚本1: [root@localhost ~]# cat /data/backup/mongobackup/test.sh
代码语言:javascript复制mongo 127.0.0.1:27018/admin -u root -p Zytest2233 << EOF
show dbs
exit;
EOF
脚本2: [root@localhost ~]# cat mongoback.sh
代码语言:javascript复制#/bin/bash
source /etc/profile
user="root"
pass="Zytest2233"
port="28018"
Cmd="/usr/local/mongodb/bin/mongodump"
Bakdir=/data/backup/mongobackup
dbs="$(/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 $dbs
do
$Dump $n -o $Bakdir
done