1.编写备份脚本
#!/bin/bash
echo "-----------数据库备份开始-------------"
DUMP=/home/dbbackup/mongodb4.4/bin/mongodump
OUT_DIR=/home/dbbackup/mongodb_bak_now
TAR_DIR=/home/dbbackup/mongodb_bak_list
DATE=`date +%Y_%m_%d`
DB_USER=root
DB_PASS=123456
DAYS=15
TAR_BAK="my_mongodb_bak_$DATE.tar.gz"
cd $OUT_DIR
rm -rf $OUT_DIR/*
BAK_DIR=$DATE'_xddb_bak'
echo '------'$BAK_DIR
mkdir -p $OUT_DIR/$BAK_DIR
$DUMP -h 192.168.0.104:27001 -d testdb -u $DB_USER -p $DB_PASS -o $OUT_DIR/$BAK_DIR
tar -zcvf $TAR_DIR/$TAR_BAK $BAK_DIR
echo "-----------数据库压缩完成-------------"
find $TAR_DIR/ -mtime +$DAYS -delete
echo "-----------数据库备份完成-------------"
scp -r $TAR_DIR/$TAR_BAK root@192.168.0.105:$TAR_DIR
exit
2.将脚本加入调度
[root@localhost shell]
0 12,4,23 * * * /home/db_backup.sh