mysql自动备份

    1. #!/bin/bash  
    2. MyUSER="SET-MYSQL-USER-NAME"     # USERNAME  
    3. MyPASS="SET-PASSWORD"       # PASSWORD   
    4. MyHOST="localhost"          # Hostname  
    5. # Linux bin paths, change this if it can not be autodetected via which command  
    6. MYSQL="$(which mysql)"  
    7. MYSQLDUMP="$(which mysqldump)"  
    8. CHOWN="$(which chown)"  
    9. CHMOD="$(which chmod)"  
    10. GZIP="$(which gzip)"  
    11.   
    12. # Backup Dest directory, change this if you have someother location  
    13. DEST="/backup"  
    14.   
    15. # Main directory where backup will be stored  
    16. MBD="$DEST/mysql"  
    17.   
    18. # Get hostname  
    19. HOST="$(hostname)"  
    20.   
    21. # Get data in dd-mm-yyyy format  
    22. NOW="$(date +"%d-%m-%Y")"  
    23.   
    24. # File to store current backup file  
    25. FILE=""  
    26. # Store list of databases   
    27. DBS=""  
    28.   
    29. # DO NOT BACKUP these databases  
    30. IGGY="test"  
    31.    
    32. [ ! -d $MBD ] && mkdir -p $MBD || :  
    33.   
    34. # Only root can access it!  
    35. $CHOWN 0.0 -R $DEST  
    36. $CHMOD 0600 $DEST  
    37.   
    38. # Get all database list first  
    39. DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"  
    40.    
    41. for db in $DBS  
    42. do  
    43.     skipdb=-1  
    44.     if [ "$IGGY" != "" ];  
    45.     then  
    46.     for i in $IGGY  
    47.     do  
    48.         [ "$db" == "$i" ] && skipdb=1 || :  
    49.     done  
    50.     fi  
    51.    
    52.     if [ "$skipdb" == "-1" ] ; then  
    53.     FILE="$MBD/$db.$HOST.$NOW.gz"  
    54.     # do all inone job in pipe,  
    55.     # connect to mysql using mysqldump for select mysql database  
    56.     # and pipe it out to gz file in backup dir :)  
    57.         $MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE  
    58.     fi  
    59. done 
原文地址:https://www.cnblogs.com/networking/p/3669289.html