linux mysql 自动备份数据库脚本

1.脚本内容:只保留七天数据库备份

#!/bin/bash

export  BACKUP_DIR="/home/backup"
export  LANG="en_US.UTF8"
export  DATE_1=`date +"%Y-%m-%d"`
export  DATE_2=`date -d "-7 day"  +"%Y-%m-%d"`
export  DB_NAME=$1
echo 'starting backup'


test -f $BACKUP_DIR/$DB_NAME.back_$DATE_1.sql

if [ $? -eq 0 ] ; then

  echo "Today's(`date +"%Y-%m-%d %H:%M:%S"`) BACKUP job has done"


else
mysqldump -uroot -p115my!!Cmri $DB_NAME > /home/backup/$DB_NAME.back_$DATE_1.sql 


######check the dump file whether produced successfully


  test -f $BACKUP_DIR/$DB_NAME.back_$DATE_1.sql


  if [ $? -eq 0 ]; then
   
     echo " `date +"%Y-%m-%d %H:%M:%S"` the database db_kenyon has been backuped to $BACKUP_DIR/db_pds_$DB_NAME.back_$DATE_1 successfully"
     
     test -f $BACKUP_DIR/$DB_NAME.back_$DATE_2.sql


     if [ $? -eq 0 ] ; then


      rm -f $BACKUP_DIR/$DB_NAME.back_$DATE_2.sql


      echo " `date +"%Y-%m-%d %H:%M:%S"` the file $BACKUP_DIR/db_kenyon_back_$DATE_2 has been moved"


     fi
     
  else
    
     echo " `date +"%Y-%m-%d %H:%M:%S"` the pg_dump failed"


  fi


fi

2.设置定时任务:

0 5 * * * /路径/backup.sh testDB:凌晨五点执行 testDB是要自动备份传递的数据库名





你可能感兴趣的:(Linux)