最新消息:

推荐一款优秀的shell备份脚本

shell 大步 934浏览 0评论

Ps:不得不承认,电脑比人强的地方就是能做重复的事情而不会感到厌倦,还是用脚本备份实在呀

这个shell脚本是prima@hosting自带的备份web和mysql数据库等用到的, 可以说非常不错, 特别是站点稍多点的时候, 稍微修改一点就可以作为一个极其优秀的脚本,
我主要用它来备份数据库, 没时间做大修改, 如果感兴趣你可以修改下, 推荐哈

#!/bin/sh

EMAIL=0
###############################################################################
# 配置区
# the backup dir
BACKUP_BASE_DIR=”/www/backup”

# 备份哪些?
WEB=0
EMAIL=0
DNS=0
MYSQL=1
CONTROL=1

# 测试模式?
testmode=0

# 本参数如设置为1,则打包压缩/www/users/目录时候,
# 将会把每个站点单独生成一个对应的tgz压缩文件
# 本参数如果为0,则/www/users/下所有站点将打包成为一个users.tar.gz的大文件
# 为了便于下载备份和解压缩备份文件,本参数默认设置为1
user_seperate=1

# 本参数如设置为1,则打包压缩/var/lib/mysql/目录时候,
# 将会把每个数据库单独生成一个对应的tgz压缩文件
# 本参数如果为0,则/var/lib/mysql/目录打包成为一个mysql.tar.gz的大文件
# 本参数默认设置为0
mysql_seperate=1

# 保留多少份旧的备份?
# 即除了当前最新的这个备份之外,还保留几个备份,最少为0个,默认为1
# 如果设为0,则只保留一个备份,每次备份的时候都会把以前的备份删除
max_old_backup=7

##############################################################################
# 以下的配置如果不明白最好不要改
##############################################################################
# 备份数据目录
WEB_DATA_DIR=”/www/users”
EMAIL_DATA_DIR=”/var/vmail”
DNS_DATA_DIR=”/var/named”
CONTROL_DATA_DIR=”/usr/local/apache2/conf /etc/my.cnf /usr/local/Zend/etc/php.ini”
# CONTROL_DATA_DIR=”/www/prima”
# GENCOMSITE_DATA_DIR=”/www/prima/htdocs/apps/GenComSite/sitepage /www/prima/htdocs/apps/GenComSite/upload”
# COUNTER_DATA_DIR=”/var/Counter”
system=`uname -a`
case “$system” in
    Linux*)             MYSQL_DATA_DIR=”/var/lib/mysql” ;;
    FreeBSD*)           MYSQL_DATA_DIR=”/var/db/mysql” ;;
    *)                  MYSQL_DATA_DIR=”/var/lib/mysql” ;;
esac

# 备份配置目录/文件位置
PRIMA_ETC_DIR=”/usr/prima/etc”
PRIMA_CONF_FILE=”$PRIMA_ETC_DIR/prima.conf”
POSTFIX_CONF_DIR=”/usr/prima/postfix/conf”
FRONTPAGE_DIR=”/usr/local/frontpage”
NAMED_CONF=”/etc/named.conf”
RESIN_CONF=”/usr/local/resin/host.conf”
TOMCAT_CONF=”/var/tomcat4/conf/server.xml”
PHPMYADMIN_CONF=”/www/prima/htdocs/apps/phpMyAdmin/config.inc.php”

# tar 参数
TAR_FLAG=”zp”

