mysql自动备份shell脚本

 #!/bin/bash
  bak_path=/data/bak_path
  mysql_bk_dir=$bak_path/mysql_bk_dir
  log_file=$backup_path/backuplog
  password=123456
   for i in $(mysql -p$password -e "show databases\G" |grep Database | awk '{print $2}')
  do
     new_file="$mysql_bk_dir"/$i$(date +%Y%m%d).tar.gz
     dump_file="$mysql_bk_dir"/$i$(date +%Y%m%d).sql
    old_file="$mysql_bk_dir"/$i$(date +%Y$m%d).tar.gz
    echo "------------------------------------------------------------" >> $log_file
    echo $(date +"%Y-%m-%d %H:%M:%S") >> $log_file
   echo "------------------------------------------------------------" >>  $log_file
     if [ -f $old_file ]; then
       rm -rf $old_file >> $log_file 2>&1;
      echo "[$old_file] Delete Old File Success!" >> $log_file
     else
         echo "[$old_file] No Old Backup File!" >> $log_file
      fi
      if [ -f $new_file ]; then
         echo "[$new_file] The backup file is exist, can't backup!" >> $log_file;
      else
         cd $mysql_bk_dir
/usr/local/mysql/bin/mysqldump --opt $i -p$password > $dump_file
        tar zcf $new_file $dump_file >> $log_file 2>&1
         echo "[$new_file] Backup success!" >> $log_file
         rm -rf $dump_file
      fi
     sleep 15
done

添加新评论 »