dreamhost站点,MYSQL数据库备份shell脚本

Share

      由于本Blog是使用的DH的空间,又比较的懒,所以写了这个脚本。注意需要开通ssh的权限拉。
      每天晚上11点    
      0 23 * * * /home/username/bak.sh
      每星期周六晚上11点
      0 23 * * 6 /home/username/bak.sh
      每月28号晚上11点
      0 23 28 * * /home/username/bak.sh

#!/bin/bash
#
#   Auto Site Backup shell
#   For DreamHost user
#
#   Powered by leekooqi 
#   http://www.icnote.com/
#
# Database connection parm
DBName=dbname   #数据库名
DBUser=username  #数据库用户名
DBPasswd=123456  #数据库密码
DBUrl=192.168.0.1 #数据库地址

# local setting
# 备份文件存放目录
BackupPath=/home/hexuzhong/backup/$(date +%y%m%d)/
if [ -d $BackupPath ]
then
  rm -rf "$BackupPath"*
else
  mkdir $BackupPath
  chmod 755 $BackupPath -R
fi
LogFile="$BackupPath"backUp.log
# 需要备份的站点文件目录
SiteDir=/home/icnote/icnote.com/
TempSqlFileName="$BackupPath"tempSQL.sql
SiteBackUpName="$BackupPath"ajaxeye.com.upload$(date +%y%m%d).tar.gz
DBDumpFileName="$BackupPath"db.ajaxeye.com$(date +%y%m%d).sql.tar.gz

echo "-------------------------------------------" >> $LogFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile
echo "--------------------------" >> $LogFile
echo "Site back up start............." >> $LogFile
echo "Web files back up start............." >> $LogFile


if [ -f $SiteBackUpName ] 
then 
  echo "[$SiteBackUpName]The Backup File is exists,Can't Backup!" >> $LogFile 
else 
  tar czvf $SiteBackUpName $SiteDir >> $LogFile
  echo "[$SiteBackUpName]Backup Success!" >> $LogFile
fi

echo "Web files back up finished!" >> $LogFile
echo "Now database dump start" >> $LogFile

if [ -f $DBDumpFileName ]
then 
  echo "[$DBDumpFileName]The Backup File is exists,Can't Backup!" >> $LogFile 
else
  mysqldump -h$DBUrl -u$DBUser -p$DBPasswd --opt $DBName > $TempSqlFileName 
  tar czvf $DBDumpFileName $TempSqlFileName >> $LogFile
  rm -rf $TempSqlFileName
  echo "[$DBDumpFileName] Backup Success!" >> $LogFile
fi

echo "Now database dump finished!" >> $LogFile
echo "The whole backup task completed at "$(date +"%y-%m-%d %H:%M:%S") >> $LogFile
echo "########################################" >> $LogFile