ubuntu自動定時備份MongoDB資料庫
阿新 • • 發佈:2018-12-10
指令碼檔案MongoDB_bak.sh:
#!/bin/bash
#backup MongoDB
#mongodump命令路徑
DUMP=/usr/bin/mongodump
#臨時備份目錄
OUT_DIR=/data/mongodb/mongodb_now
#備份存放路徑
TAR_DIR=/data/mongodb/mongodb_list
#獲取當前系統時間
DATE=`date +%Y_%m_%d`
#DAYS=15代表刪除15天前的備份,即只保留近15天的備份
DAYS=15
#最終儲存的資料庫備份檔案
TAR_BAK="mongodb_bak_$DATE.tar.gz"
cd $OUT_DIR
rm -rf $OUT_DIR/*
mkdir -p $OUT_DIR/$DATE
#備份全部資料庫
$DUMP -h 127.0.0.1:27017 -o $OUT_DIR/$DATE
#壓縮為.tar.gz格式
tar -zcvf $TAR_DIR/$TAR_BAK $OUT_DIR/$DATE
#刪除15天前的備份檔案
find $TAR_DIR/ -mtime +$DAYS -delete
exit
儲存並修改成可以執行的許可權
chmod +x MongoDB_bak.sh
新增到計劃任務
vi /etc/crontab
每星期六晚上22:30開始執行MongoDB資料庫備份指令碼
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL =/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
30 22 * * 6 root /data/mongodb/MongoDB_bak.sh
#