First, back up your data
1.1 write regular tasks
- Write regular script regular tasks in /etc/cron.d/backup.sh
- Daily 2:10 to perform backup mysql database script
- Daily 4:10 mongo database backup script execution
10 2 * * * root sh /backup/backup-mysql.sh
10 4 * * * root sh /backup/backup-mongodb.sh
1.2 backup script
1.2.1 mysql backup script
- Use innobackupex backup mysql database full amount of data
- To delete more than seven days of backup data
#!/bin/sh
innobackupex --defaults-file=/etc/my.cnf --user=root --password=Mogu07550831 /backup/daily
currentFile=`ls /backup/daily | sort -rn | head -1`
historyFile=`ls /backup/daily | sort -n | head -1`
if [ "$currentFile" = "$historyFile" ]; then
echo "init backup"
else
cd /backup/daily
dirNum=`ls -l |grep "^d"|wc -l`
if (( dirNum > 7 )); then
echo "del $historyFile"
rm -rf /backup/daily/$historyFile
fi
fi
1.2.2 mongo backup script
- Use the data to back up the specified database mongodump
- Delete backup files of more than 5 days
#!/bin/sh
today=`date "+%Y%m%d-%H%M"`
cd /backup/mongodb
mkdir $today
chmod -R 777 $today
mongodump --authenticationDatabase=admin -d statistics -o $today -u root -p Mogu07550831 --gzip
mongodump --authenticationDatabase=admin -d everydaykline -o $today -u root -p Mogu07550831 --gzip
mongodump --authenticationDatabase=admin -d admin -o $today -u root -p Mogu07550831 --gzip
mongodump --authenticationDatabase=admin -d evaluation -o $today -u root -p Mogu07550831 --gzip
currentFile=`ls /backup/mongodb | sort -rn | head -1`
historyFile=`ls /backup/mongodb | sort -n | head -1`
if [ "$currentFile" = "$historyFile" ]; then
echo "init backup"
else
cd /backup/mongodb
dirNum=`ls -l |grep "^d"|wc -l`
if (( dirNum > 5 )); then
echo "del $historyFile"
rm -rf /backup/mongodb/$historyFile
fi
fi
2. Restore data
2.1 mysql data recovery
- Restore the backup data through innobackupex
- Restart mysql service
innobackupex --datadir=/var/lib/mysql --copy-back /bak/daily/2019-06-13_02-10-02/
chown -R mysql:mysql /var/lib/mysql
service mysql start
2.2 mongo data recovery
- Use mongorestore restore data to develop library
- Restart mongo Service
cd $sourcePath
unzip mongo-data-init.zip
# load init database
mongorestore -d admin --dir=$sourcePath/mongo-data-init/admin --gzip
mongorestore -d evaluation --dir=$sourcePath/mongo-data-init/evaluation --gzip
mongorestore -d statistic --dir=$sourcePath/mongo-data-init/statistics --gzip
mongorestore -d everydaykline --dir=$sourcePath/mongo-data-init/everydaykline --gzip
sleep 2s
ps -ef|grep "mongod --dbpath=/opt/data/mongodb" |grep -v grep |awk '{print $2}' | xargs kill -2
sleep 5s
cd /opt/mongodb
mongod -f conf/mongod.conf