One, the former yan

Mysql database is backed up periodically through mysqldump and crontab

The script for backing up the database is as follows

backMysql.sh

#! / bin/bash PATH = / bin, / sbin, / usr/bin: / usr/sbin, / usr/local/bin: / usr/local/sbin export PATH # database IP DBHOST = 'XXX. XXX. XXX. XXX' DBPASSWD='password' // select * from db host where dbbuser ='admin' Multiple databases are separated by Spaces DBNAME=' DBNAME '// Database name # Backup time Backtime =' date +%Y-% M -%d_%H% m %S' # Backup path (current directory) BACKPATH=$(dirName $(readlink -f Echo $BACKPATH ="${BACKPATH}/log" ${BACKPATH}/db"  -d "${LOGPATH}" ] && mkdir -p "${LOGPATH}" [ ! -d "${DBPATH}"] && mkdir -p "${DBPATH}" # echo "backtime "${DBNAME} start" >> ${LOGPATH}/mysqlback.log do source=`mysqldump -u ${DBUSER} -h${DBHOST} -p${DBPASSWD} ${table}> ${LOGPATH}/${backtime}.sql` 2>> ${LOGPATH}/mysqlback.log; $? If ["$?" == 0]; Tar -czf ${DBPATH}/${table}${backtime}.tar.gz. /${backtime}. SQL > /dev/null Rm -f ${LOGPATH}/${backtime}. SQL Find $dbpath-name "*.tar.gz" -type f-mtime + 7-exec rm -rf {} \; find $dbpath-name "*.tar.gz" -type f-mtime + 7-exec rm -rf {} \; > /dev/null 2>&1 echo "${DBNAME} back up successfully!!" >> ${LOGPATH}/mysqlback.log else ${DBNAME} /mysqlback.log >> ${LOGPATH}/mysqlback.log fi doneCopy the code

3. Regular backup

Run the backmysql. sh script on crontab periodically to back up data every hour as follows:

  1. Run crontab -e
  2. 0 * * * * / home/admin/dbBackup backMysql. Sh

Crontab is backed up every hour on the hour. You can check the usage of crontab