最全最强悍的linux自动备份+自动删除老的备份脚本

先新建一个backup.sh的文件 在把下面的内容添加进去改改实际路径

做个计划任务就自动备份了 命令:crontab -e

#!/bin/bash

#backup.sh

#备份至/root/bak/

BackupPath=/root/bak/

LogFile=/root/bak/back.log

DBPath=/usr/local/app/   #需要备份的目录

NewFile="$BackupPath"app_back$(date +%y%m%d).tgz

DumpFile="$BackupPath"app_back$(date +%y%m%d)

OldFile="$BackupPath"app_back$(date +%y%m%d --date='7 days ago').tgz

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

echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile

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

Delete Old File

if [  -f $OldFile ]

then

   rm -f $OldFile >> $LogFile 2>&1

   echo "[$OldFile]Delete Old File Success!" >> $LogFile

else

   tar cpzvf $NewFile $DBPath >> $LogFile 2>&1

   echo "[$OldFile]No Old Backup File!" >> $LogFile

fi

 

if [  -f $NewFile ]

then

echo "[$NewFile]The Backup File is exists,Can't Backup!" >> $LogFile

 

#      tar pczvf $NewFile $DBPath >> $LogFile 2>&1

      echo "[$NewFile]Backup Success!" >> $LogFile

else

   tar cpzvf $NewFile $DBPath >> $LogFile 2>&1

fi

 

如果有多个目录需要备份则多把那几个变量复制下 在改个参数就可以了

 

下面是mysql数据库自动备份脚本

 

参数按照你们实际的路径

 

#!/bin/bash

#备份至/root/dbxxxxx.tgz

#This is a ShellScript For Auto DB Backup

#Powered by aspbiz

#Setting

DBName=mysql

DBUser=root

DBPasswd=123456

BackupPath=/root/

LogFile=/root/db.log

DBPath=/var/lib/mysql/

#BackupMethod=mysqldump

#BackupMethod=mysqlhotcopy

#BackupMethod=tar

#Setting End

 

 

NewFile="$BackupPath"db$(date +%y%m%d).tgz

DumpFile="$BackupPath"db$(date +%y%m%d)

OldFile="$BackupPath"db$(date +%y%m%d --date='30 days ago').tgz

 

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

echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile

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

#Delete Old File

if [ -f $OldFile ]

then

   rm -f $OldFile >> $LogFile 2>&1

   echo "[$OldFile]Delete Old File Success!" >> $LogFile

else

   echo "[$OldFile]No Old Backup File!" >> $LogFile

fi

 

if [ -f $NewFile ]

then

   echo "[$NewFile]The Backup File is exists,Can't Backup!" >> $LogFile

else

   case $BackupMethod in

   mysqldump)

      if [ -z $DBPasswd ]

      then

         mysqldump -u $DBUser --opt $DBName > $DumpFileelse

         mysqldump -u $DBUser -p$DBPasswd --opt $DBName > $DumpFile

      fi

      tar czvf $NewFile $DumpFile >> $LogFile 2>&1

      echo "[$NewFile]Backup Success!" >> $LogFile

      rm -rf $DumpFile

      ;;

   mysqlhotcopy)

      rm -rf $DumpFile

      mkdir $DumpFile

      if [ -z $DBPasswd ]

      then

         mysqlhotcopy -u $DBUser $DBName $DumpFile >> $LogFile 2>&1

      else

         mysqlhotcopy -u $DBUser -p $DBPasswd $DBName $DumpFile >>$LogFile 2>&1

      fi

      tar czvf $NewFile $DumpFile >> $LogFile 2>&1

      echo "[$NewFile]Backup Success!" >> $LogFile

      rm -rf $DumpFile

      ;;

   *)

      /etc/init.d/mysqld stop >/dev/null 2>&1

      tar czvf $NewFile $DBPath$DBName >> $LogFile 2>&1

      /etc/init.d/mysqld start >/dev/null 2>&1

      echo "[$NewFile]Backup Success!" >> $LogFile

      ;;

   esac

fi

 

希望你们有更好的

有问题可以联系我qq 544040277              流浪的风

 

你可能感兴趣的:(linux,职场,休闲,linux备份脚本)