首頁論壇分類作業系統Linux【Linux】實用 MySQL/MariaDB 自動備份 - 連續
【Linux】實用 MySQL/MariaDB 自動備份 - 連續
   編 輯        刪 除      # 5c8224a79c11d 發表: Odyssey     新增:2018-11-26 09:49:12     更新:2018-11-26 09:49:12

此腳本原始出處來自「網頁搜尋」
所以出處已不可考
而且也已有所增修

#!/bin/sh
# -------------------------------------------------------------------------------------------
# CentOS Auto Complete Backup MySQL/MariaDB Database
# Version: 2014/11/06
# -------------------------------------------------------------------------------------------
# Databse Basic Settings
# -------------------------------------------------------------------------------------------
# DB Host
DBHost=''
# DB Name
DBName=''
# DB User Name
DBUser=''
# DB User Password
DBPasswd=''
# DB Default Character Set
DefaultCharacterSet='utf8'
# -------------------------------------------------------------------------------------------
# Backup Data Storage Path
# -------------------------------------------------------------------------------------------
# Storage Directory
StorageDirectory='/home'
# Backup Process Log
LogFile=''
# -------------------------------------------------------------------------------------------
# Execute File Path
# -------------------------------------------------------------------------------------------
# mysqldump File Path
MYSQLDUMP='/usr/local/mysql/bin/mysqldump'
# tar File Path
TAR='/bin/tar'
# rm File Path
RM='/bin/rm'
# -------------------------------------------------------------------------------------------
# Define Backup File Name
# -------------------------------------------------------------------------------------------
# Today Original Backup Filename
DumpFile=$StorageDirectory$(date +%Y-%m-%d)_$DBName.sql
# Today Compress Backup Filename
NewFile=$StorageDirectory$(date +%Y-%m-%d)_$DBName.bz2
# -------------------------------------------------------------------------------------------
# Shell Script
# -------------------------------------------------------------------------------------------
# Begin
echo "-------------------------------------------" >> $LogFile
# Write Script Execute Time
echo $(date +"%Y-%m-%d %H:%M:%S") >> $LogFile

# Check Exist Today Backup Compress File
if [ -f $NewFile ] ; then
    # Write Error Message
    echo "Error! [$NewFile] file already exist. Can not execute backup action!" >> $LogFile
else
    # --------------------------------------
    # Check Exist Today Backup Original File
    if [ -f $DumpFile ] ; then
        # Write Error Message
        echo "Error! [$DumpFile] file still exist. Can not execute backup action!" >> $LogFile
        # Break if
        break 2
    fi
    # --------------------------------------
    # Execute Real Backup Command
    $MYSQLDUMP -u $DBUser -p$DBPasswd --lock-all-tables --default-character-set=$DefaultCharacterSet --complete-insert $DBName > $DumpFile
    # --------------------------------------
    # Check Exist Original Backup SQL File
    if [ -f $DumpFile ] ; then
        # Write Success Message
        echo "Success! [$DumpFile] file is existing. Complete the Real Backup Command action!" >> $LogFile
        # --------------------------------------
        # Compress The Original Backup File
        $TAR -jcf $NewFile $DumpFile >> $LogFile 2>&1
        # --------------------------------------
        # Check Exist Compress Backup File
        if [ -f $NewFile ] ; then
            # Write Compress Action Log
            echo "Success! [$NewFile] backup file compress complete!" >> $LogFile
            # --------------------------------------
            # Delete The Original Backup File
            $RM -rf $DumpFile
            # --------------------------------------
            # Check Exist Today Backup Compress File
            if [ -f $DumpFile ] ; then
                # Write Delete Action Log
                echo "Error! [$DumpFile] source file delete fail!" >> $LogFile
            else
                # Write Delete Action Log
                echo "Success! [$DumpFile] source file delete complete!" >> $LogFile
            fi
            # --------------------------------------
        else
            # Write Error Message
            echo "Error! [$NewFile] backup file compress fail!" >> $LogFile
        fi
        # --------------------------------------
    else
        # Write Error Message
        echo "Error! [$DumpFile] file is not exist. Fail the Real Backup Command action!" >> $LogFile
    fi
    # --------------------------------------
fi

# End
echo "-------------------------------------------" >> $LogFile
# -------------------------------------------------------------------------------------------

夢幻奧德修斯 - OdysseusDream
Website:https://www.odysseusdream.com/
Discord:https://discord.gg/ht4mwMG
FB社團:https://www.facebook.com/groups/OdysseusDream/

我要回覆
【Linux】實用 MySQL/MariaDB 自動備份 - 連續
首頁論壇分類作業系統Linux【Linux】實用 MySQL/MariaDB 自動備份 - 連續