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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容