###########################################################################
# 配置结束
# 下面的程序请不要改动
###########################################################################
# my functions
backup_list()
{
if [ -z "$1" ] || [ -z "$2" ] ; then
  echo “backup_list() usage: backup_list data_to_backup backup_dir”
  return 1
fi

list=$1
backup_dir=$2
for name in $list
do
  echo
  echo “do backup for $name”

  prefix=`dirname $name`
  self=`basename $name`
  if [ -z "$prefix" ] || [ -z "$self" ] ; then
    echo “Warning: can not fetch prefix/self for data $name, skip.”
    continue
  fi

  if [ -d "$name" ] ; then
    cd $prefix
    if [ "$testmode" = 1 ] ; then
      cmd=”tar $TAR_FLAG -c -f $backup_dir/$self.tar.gz $self”
      echo $cmd
    else
      tar $TAR_FLAG -c -f $backup_dir/$self.tar.gz $self
    fi
  else
    if [ -f "$name" ] ; then
      if [ "$testmode" = 1 ] ; then
        #cmd=”cp -p $name $backup_dir/$self”
    cmd=”tar $TAR_FLAG -c -f $backup_dir/$self.tar.gz $name”
        echo $cmd
      else
        #cp -p $name $backup_dir/$self
    tar $TAR_FLAG -c -f $backup_dir/$self.tar.gz $name 2> /dev/null
      fi
    else
      echo “Warning: data $name is not a dir or file, skip.”
    fi
  fi

done

return 0
}

# function to clean old backup
clean_old_backup()
{
count=0
for name in `ls $BACKUP_BASE_DIR|grep -E ‘[0-9]{6,6}-[0-9]{6,6}’ | sort -r`
do
  count=$((count+1))
  if [ $count -gt $max_old_backup ] ; then
    echo -n “Cleaning old backup: $name… “
    if [ "$testmode" = 1 ] ; then
      echo -n “rm -fr $BACKUP_BASE_DIR/$name”
    else
      if [ "$BACKUP_BASE_DIR/$name" = "/" ] ; then
        echo “No rm -fr /!”
        exit 1
      fi
      rm -fr $BACKUP_BASE_DIR/$name
    fi
    echo ” Done.”
  else
    echo “Keeping old backup: $name”
  fi
done
}

# main process

echo “prima backup util”
datestr=`date ‘+%Y%m%d-%H%M%S’`
echo “date: $datestr”
echo

# clean old backup
clean_old_backup

# fetch data and config to be backuped
data_to_backup=”"
user_to_backup=”"
mysql_to_backup=”"

if [ "$CONTROL" = "1" ] ; then
    data_to_backup=”$data_to_backup $CONTROL_DATA_DIR”
fi

if [ "$WEB" = "1" ] ; then
    if [ "$user_seperate" = "1" ] ; then
        user_dirs=`ls $WEB_DATA_DIR`
        for x in  $user_dirs;
        do
            x=”$WEB_DATA_DIR/$x”
            if [ -d $x ] ; then
                user_to_backup=”$user_to_backup $x”    
            fi
        done
    else
        data=`grep ^home_base $PRIMA_CONF_FILE | awk -F ‘=’ ‘{print $2}’ 2>/dev/null`
        if [ -z "$data" ] ; then
            data=$WEB_DATA_DIR
        fi
        data_to_backup=”$data_to_backup $data”
    fi
fi
if [ "$EMAIL" = "1" ] ; then
  
  data=`grep ^mail_space_base $PRIMA_CONF_FILE | awk -F ‘=’ ‘{print $2}’ 2>/dev/null`
    if [ -z "$data" ] ; then
        data=$EMAIL_DATA_DIR
    fi
    data_to_backup=”$data_to_backup $data”
fi
if [ "$DNS" = "1" ] ; then
    data=`grep ^zonefile_base $PRIMA_CONF_FILE | awk -F ‘=’ ‘{print $2}’ 2>/dev/null`
    if [ -z "$data" ] ; then
        data=$DNS_DATA_DIR
    fi
    data_to_backup=”$data_to_backup $data”
fi
if [ "$MYSQL" = "1" ] ; then
    if [ "$mysql_seperate" = "1" ] ; then
        mysql_dirs=`ls $MYSQL_DATA_DIR`
        for x in  $mysql_dirs;
        do
            x=”$MYSQL_DATA_DIR/$x”
            if [ -d $x ] ; then
                mysql_to_backup=”$mysql_to_backup $x”    
            fi
        done
    else
        data_to_backup=”$data_to_backup $MYSQL_DATA_DIR”
    fi
