代码改变世界

【MongoDB】MongoDB备份脚本

  abce  阅读(262)  评论(0编辑  收藏  举报
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
62
63
64
65
66
67
68
#!/bin/bash
################################################################################
#0 * * * * /root/dhmon/mongo-backup.sh >> /root/dhmon/full_mongo_backup.log 2>&1
################################################################################
export PATH=/bin:/usr/bin:/usr/local/bin
TODAY=`date +"%Y%m%d%H%M%S"`
  
######################################################################
DB_BACKUP_PATH='/databak/mdb'
MONGO_HOST='8.8.8.8'
MONGO_PORT='27017'
 
# If mongodb is protected with username password.
# Set AUTH_ENABLED to 1
# and add MONGO_USER and MONGO_PASSWD values correctly
AUTH_ENABLED=1
MONGO_USER='abcd'
MONGO_PASSWD='abcd'
 
 
# Set DATABASE_NAMES to "ALL" to backup all databases.
# or specify databases names seprated with space to backup
# specific databases only.
 
DATABASE_NAMES='ALL'
#DATABASE_NAMES='mydb db2 newdb'
 
## Number of days to keep local backup copy
BACKUP_RETAIN_DAYS=15
  
######################################################################
######################################################################
# Create backup directory
if ! mkdir -p ${DB_BACKUP_PATH}/${TODAY}; then
  echo "Can't create backup directory in ${DB_BACKUP_PATH}. Go and fix it!" 1>&2
  exit 1;
fi;
 
AUTH_PARAM=""
 
if [ ${AUTH_ENABLED} -eq 1 ]; then
    AUTH_PARAM=" --username ${MONGO_USER} --password ${MONGO_PASSWD} "
fi
 
if [ ${DATABASE_NAMES} = "ALL" ]; then
    echo "Running backup for all databases at $(date +"%Y-%m-%d:%H-%M-%S"). "
    echo "--------------------------------------------"
    mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} ${AUTH_PARAM} --gzip --oplog --out ${DB_BACKUP_PATH}/${TODAY}/
     
else
    echo "Running backup for selected databases"
    for DB_NAME in ${DATABASE_NAMES}
    do
        mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} --db ${DB_NAME} ${AUTH_PARAM}  --gzip --out ${DB_BACKUP_PATH}/${TODAY}/
    done
fi
 
  
######## Remove backups older than {BACKUP_RETAIN_DAYS} days  ########
if [ ! -z ${DB_BACKUP_PATH} ]; then
      cd ${DB_BACKUP_PATH}
      find $DB_BACKUP_PATH -type f -mtime +$BACKUP_RETAIN_DAYS -exec rm {} +
      echo "--------------------------------------------"
      echo "Database backup complete at $(date +"%Y-%m-%d:%H-%M-%S")!"
fi
  
 
######################### End of script ##############################

  

https://pranavprakash.net/2017/02/04/automate-mongodb-backups-using-cron-and-shell-script/

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2017-08-01 二进制安装mysql 5.6
2016-08-01 Oracle 11g RAC 第二节点root.sh执行失败后再次执行root.sh
2016-08-01 RAC GI安装,报"Task resolv.conf Integerity"验证失败
点击右上角即可分享
微信分享提示