七叶笔记 » golang编程 » linux下备份文件及mysql数据库的脚本

linux下备份文件及mysql数据库的脚本

需要完整的全部代码的发私信给我。

变量申请部分:

linux下获取随机密码的脚本:主要要把密码写入到log日志上,后续解压需用到该密码

#set envariable for md5

#mk5 info file in env $BAKDEST

export pswd=`< /dev/urandom tr -dc 0-9-A-Z-a-z-|head -c ${1:-20};echo`

export sysflag=tmsp

export dbflag=${sysflag}_db

export apflag=${sysflag}_ap

export md5flag=$BAKDEST/${sysflag}_${today}_md5.log

export logflag=$BAKDEST/${sysflag}_${today}.log

备份mysql和应用的脚本:

#backup database

mysqldump -uroot -ptmsp11 source > $BAKDEST/${dbflag}_${today}.bak

#backup application

tar -czvf – –exclude=*.log* –exclude=*.out* $FILEDEST | openssl enc -e -aes256 -k $pswd -out ${apflag}_${today}.tar.gz

echo “#################” > ${md5flag}

echo “date:”${today} >> ${md5flag}

echo “pswd:”$pswd >> ${md5flag}

md5sum ${apflag}_${today}.tar.gz >> ${md5flag}

echo “###untar command:” >> ${md5flag}

echo “mkdir -p “$FILEDEST >> ${md5flag}

echo “openssl enc -d -aes256 -k ” $pswd ” -in ${sysflag}_”${today}”.tar.gz | tar xz -C $FILEDEST” >> ${md5flag}

删除旧归档文件的脚本:这里保留7份最后的备份文件,可以根据自己需要调整

#remove old backup file

#keep 7 active bakcup file

echo “#########del history tar.gz file ##################”;date

find $BAKDEST -type f -name “${sysflag}.*” ! -name “*.sh” | sort -nr | awk ‘{if (NR>=7){print $1}}’|xargs rm -f

备份文件传送至异机ftp备份服务器的脚本:

#upload backup file to lftp server

#set env par for local ftp sever

export LFTPFLAG=1#控制是否进行备份

export LFTPSERVER=***.***.***.***

export LFTPUSER=****

export LFTPPWD=****

export LFTPBAK=/food_source

export LFTPSH=$BAKDEST/lftp_bak.sh

echo “#####Start Copy back data to lftp server########”;date

if [ ${LFTPFLAG} -eq 1 ]; then

cd $TOPBAK

find . -type f -mtime -1 -name “${sysflag}*” ! -name “ftp_file.txt” > $BAKDEST/ftp_file.txt

sed -i ‘s/^/put /’ $BAKDEST/ftp_file.txt

echo “ftp -niv <<- EOF” > $BAKDEST/ftp_file.temp

echo “open “$LFTPSERVER >> $BAKDEST/ftp_file.temp

echo “user “$LFTPUSER $LFTPPWD >> $BAKDEST/ftp_file.temp

echo “lcd “$TOPBAK >> $BAKDEST/ftp_file.temp

echo “cd “$LFTPBAK >> $BAKDEST/ftp_file.temp

echo “bin” >> $BAKDEST/ftp_file.temp

cat $BAKDEST/ftp_file.temp $BAKDEST/ftp_file.txt > $LFTPSH

rm -f $BAKDEST/ftp_file.temp

rm -f $BAKDEST/ftp_file.txt

echo “bye” >> $LFTPSH

echo “EOF” >> $LFTPSH

chmod +x $LFTPSH

sh $LFTPSH

fi

echo “#####End Copy back data to lftp server########”;

备份文件传送到异地ftp服务器的脚本:这里走的是阿里云的sftp

#set env par for aliyun ftp

export CFTPFLAG=1

export CFTPSERVER=***.***.***.***

export CFTPUSER=***

export CFTPPWD=***

export CFTPBAK=/data/backup/food_source

export CFTPSH=$BAKDEST/cftp_bak.sh

echo “#####Start Copy back data to aliyun ftp server########”;date

if [ ${CFTPFLAG} -eq 1 ]; then

cd $TOPBAK

find . -type f -mtime -1 -name “${sysflag}*” > $BAKDEST/ftp_file.txt

sed -i ‘s/^/put /’ $BAKDEST/ftp_file.txt

echo “sftp backup@”${CFTPSERVER}” <<EOF” > $BAKDEST/ftp_file.temp

echo “lcd “$TOPBAK >> $BAKDEST/ftp_file.temp

echo “cd “$CFTPBAK >> $BAKDEST/ftp_file.temp

cat $BAKDEST/ftp_file.temp $BAKDEST/ftp_file.txt > $CFTPSH

rm -f $BAKDEST/ftp_file.temp

rm -f $BAKDEST/ftp_file.txt

echo “exit” >> $CFTPSH

echo “EOF” >> $CFTPSH

chmod +x $CFTPSH

sh $CFTPSH

fi

echo “#####End Copy back data to aliyun ftp server########”;

利用crontab就可以实现定时备份文件夹、mysql数据库,并传送到本地ftp服务器、远方sftp服务器。

相关文章