Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB database backup script

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

#! / 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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report