日期:2014-05-16  浏览次数:20553 次

suse-linux定时备份数据库和文件shell脚本
#!/bin/bash
#2009-11-8 第一次编写
#作者:duanyunxi
#版本:v1.1
#备份脚本

##设置备份##
echo "##开始设置备份!##"
if [ -f /etc/crontab ]; then
  RETVAL=`service cron status|grep  'running'`
  if [ "$RETVAL" != "" ]; then
    
       echo "请选择备份格式(s:数据库|t:tar.gz压缩包)?"
       read ENCODE

       #################数据库sql脚本备份###########
       if [ "$ENCODE" == "s" ] || [ "$ENCODE" == "S" ]; then
         ##提示输入mysql管理员用户root的密码##
RETVAL="1"
until [ $RETVAL -eq 0 ]
do
   echo "请输入mysql管理员用户root的密码"
   read MYSQLPASSWD
   mysqladmin -uroot -p$MYSQLPASSWD version 1> /dev/null 2>&1
   RETVAL=$?
   if [ $RETVAL -eq 0 ]; then
echo "mysql管理员root密码输入正确!"
   else
echo "mysql管理员密码错误,请重新输入!"
   fi
         done

##提示输入要备份的数据库名##
echo "请输入要备份数据库名,例如:icfjkcms"
         INPUT="n"
         until [ "$INPUT" == "Y" ] || [ "$INPUT" == "y" ]
         do
         read DATABASE
         echo "您输入的数据库名是:$DATABASE,请确认(y|n)?"
         read INPUT
          if [ "$INPUT" != "Y" ] && [ "$INPUT" != "y" ]; then
   echo "请输入要备份数据库名,例如:icfjkcms"
          else
   echo "您输入的备份数据库名为:$DATABASE"
          fi
         done


##提示输入备份频率##
echo "***定时备份频率格式:分 时  天 月 一周第几天"
echo "***例如:*/20 6-12 * 12 * 表示在12月内,每天的早上6点到12点中,每隔20分钟执行一次"
echo "请输入备份频率,例如:*/2 *  * * *"
         INPUT="n"
         until [ "$INPUT" == "Y" ] || [ "$INPUT" == "y" ]
         do
         read RATE
         echo "您输入的备份频率是:$RATE,请确认(y|n)?"
         read INPUT
          if [ "$INPUT" != "Y" ] && [ "$INPUT" != "y" ]; then
   echo "请输入备份频率,例如:*/2 *  * * *"
          else
   echo "您输入的备份频率为:$RATE"
          fi
         done




DAY=`date +%Y%m%d`
TIME=`date +%H%M%S`

###备份文件备份脚本存放目录###
         mkdir -p /var/backup/$DAY/sh

echo "生成备份的脚本文件"
         echo "#!/bin/bash" >> /var/backup/$DAY/sh/$DATABASE-SQL$TIME.sh
         echo "BACKDAY=\`date +%Y%m%d%H%M\`" >> /var/backup/$DAY/sh/$DATABASE-SQL$TIME.sh
         echo "mysqldump --default-character-set=utf8 -hlocalhost -uroot -p$MYSQLPASSWD -B -d --opt -q $DATABASE > /var/backup/$DAY/DDL$DATABASE-\$BACKDAY.sql ;" >> /var/backup/$DAY/sh/$DATABASE-SQL$TIME.sh
         echo "mysqldump --default-character-set=utf