SSH设置系统计划任务备份网站到邮箱

2011年3月19日 | 分类: Linux | 标签: , , , , , , , , ,

给系统添加计划任务,实现将网站重要的文件夹备份。特定:每天增量备份,每个星期一完全备份,且切割为5M大小的附件发送到你的邮箱。

wget http://www.yishanhome.com/wp-content/uploads/2011/03/bak_web-install.sh;sh bak_web-install.sh

#!/bin/bash
printf "
#This is a Shell-Based tool of Backup web sites directory
# Author: yishan
# Website: http://www.yishanhome.com
"
echo "Command is running, please wait....."
  ret=`rpm -qa|grep mutt|wc -l`
if [ $ret = "0" ] ; then
  echo "install mutt"
    yum install -y mutt
  fi
  ret=`service crond stop|wc -l`
if [ $ret = "0" ] ; then
  echo "install crond"
    yum install -y vixie-cron
  fi
echo "Command is ready, press any key to continue....."
read anykey
workdir="/root"
wwwdir="/home"
outdir="/root/bak_web"
ip=`hostname -i`
zippws="yishanhome"
echo "Please input the Shell-Based Work directory:"
  read -p "(Current location: $workdir):" workdir
  if [ "$workdir" = "" ]; then
   workdir="/root"
  fi
echo "Please input the website you need to backup the directory path:"
  read -p "(Current location: $wwwdir):" wwwdir
  if [ "$wwwdir" = "" ]; then
   wwwdir="/home"
  fi
echo "Please input your backup file directory path:"
  read -p "(Current directory: $outdir):" outdir
  if [ "$outdir" = "" ]; then
   outdir="/root/bak_web"
  fi
input_mailsend()
{
  read -p "Please input the email address stored backup files:" mailsend
  if [ "$mailsend" = "" ]; then
  input_mailsend
  fi
}
input_mailsend
echo -e '\033[0;31;1mPlease input ZIP Password:\033[0m'
  read -p "(default : $zippws):" zippws
    if [ "$zippws" = "" ]; then
    zippws=yishanhome
  fi
mkdir -p $outdir
cat > $workdir/bak_web.sh <<EOF
#!/bin/sh
dayofweek=\`date "+%u"\`
DATE=\`date +%Y_%m_%d\`
TAR_FILE="\$DATE.tar.gz"
ZIP_FILE="web_$ip""_\$DATE.zip"
cd $outdir
rm -rf $outdir/tmp
if  [ ! -f "\$ZIP_FILE" ]; then
if [ \$dayofweek -eq 1 ]; then
    rm -rf snapshot
fi
    tar -g snapshot -zcPf "\$TAR_FILE" $wwwdir
    zip -P $zippws -r \$ZIP_FILE \$TAR_FILE
rm -rf \$TAR_FILE
mkdir tmp
cd tmp
split -b 5m -a 3 -d $outdir/\$ZIP_FILE \$ZIP_FILE.part
for file in *
do
echo "\$ZIP_FILE" | mutt -s \$ZIP_FILE $mailsend -a \$file
sleep 30s
done
    fi
cd $outdir
rm -rf $outdir/tmp
find ./ -name "web*" -type f -mtime +5 -exec rm {} \;
EOF
 
chmod 700 $workdir/bak_web.sh
chkconfig --level 35 crond on
echo "6 4 * * * root $workdir/bak_web.sh" >>/etc/crontab
service crond restart
cd $workdir
$workdir/bak_web.sh
  1. 2011年3月31日11:37

    深圳打折网学习完,留个脚印!

    Internet Explorer 8.0 Windows XP
  2. 2011年3月31日15:52

    好文章,适合有linux服务器使用经经验者,一般的站长可能接触的不多

    Maxthon 3.0 Windows XP

返回顶部