网站应用与mysql自动完全增量备份

!#/bin/bash
today=$(date +%Y%m%d) #设置日期
backupdir=/app/backup/app/ #设置备份目录
sqlbackupdir=/app/backup/sql/ #设置sql备份目录
#设置ftp登录信息
ftp_hostname=yang.test
ftp_user=yang
ftp_password=yang
#设置mysql相关信息
sql_user=root
sql_password=root
databasename=mysql
datadir=/usr/local/mysql/data
#设置网站应用程序所在目录
appdir=/etc/httpd/wwwroot/
#查看是否存在30天前文件
#old=$(find -mtime 30 | wc -l)
oldfilename=$(find $appbackupdir -mtime 30)
olddbfimename=$(find $sqlbackupdir -mtime 30)
#定义备份文件名
allbackup=$(date +%Y%m%d)_app_all.tgz
inbackup=$(date +%Y%m%d)_app_incremental.tgz
#sql备份文件名
sqlbackup=$(date +%Y%m%d)_sql_all.sql
insqlbackup=$$(date +%Y%m%d)_sql_incremental.sql
#应用是否有文件修改
todayebit=$(find $appdir -mtime 0)
#30天前完整备份文件名
oldallbackup=/app/backup/app/$(date -d "30 days ago" +%Y%m%d)_app_all.tgz
oldsqlbackup=/app/backup/sql/$(date -d "30 days ago" +%Y%m%d)_sql_all.sql
#开始备份app
echo "This is app's backup!"  >>  /usr/logs/backup.log
echo "Start App Backup!"    >>  /usr/logs/backup.log
echo "_________________________________________" >>  /usr/logs/backup.log
if [ -e $oldallbackup ];thin
        cd $backupdir
        rm -rf *
        tar -zcvf $inbackup $appdir -C $backupdir >>  /usr/logs/backup.log >2&1
        echo "________________________________________________________________"  >>  /usr/logs/backup.log
    else
        cd $backupdir
        if [ -e $todayedit ];thin  #判断今天有没有更改文件
                echo "new files is not today"  >>  /usr/logs/backup.log
                echo "________________________________________________________________"  >>  /usr/logs/backup.log >2&1
                    else
                        tar zcvf $inbackup $todayebit -C $backupdir >>  /usr/logs/backup.log  #开始增量备份
                        echo "________________________________________________________________"  >>  /usr/logs/backup.log
        fi
fi
    echo "Backup is success!"
#开始备份mysql
todayappbackup=$(find /app/backup/app -mtime 0)
todaydbbackup=$(find /app/backup/sql -mtime 0)
cd $sqlbackupdir
#模式1:完整备份(不需要可注释)
mysqldump --flush-logs -u -$sql_user -p $sql_password --quick $databasename > sqlbackup=$(date +%Y%m%d)_sql_all.sql
#若数据量不是很大,则每天进行完整备份删除前一天备份
rm -rfv `find . -mtime 1` >>  /usr/logs/backup.log
#模式2:完整备份加增量备份
echo "This is sql's backup!"  >>  /usr/logs/backup.log
echo "Start sql Backup!"    >>  /usr/logs/backup.log
echo "_________________________________________" >>  /usr/logs/backup.log
cd $backupdir
if [ -e $oldsqlbackup ];thin
        rm -rf *
        mysqldump --flush-logs -u -$sql_user -p $sql_password --quick $databasename > sqlbackup=$(date +%Y%m%d)_sql_all.sql
        echo "________________________________________________________________"  >>  /usr/logs/backup.log
    else
        mysqladmin flush-logs
        cd $datadir
        FILELIST=`cat mysql-bin.index`
        COUNTER=0
        for file in $FILELIST
        do
            COUNTER=`expr $COUNTER + 1 `
        done
        NextNum=0
        for file in  $FILELIST
        do
                    base=`basename $file`
            NextNum=`expr $NextNum + 1`
            if [ $NextNum -eq $COUNTER ];then
                echo "skip lastest"
                    else
                dest=$sqlbackupdir/$base
                if(test -e $dest)
                    then
                        echo "skip exist $base"
                    else
                        echo "copying $base"
                        cp -rv $base $sqlbackupdir
                    fi
            fi
        done
fi
#上传到ftp服务器
ftp $ftp_hostname -u $ftp_user -p $ftp_password << EOF
put     $todayappbackup
put     $todaydbbackup
bye
EOF

   赋予脚本可执行权限并加入计划任务即可进行mysql与app的自动备份

你可能感兴趣的:(mysql,APP,应用程序,增量备份,完全备份)