shell定时备份mongo

在服务器上用shell语言+crontab完成对mongo库的定时备份和清除

1. /www/tools/目录下的mongo.sh(备份功能)

#!/bin/bash

targetpath="/data/backup/mongo/"

nowtime=$(date +%y%m%d)

start()

{

mongodump -h (mongo库的地址) -o ${targetpath}/${nowtime}

}

execute()

{

start

if [ $? -eq 0 ]

then

echo "back successfully!"

else

echo "back failure!"

fi

}

if [ ! -d "${targetpath}/${nowtime}/" ]

then

mkdir ${targetpath}/${nowtime}

fi

execute

echo "============== back end ${nowtime} =============="


2.  /www/tools/目录下的delmongo.sh(清除功能)

#!/bin/bash

targetpath='/data/backup/mongo/'

nowtime=$(date -d '-3 days' "+%y%m%d")

if [ -d "${targetpath}/${nowtime}/" ]

then

rm -rf "${targetpath}/${nowtime}/"

echo "=======${targetpath}/${nowtime}/===删除完毕=="

fi

echo "===$nowtime ==="


3. 添加crontab

20 1 * * * cd /www/tools;/bin/bash /www/tools/mongo.sh

20 1 * * * cd /www/tools;/bin/bash /www/tools/delmongo.sh

你可能感兴趣的:(shell定时备份mongo)