linux mysql备份shell

 

#!/bin/bash
# Shell script to backup MySql database
# Author: Henry he
# Last updated: 2014-08-25
# crontab -e
# 0 8 * * 1 /home/elkan/H_Docs/PortMasterListPortal/backup.sh >> /opt/mysql_backup/log

echo $(date +"%Y-%m-%d %H:%M:%S")

username="root"     # USERNAME
password="123456"       # PASSWORD 
Hostname="localhost"          # Hostname
DBName="PortMasterList" #DB name
#DBName="testMavenWeb"
programProt="8080"

#max save backup file number
maxBackUpFileCount=5

# Linux bin paths, change this if it can not be autodetected via which command
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"

# Main directory where backup will be stored
backupDir="/opt/mysql_backup/"$DBName

# if not exsis then mkdir
[ ! -d $backupDir ] && mkdir -p $backupDir || :

# Get data in dd-mm-yyyy format
NOW="$(date +"%Y-%m-%d-%H-%M-%S")"

FILE="$backupDir/$DBName.$NOW.bak"

# Only root can access it!
#$CHOWN 0.0 -R $backupDir
#$CHMOD 0600 $backupDir

# connect to mysql using mysqldump for select mysql database
# and pipe it out to file in backup dir
echo 'usenrame:'$username 'password:'$password 'DBName:'$DBName
$MYSQLDUMP -u $username -h $Hostname -p$password $DBName > $FILE

# check the backup is success
success="true"
if [ ! -s "$FILE" ]; then 
    success="false"
    rm -f $FILE
    echo 'backup fail'
else
    # delete the redundant backup files
    i=0
    for file in $(ls -t $backupDir)
    do
        i=`expr $i + 1`
        #echo $file
        if [ "$i" -gt "$maxBackUpFileCount" ]
        then
            $(rm -f "$backupDir/$file")
            echo 'rm file':"$backupDir/$file"
        fi
    done

    echo 'backup success file path:'$FILE
fi

#curl calling java to send email
curl 'http://'$Hostname':'$programProt'/PortMasterListPortal/ajax/sendBackup.html?success='$success'&secureKey=xxx'
echo ''
原文地址:https://www.cnblogs.com/hzm112567/p/3936774.html