Restore the mongodb timed backup using the crontab command in the centos system
- 2020-11-18 06:32:55
- OfStack
Perform backup operations through the centos script, and use the crontab command to achieve timed backup and recovery, and delete the backup before a specified number of days
Specific operation:
1. Create Mongodb database backup directory
mkdir -p /home/backup/mongod_bak/mongod_bak_now
mkdir -p /home/backup/mongod_bak/mongod_bak_list
2. Create new Mongodb database backup script
vi /home/crontab/ mongod_bak.sh # create a new file and enter the following code
#!/bin/sh
DUMP=/usr/local/mongodb/bin/mongodump #mongodump Backup file execution path
OUT_DIR=/home/backup/mongod_bak/mongod_bak_now # Temporary backup directory
TAR_DIR=/home/backup/mongod_bak/mongod_bak_list # Backup storage path
DATE=`date +%Y_%m_%d` # Gets the current system time
DB_USER=username # Database account
DB_PASS=123456 # Database password
DAYS=7 #DAYS=7 On behalf of the delete 7 Days before the backup, that is, only keep the most recent 7 Days of backup
TAR_BAK="mongod_bak_$DATE.tar.gz" # The final database backup file name saved
cd $OUT_DIR
rm -rf $OUT_DIR/*
mkdir -p $OUT_DIR/$DATE
$DUMP -u $DB_USER -p $DB_PASS -o $OUT_DIR/$DATE # Back up all databases
tar -zcvf $TAR_DIR/$TAR_BAK $OUT_DIR/$DATE # Compressed into .tar.gz format
find $TAR_DIR/ -mtime +$DAYS -delete # delete 7 Days before the backup file
3. Modify file properties to make them executable
chmod +x /home/crontab/mongod_bak.sh
4. Modify /etc/crontab # to add scheduled tasks
crontab -e
Add below
30 1 * * * root /home/crontab/mongod_bak.sh # Every morning 1 point 30 Perform a backup
5. Restart crond for the Settings to take effect
/sbin/service crond reload # Reload configuration
chkconfig --level 35 crond on # Add boot to start automatically :
/sbin/service crond start # Start the service
crontab -l # list crontab file
mongod_bak_2017_02_28.tar.gz can be found daily in the /home/backup/mongod_bak/mongod_bak_list directory.
At this point, Linux automatically backs up the Mongodb database and deletes the backup before the specified number of days.
Mongodb periodically restores the backup
Restore all databases:
mongorestore � drop � directoryperdb
/home/backup/mongod_bak/mongod_bak_now/2017_02_28/
Restore a single database:
mongorestore � drop -d dataname � directoryperdb
/home/backup/mongod_bak/mongod_bak_now/2017_02_28/dataname
The parameters? drop: Delete the original database data before restoring it to avoid data duplication.
The directoryperdb parameter: database backup directory
-ES70en parameter: followed by the name of the database to be restored
crontab command:
The crontab command is commonly used in the operating systems of Unix and class Unix to set instructions that are executed periodically. This command reads instructions from a standard input device and stores them in an "crontab" file for later reading and execution. The word comes from the Greek chronos(? Argument &western & # 63;) "Time" means time.
Typically, crontab stores instructions that are activated by the daemon, and crond often runs in the background, checking every minute to see if any scheduled jobs need to be executed. This type of job is commonly referred to as cron jobs.
Install crontab:
[root@CentOS ~]# yum install vixie-cron
[root@CentOS ~]# yum install crontabs
Description:
vixie-cron software package is the main program of cron;
The crontabs package is a program used to install, uninstall, or enumerate the tables used to drive the cron daemon.
cron is the built-in service of linux, but it does not get up automatically. You can start and close the service by:
/sbin/service crond start # Start the service
/sbin/service crond stop # Close the service
/sbin/service crond restart # Restart the service
/sbin/service crond reload # Reload configuration
View crontab service status:
#!/bin/sh
DUMP=/usr/local/mongodb/bin/mongodump #mongodump Backup file execution path
OUT_DIR=/home/backup/mongod_bak/mongod_bak_now # Temporary backup directory
TAR_DIR=/home/backup/mongod_bak/mongod_bak_list # Backup storage path
DATE=`date +%Y_%m_%d` # Gets the current system time
DB_USER=username # Database account
DB_PASS=123456 # Database password
DAYS=7 #DAYS=7 On behalf of the delete 7 Days before the backup, that is, only keep the most recent 7 Days of backup
TAR_BAK="mongod_bak_$DATE.tar.gz" # The final database backup file name saved
cd $OUT_DIR
rm -rf $OUT_DIR/*
mkdir -p $OUT_DIR/$DATE
$DUMP -u $DB_USER -p $DB_PASS -o $OUT_DIR/$DATE # Back up all databases
tar -zcvf $TAR_DIR/$TAR_BAK $OUT_DIR/$DATE # Compressed into .tar.gz format
find $TAR_DIR/ -mtime +$DAYS -delete # delete 7 Days before the backup file
0
Manually start crontab service:
#!/bin/sh
DUMP=/usr/local/mongodb/bin/mongodump #mongodump Backup file execution path
OUT_DIR=/home/backup/mongod_bak/mongod_bak_now # Temporary backup directory
TAR_DIR=/home/backup/mongod_bak/mongod_bak_list # Backup storage path
DATE=`date +%Y_%m_%d` # Gets the current system time
DB_USER=username # Database account
DB_PASS=123456 # Database password
DAYS=7 #DAYS=7 On behalf of the delete 7 Days before the backup, that is, only keep the most recent 7 Days of backup
TAR_BAK="mongod_bak_$DATE.tar.gz" # The final database backup file name saved
cd $OUT_DIR
rm -rf $OUT_DIR/*
mkdir -p $OUT_DIR/$DATE
$DUMP -u $DB_USER -p $DB_PASS -o $OUT_DIR/$DATE # Back up all databases
tar -zcvf $TAR_DIR/$TAR_BAK $OUT_DIR/$DATE # Compressed into .tar.gz format
find $TAR_DIR/ -mtime +$DAYS -delete # delete 7 Days before the backup file
1
Other commands:
#!/bin/sh
DUMP=/usr/local/mongodb/bin/mongodump #mongodump Backup file execution path
OUT_DIR=/home/backup/mongod_bak/mongod_bak_now # Temporary backup directory
TAR_DIR=/home/backup/mongod_bak/mongod_bak_list # Backup storage path
DATE=`date +%Y_%m_%d` # Gets the current system time
DB_USER=username # Database account
DB_PASS=123456 # Database password
DAYS=7 #DAYS=7 On behalf of the delete 7 Days before the backup, that is, only keep the most recent 7 Days of backup
TAR_BAK="mongod_bak_$DATE.tar.gz" # The final database backup file name saved
cd $OUT_DIR
rm -rf $OUT_DIR/*
mkdir -p $OUT_DIR/$DATE
$DUMP -u $DB_USER -p $DB_PASS -o $OUT_DIR/$DATE # Back up all databases
tar -zcvf $TAR_DIR/$TAR_BAK $OUT_DIR/$DATE # Compressed into .tar.gz format
find $TAR_DIR/ -mtime +$DAYS -delete # delete 7 Days before the backup file
2