먼저, 데이터를 백업
1.1 쓰기 일반 작업
- /etc/cron.d/backup.sh에서 쓰기해서 일반 작업
- 백업 MySQL 데이터베이스 스크립트를 수행 할 2시 10분 매일
- 매일 4시 10분 몽고 데이터베이스 백업 스크립트 실행
10 2 * * * root sh /backup/backup-mysql.sh
10 4 * * * root sh /backup/backup-mongodb.sh
1.2 백업 스크립트
1.2.1 MySQL의 백업 스크립트
- 데이터의 백업 MySQL 데이터베이스 전액을 innobackupex 사용
- 백업 데이터의 7 일 이상을 삭제하려면
#!/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 몽고 백업 스크립트
- 지정된 데이터베이스 mongodump를 백업 할 데이터를 사용
- 5 일 이상의 백업 파일 삭제
#!/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.1 MySQL의 데이터 복구
- innobackupex를 통해 백업 데이터를 복원
- 다시 시작 MySQL의 서비스
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 몽고 데이터 복구
- 를 사용하여 라이브러리를 개발하기 위해 데이터를 복원 mongorestore
- 다시 시작 몽고 서비스
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