fi
data=`grep ^app_names $PRIMA_CONF_FILE | grep GenComSite 2>/dev/null`
if [ ! -z "$data" ] ; then
    data=$GENCOMSITE_DATA_DIR
fi
data_to_backup=”$data_to_backup $data”

data=`grep ^app_names $PRIMA_CONF_FILE | grep CounterConfig 2>/dev/null`
if [ ! -z "$data" ] ; then
    data=$COUNTER_DATA_DIR
fi
data_to_backup=”$data_to_backup $data”

echo
echo “data_to_backup: $data_to_backup”
echo
if [ "$user_seperate" = "1" ] ; then
    echo
    echo “user_to_backup: $user_to_backup”
    echo
fi
if [ "$mysql_seperate" = "1" ] ; then
    echo
    echo “mysql_to_backup: $mysql_to_backup”
    echo
fi

conf_to_backup=”"
if [ "$WEB" = "1" ] || [ "$EMAIL" = "1" ] || [ "$DNS" = "1" ] || [ "$MYSQL" = "1" ] ; then
    conf_to_backup=”$conf_to_backup $PRIMA_ETC_DIR”
fi
if [ "$WEB" = "1" ] ; then
    resin=`grep ^use_mod_caucho $PRIMA_CONF_FILE | awk -F ‘=’ ‘{print $2}’ 2>/dev/null`
    if [ -z "$resin" ] ; then
        conf_to_backup=”$conf_to_backup $RESIN_CONF”
    fi
    tomcat=`grep ^use_mod_jk $PRIMA_CONF_FILE | awk -F ‘=’ ‘{print $2}’ 2>/dev/null`
#    if [ -z "$tomcat" ] ; then
#        conf_to_backup=”$conf_to_backup $TOMCAT_CONF”
#    fi
fi
if [ "$EMAIL" = "1" ] ; then
    conf_to_backup=”$conf_to_backup $POSTFIX_CONF_DIR”
fi
if [ "$DNS" = "1" ] ; then
    data=`grep ^named_conf $PRIMA_CONF_FILE | awk -F ‘=’ ‘{print $2}’ 2>/dev/null`
    if [ -z "$data" ] ; then
        data=$NAMED_CONF
    fi
    conf_to_backup=”$conf_to_backup $data”
fi
if [ "$MYSQL" = "1" ] ; then
    conf_to_backup=”$conf_to_backup $PHPMYADMIN_CONF”
fi

echo
echo “conf_to_backup: $conf_to_backup”
echo

# create backup dir
if [ ! -d "$BACKUP_BASE_DIR" ] ; then
  echo “Warning: backup base dir $BACKUP_BASE_DIR does not exist.”
  echo “I will create one for you.”
fi

base_dir=”$BACKUP_BASE_DIR/$datestr”
mkdir -p -m 700 $base_dir
if [ $? != 0 ] ; then
  echo “Error: backup dir $base_dir create failed.”
  exit 1
fi

umask 066

# backup data
data_backup_dir=”$base_dir/data”
mkdir -p -m 700 $data_backup_dir

backup_list “$data_to_backup” $data_backup_dir

# backup conf
conf_backup_dir=”$base_dir/data/conf”
mkdir -p -m 700 $conf_backup_dir

backup_list “$conf_to_backup” $conf_backup_dir

#backup user_site
if [ "$user_seperate" = "1" ] ; then
user_backup_dir=”$base_dir/data/users”
mkdir -p -m 700 $user_backup_dir
backup_list “$user_to_backup” $user_backup_dir
fi

#backup mysql
if [ "$mysql_seperate" = "1" ] ; then
mysql_backup_dir=”$base_dir/data/mysql”
mkdir -p -m 700 $mysql_backup_dir
backup_list “$mysql_to_backup” $mysql_backup_dir
fi

转载请注明:大步's Blog » 推荐一款优秀的shell备份脚本

SiteMap