千家信息网

mysql数据库备份脚本

发表于:2024-11-28 作者:千家信息网编辑
千家信息网最后更新 2024年11月28日,#!/bin/bashBegin=date +"%Y年%m月%d日 %H:%M:%S"BakDir=/home/backup/backupLogFile=/home/backup/backup/bak
千家信息网最后更新 2024年11月28日mysql数据库备份脚本

#!/bin/bash

Begin=date +"%Y年%m月%d日 %H:%M:%S"
BakDir=/home/backup/backup
LogFile=/home/backup/backup/bak.log
Date=date +%Y%m%d

cd $BakDir

DumpFile=$Date.sql
BZDumpFile=$Date.sql.tar.bz2
/usr/bin/mysqldump -u数据库登录帐号 -p数据库密码 --all-databases > $DumpFile
/bin/tar jvcf $BZDumpFile $DumpFile
/bin/rm $DumpFile

openssl enc -k 备份加密密码 -aes-128-ecb -e -in $BZDumpFile -out $BZDumpFile-aes
#-d 解密
/bin/rm $BZDumpFile

Last=date +"%Y年%m月%d日 %H:%M:%S"
echo "">>$LogFile
echo "---------fullybak--------">>$LogFile
echo "$BZDumpFile-aes copying">>$LogFile
echo 开始:$Begin 结束:$Last $BZDumpFile-aes succ >> $LogFile

#!/bin/bash

Begin=date +"%Y年%m月%d日 %H:%M:%S"

BakDir=/home/backup/backup
LogFile=/home/backup/backup/bak.log
Date=date +%Y%m%d

cd $BakDir

echo "">>$LogFile
echo "---------scp--------">>$LogFile

if (test -e $Date.sql.tar.bz2-aes)
then
scp $BakDir/$Date.sql.tar.bz2-aes backup@10.0.0.22:/home/backup/backup55
if [ "$?" = "0" ]
then
echo "$Date.sql.tar.bz2-aes sending...">>$LogFile
else
echo "scp failed...">>$LogFile
fi
else
echo "$Date.sql.tar.bz2-aes not exist!">>$LogFile
fi

if (test -e $Date.tar.bz2-aes)
then
scp $BakDir/$Date.tar.bz2-aes backup@10.0.0.22:/home/backup/backup55
if [ "$?" = "0" ]
then
echo "$Date.tar.bz2-aes sending...">>$LogFile
else
echo "scp failed...">>$LogFile
fi
else
echo "$Date.tar.bz2-aes not exist!">>$LogFile
fi

Last=date +"%Y年%m月%d日 %H:%M:%S"

echo 开始:$Begin 结束:$Last succ >> $LogFile

#!/bin/bash

Begin=date +"%Y年%m月%d日 %H:%M:%S"
LogFile=/home/backup/backup/bak.log
BakDir=/home/backup/backup
cd $BakDir
oldBakDir0=date -d '-35 days' +%Y%m%d.tar.bz2-aes
oldBakDir1=date -d '-34 days' +%Y%m%d.tar.bz2-aes
oldBakDir2=date -d '-33 days' +%Y%m%d.tar.bz2-aes
oldBakDir3=date -d '-32 days' +%Y%m%d.tar.bz2-aes
oldBakDir4=date -d '-31 days' +%Y%m%d.tar.bz2-aes
oldBakDir5=date -d '-30 days' +%Y%m%d.tar.bz2-aes
oldBakDir6=date -d '-29 days' +%Y%m%d.tar.bz2-aes
oldBakFile=date -d '-35 days' +%Y%m%d.sql.tar.bz2-aes

Date=date +%Y%m%d
GZDumpFile=$Date.sql.tar.bz2-aes

echo "">>$LogFile
echo "---------rm--------">>$LogFile
if (test -e $GZDumpFile)
then
if (test -e $oldBakDir0)
then
rm -rf $oldBakDir0
echo "delete increased sql $oldBakDir0" >> $LogFile
else
echo "$oldBakDir0 not exist!" >> $LogFile
fi

if (test -e $oldBakDir1)then    rm -rf $oldBakDir1    echo "delete increased sql $oldBakDir1" >> $LogFileelse    echo "$oldBakDir1 not exist!" >> $LogFilefiif (test -e $oldBakDir2)then    rm -rf $oldBakDir2    echo "delete increased sql $oldBakDir2" >> $LogFileelse    echo "$oldBakDir2 not exist!" >> $LogFilefiif (test -e $oldBakDir3)then    rm -rf $oldBakDir3    echo "delete increased sql $oldBakDir3" >> $LogFileelse    echo "$oldBakDir3 not exist!" >> $LogFilefiif (test -e $oldBakDir4)then    rm -rf $oldBakDir4    echo "delete increased sql $oldBakDir4" >> $LogFileelse    echo "$oldBakDir4 not exist!" >> $LogFilefiif (test -e $oldBakDir5)then    rm -rf $oldBakDir5    echo "delete increased sql $oldBakDir5" >> $LogFileelse    echo "$oldBakDir5 not exist!" >> $LogFilefiif (test -e $oldBakDir6)then    rm -rf $oldBakDir6    echo "delete increased sql $oldBakDir6" >> $LogFileelse    echo "$oldBakDir6 not exist!" >> $LogFilefiif (test -e $oldBakFile)then    rm -rf $oldBakFile    echo "delete full sql $oldBakFile" >> $LogFileelse    echo "$oldBakFile not exist!" >> $LogFilefi

else
echo "Warning:There is no backup today!!" >> $LogFile
fi

Last=date +"%Y年%m月%d日 %H:%M:%S"
echo 开始:$Begin 结束:$Last succ >> $LogFile

0 23 0 /bin/sh /home/backup/Mysql-FullyBak.sh > /home/backup/cronlog.txt 2>&1
45 23 0 /bin/sh /home/backup/rmBak.sh > /home/backup/cronlog.txt 2>&1
30 23 * /bin/sh /home/backup/scpBak.sh > /home/backup/cronlog.txt 2>&1

#! /bin/bash

username=root
ps='TH'
mysql_data="/home/mysql_backup/data"/lvshan.sql
password=echo $ps|base64 -d
mysql_zip="/home/mysql_backup/data"/lvshan_zip.zip
mysql_back="/home/mysqlbackup/data"/lvshandata +%Y_%m_%d.zip

/usr/bin/mysqldump --opt -u$username -p$password -hlocalhost lvshan > $mysql_data
/bin/zip $mysql_zip $mysql_data
/bin/rm $mysql_data

openssl enc -k 备份加密密码 -aes-128-ecb -e -in $mysql_zip -out $mysql_back
/bin/rm $mysql_zip

scp -P 6008 $mysql_back root@ip:/home/mysql_backup_lvshan_ip

0