原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://navyaijm.blog.51cto.com/4647068/1686280
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
#!/bin/sh
DATE=`
date
+%Y%m%d`
DEL_DATE=$(
date
-d
'-30 days'
"+%Y%m%d"
)
HOST=10.10.17.27
PORT=27000
USER=admin
PASSWORD=123456
DATA_DIR=
"/data/mongodb/navy_db/db"
BACKUP_PATH=
"/data/mongodbbackup/$DATE"
date
+%Y%m%d%H%M >>
/data/log/mongodb_bak
.log
#第一步锁表
lock()
{
echo
"db.fsyncLock()"
| mongo --host $HOST --port $PORT -u $USER -p $PASSWORD admin
}
execute()
{
lock
if
[ $? -
eq
0 ]
then
echo
"mongodb lock successfully!"
>>
/data/log/mongodb_bak
.log
else
echo
"mongodb lock fail!"
>>
/data/log/mongodb_bak
.log
fi
}
execute
#第二步备份
back()
{
rsync
-av $DATA_DIR $BACKUP_PATH/
}
execute()
{
back
if
[ $? -
eq
0 ]
then
echo
"mongodb back successfully!"
>>
/data/log/mongodb_bak
.log
else
echo
"mongodb back fail!"
>>
/data/log/mongodb_bak
.log
fi
}
execute
#第三部解锁
unlock()
{
echo
"db.fsyncUnlock()"
| mongo --host $HOST --port $PORT -u $USER -p $PASSWORD admin
}
execute()
{
unlock
if
[ $? -
eq
0 ]
then
echo
"mongodb unlock successfully!"
>>
/data/log/mongodb_bak
.log
else
echo
"mongodb unlock fail!"
>>
/data/log/mongodb_bak
.log
fi
}
execute
#删除历史数据
rm
-rf
"/data/mongodbbackup/${DEL_DATE}/"
|
1
2
3
4
5
|
1、停止需要恢复的实例
2、备份清空
/data/mongodb/navy_db/db
目录
3、拷贝
/data/mongodbbackup/
$DATE下面的所有文件和文件夹到
/data/mongodb/navy_db/db
目录
4、
chown
mongod:mongod
/data/mongodb/navy_db/db/
-R
5、启动实例
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#!/bin/sh
DATE=`
date
+%Y%m%d`
DEL_DATE=$(
date
-d
'-30 days'
"+%Y%m%d"
)
HOST=10.10.17.27:27000
USER=admin
PASSWORD=123456
BACKUP_PATH=
"/backup/mongodbbackup/$DATE"
date
+%Y%m%d%H%M >>
/data/log/mongodb_bak
.log
start()
{
mongodump -h $HOST -u $USER -p $PASSWORD -o $BACKUP_PATH
}
execute()
{
start
if
[ $? -
eq
0 ]
then
echo
"mongodb back successfully!"
>>
/data/log/mongodb_bak
.log
else
echo
"mongodb back fail!"
>>
/data/log/mongodb_bak
.log
fi
}
execute
rm
-rf
"/backup/mongodbbackup/${DEL_DATE}/"
|
1
|
mongorestore --host 10.10.17.27 --port 27000
/backup/mongodbbackup/20150818/
|
1
|
mongorestore --host 10.10.17.27 --port 27001 --db navy_db_new --collection navy_db_table_new
/backup/mongodbbackup/20150818/navy_db/navy_db_table
.bson
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#!/bin/sh
DATE=`
date
+%Y%m%d`
DEL_DATE=$(
date
-d
'-30 days'
"+%Y%m%d"
)
HOST=
"dbset/10.10.17.27:27000,10.10.17.26:27000"
USER=admin
PASSWORD=123456
BACKUP_PATH=
"/data/mongodbbackup/$DATE"
date
+%Y%m%d%H%M >>
/data/log/mongodb_bak
.log
start()
{
mongodump -h $HOST -u $USER -p $PASSWORD --oplog -o $BACKUP_PATH
}
execute()
{
start
if
[ $? -
eq
0 ]
then
echo
"mongodb back successfully!"
>>
/data/log/mongodb_bak
.log
else
echo
"mongodb back fail!"
>>
/data/log/mongodb_bak
.log
fi
}
execute
rm
-rf
"/backup/mongodbbackup/${DEL_DATE}/"
|
1
|
rs.remove(
"10.10.17.26:27000"
)
|
1
|
mongorestore --host 10.10.17.26 --port 27000 --oplogReplay
/data/mongodbbackup/20150820/
|
1
2
|
use
local
db.createCollection(
"oplog.rs"
, {
"capped"
:
true
,
"size"
: 10000000})
|
1
|
mongorestore --host 10.10.17.26 --port 27000 -d
local
-c oplog.rs
/data/mongodbbackup/20150820/oplog
.bson
|
1
|
rs.add(
"10.10.17.26:27000"
)
|