MongoDB database backup script
#! / bin/bash
# backup MongoDB
# mongodump command path
DUMP=/usr/local/mongodb/bin/mongodump
# temporary backup directory
OUT_DIR=/data/mongodb_bak/mongodb_bak_now
# backup storage path
TAR_DIR=/data/mongodb_bak/mongodb_baklist
# get the current system time
DATE= `date +% Y%m_%d#mongo hostname HOST=ifconfig ens192 | grep netmask | awk'{print $2}'`
# backup database
DB=data01
# Database account
DB_USER=root
# Database password
DB_PASS=123456
# DAYS=15 means to delete backups from 15 days ago, that is, to keep only backups for nearly 15 days.
DAYS=15
# the final saved database backup file
TAR_BAK= "mongodbbak$DATE.tar.gz"
# determine whether the backup command file and backup directory exist
If [!-f "$DUMP"]; then
Echo "mongodump the command does not exist, check the correct path."
Exit 0
Elif [!-d "$OUT_DIR"]; then
Echo "Create tmp backup dir"
Mkdir-p $OUT_DIR
Elif [!-d "$TAR_DIR"]; then
Echo "Create Backup dir"
Mkdir-p $TAR_DIR
Els
Echo "Start Backup"
Fi
Cd $OUT_DIR
Rm-rf $OUT_DIR/*
Mkdir-p $OUT_DIR/$DATE
# back up a single database
$DUMP-h $HOST-u $DB_USER-p $DB_PASS-d $DB-o $OUT_DIR/$DATE
# backup all databases
# $DUMP-h $HOST-u $DB_USER-p $DB_PASS-- authenticationDatabase "admin"-o $OUT_DIR/$DATE
# compressed to .tar.gz format
Tar-zcvf $TAR_DIR/$TAR_BAK $OUT_DIR/$DATE
# Delete backup files from 15 days ago
Find $TAR_DIR/-mtime + $DAYS-delete