LINUX.ORG.RU

покритикуйте bash крипт для бэкапа


1

0

backup:/s0/backup# cat backup.sh
#!/bin/bash


TIMESTAMP=`date +%F_%R_%s | sed s/:/=/`
ARCH="-j"  					# bz2 or gz
VERB=""

help_usage ()
{
echo -e "Usage: $0 [jzblixvh]  
	-j - Use bzip2 for archive backup [default] 
	-z - Use gzip for archive backup 
	-t <TYPE_OF_BACKUP> - Define type of backup inc - incremental or full - for full backup
	-f <CONFIG_FILE> - Define file which contains configuration
	-b <BACKUP NAME> - name backup (use \"default\" for $BACKUP_NAME)
	-v - verbose [non default]
	-h - print this help"
}

if [[ $# -eq 0 ]]
then
	help_usage
	exit 0
fi

while getopts ":jzb:vhd:x:l:f:t:" Option
do
  case $Option in
	j )
	     ARCH="-j"
	     ;;
	z )
	     ARCH="-z"
	     ;;
	d )
	     INSTALL_DIR="${OPTARG}"
	     ;;
	t )
	     TYPE_OF_BACKUP="${OPTARG}"
	     if [[ ${TYPE_OF_BACKUP} = "inc" || ${TYPE_OF_BACKUP} = "full" ]]
	     then :
	     else
	     	echo "Use -t only for \"full\" or \"inc\" type of backup"
		exit 0
	     fi
	     ;;
	b )
		 if [[ $OPTARG != "default" ]]
		 then
	     	BACKUP_NAME="${OPTARG}" 
		 fi
	     ;;
	l )
	     BACKUP_LIST="${OPTARG}"
	     ;;
	x )
	     EXCLUDE_LIST="${OPTARG}"
	     ;;
	f )
		 CONF_FILE="${OPTARG}"
		 if [[ -e "${CONF_FILE}" ]]
		 then 	
		 	. "${CONF_FILE}"	
		 else
		 	echo "Can't find ${CONF_FILE}. Used valid"
		 fi
		 ;;
	v )
	     VERB="-v"
	     ;;
	h )
	 	 help_usage
	     exit 0
	     ;;
	* )
		 help_usage
		 exit 0
		;;
  esac
done
shift $(($OPTIND - 1))

if [[ ! -e ${BACKUP_LIST} ]]
then
	echo "Edit conf file to to define list of backup files"
	exit 0;
fi

if [[ ! -e ${EXCLUDE_LIST} ]]
then
	echo "Edit conf file to to define list of exclude files"
	exit 0;
fi

remote_hostname=$(ssh $SSH_CONN_OPT $REMOTE_USER@$REMOTE_HOST hostname)
if [[ "$TYPE_OF_BACKUP" == "inc" ]]
then
	DATE_TIME=$(ls -1 -t ${LOCAL_DIR}/backup* | head -n 1 | grep -E -o "[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}=[0-9]{2}")
	INC_TIME=$(echo ${DATE_TIME} | grep -E -o "[0-9]{4}-[0-9]{2}-[0-9]{2}")
	INC_TIME="${INC_TIME} $(echo ${DATE_TIME} | grep -E -o '[0-9]{2}=[0-9]{2}' | sed s/\=/:/)"
	BACKUP_NAME="backup_${remote_hostname}_${TYPE_OF_BACKUP}_${TIMESTAMP}"
;
	ADV_OPT="--newer-mtime '${INC_TIME}'"
else
	BACKUP_NAME="backup_${remote_hostname}_${TYPE_OF_BACKUP}_${TIMESTAMP}"
;
fi

if [[ "${ARCH}" == "-j" ]]
then
	BACKUP_NAME="${BACKUP_NAME}.tar.bz2"
else
	BACKUP_NAME="${BACKUP_NAME}.tar.gz"
fi

COMM="tar --create --ignore-failed-read ${ARCH} \
	    ${VERB} --file="${REMOTE_DIR}/${BACKUP_NAME}" `cat ${BACKUP_LIST}` \
	    $(for entity in `cat ${EXCLUDE_LIST}`; do echo '--exclude' ${entity}; done) \
	    --exclude ${REMOTE_DIR} ${ADV_OPT}" 

echo "Start make ${TYPE_OF_BACKUP} backup to ${BACKUP_NAME}"

echo "use ${ARCH} to archive"
ssh ${SSH_CONN_OPT} ${REMOTE_USER}@${REMOTE_HOST} ${COMM}
echo "End backup"

echo "Start copy backup to local machine"
scp ${SCP_CONN_OPT} ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/${BACKUP_NAME} ${LOCAL_DIR}
echo "End copy"

echo "Begin delete remote copy"
COMM="rm -f ${REMOTE_DIR}/${BACKUP_NAME}"
ssh ${SSH_CONN_OPT} ${REMOTE_USER}@${REMOTE_HOST} ${COMM}
echo "End delete"

echo "Delete obsolete"
find $LOCAL_DIR -mtime "+$EXPIRE_TIME" -name "backup.*.tar.*" -exec rm -f {} \;
echo "End delete"

backup:/s0/backup# cat serv/serv.conf 
#
# Config file for backup serv server
#

REMOTE_HOST="192.168.0.1"
REMOTE_USER="root"
BACKUP_LIST="/s0/backup/serv/list.backup"
EXCLUDE_LIST="/s0/backup/serv/list.exclude"
REMOTE_DIR="/bk"
LOCAL_DIR="/s0/backup/serv"
EXPIRE_TIME="7" 	#in days
TYPE_OF_BACKUP="full"

SSH_CONN_OPT="-p 22"
SCP_CONN_OPT="-P 22"


★★

Re: покритикуйте bash крипт для бэкапа

--- script_orig.txt     Tue Dec 13 14:20:17 2005
+++ script.txt  Tue Dec 13 14:20:17 2005
@@ -1,7 +1,7 @@
 #!/bin/bash


-TIMESTAMP=`date +%F_%R_%s | sed s/:/=/`
+TIMESTAMP=`date '+%F_%H=%M_%s'`
 ARCH="-j"                                      # bz2 or gz
 VERB=""

@@ -28,9 +28,11 @@
   case $Option in
        j )
             ARCH="-j"
+            SUFFIX='tar.bz2'
             ;;
        z )
             ARCH="-z"
+            SUFFIX='tar.gz'
             ;;
        d )
             INSTALL_DIR="${OPTARG}"
@@ -95,9 +97,9 @@
 remote_hostname=$(ssh $SSH_CONN_OPT $REMOTE_USER@$REMOTE_HOST hostname)
 if [[ "$TYPE_OF_BACKUP" == "inc" ]]
 then
-       DATE_TIME=$(ls -1 -t ${LOCAL_DIR}/backup* | head -n 1 | grep -E -o "[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}=[0-9]{2}")
-       INC_TIME=$(echo ${DATE_TIME} | grep -E -o "[0-9]{4}-[0-9]{2}-[0-9]{2}")
-       INC_TIME="${INC_TIME} $(echo ${DATE_TIME} | grep -E -o '[0-9]{2}=[0-9]{2}' | sed s/\=/:/)"
+       DATE_TIME=$(ls -1 -t ${LOCAL_DIR}/backup* | grep -m 1 -E -o "[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}=[0-9]{2}")
+       INC_TIME=$(echo ${DATE_TIME} | cut -d'_' -f1)
+       INC_TIME="${INC_TIME} $(echo ${DATE_TIME} | cut -d'_' -f2 | sed s/\=/:/)"
        BACKUP_NAME="backup_${remote_hostname}_${TYPE_OF_BACKUP}_${TIMESTAMP}"

 ;
        ADV_OPT="--newer-mtime '${INC_TIME}'"
@@ -106,33 +108,18 @@
 ;
 fi

-if [[ "${ARCH}" == "-j" ]]
-then
-       BACKUP_NAME="${BACKUP_NAME}.tar.bz2"
-else
-       BACKUP_NAME="${BACKUP_NAME}.tar.gz"
-fi
+BACKUP_NAME="${BACKUP_NAME}.$SUFFIX"

-COMM="tar --create --ignore-failed-read ${ARCH} \
-           ${VERB} --file="${REMOTE_DIR}/${BACKUP_NAME}" `cat ${BACKUP_LIST}` \
-           $(for entity in `cat ${EXCLUDE_LIST}`; do echo '--exclude' ${entity}; done) \
+COMM="tar --create --ignore-failed-read ${ARCH} ${VERB} \
+           -f - \
+           --files-from=${BACKUP_LIST} \
+           --exclude-from ${EXCLUDE_LIST} \
            --exclude ${REMOTE_DIR} ${ADV_OPT}"

 echo "Start make ${TYPE_OF_BACKUP} backup to ${BACKUP_NAME}"

-echo "use ${ARCH} to archive"
-ssh ${SSH_CONN_OPT} ${REMOTE_USER}@${REMOTE_HOST} ${COMM}
-echo "End backup"
+ssh ${SSH_CONN_OPT} ${REMOTE_USER}@${REMOTE_HOST} ${COMM} > ${LOCAL_DIR}/${BACKUP_NAME}

-echo "Start copy backup to local machine"
-scp ${SCP_CONN_OPT} ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/${BACKUP_NAME} ${LOCAL_DIR}
-echo "End copy"
-
-echo "Begin delete remote copy"
-COMM="rm -f ${REMOTE_DIR}/${BACKUP_NAME}"
-ssh ${SSH_CONN_OPT} ${REMOTE_USER}@${REMOTE_HOST} ${COMM}
-echo "End delete"
-
 echo "Delete obsolete"
-find $LOCAL_DIR -mtime "+$EXPIRE_TIME" -name "backup.*.tar.*" -exec rm -f {} \;
-echo "End delete"
+find $LOCAL_DIR -mtime "+$EXPIRE_TIME" -name 'backup.*.tar.*' -print0 | xargs -0 rm -f
+echo "End delete"

sdio ★★★★★ ()
Ответ на: Re: покритикуйте bash крипт для бэкапа от sdio

Re: покритикуйте bash крипт для бэкапа

COMM="tar --create --ignore-failed-read ${ARCH} ${VERB} \
+           -f - \
+           --files-from=${BACKUP_LIST} \
+           --exclude-from ${EXCLUDE_LIST} \
            --exclude ${REMOTE_DIR} ${ADV_OPT}"

imho: он эти файлы будет искать на удаленной машине и не найдет.
файлы хранятся на локальной машине. т.е. без for'а не обойтись.

tugrik ★★ ()
Ответ на: Re: покритикуйте bash крипт для бэкапа от sdio

Re: покритикуйте bash крипт для бэкапа

Интересно это можно считать тестом???
backup:/s0/backup/test# cat test.sh 
#!/bin/bash

EXCLUDE_LIST=$1

time VAR=$(sed 's/.*/--exclude &/' ${EXCLUDE_LIST})
time VAR=$(for entity in `cat ${EXCLUDE_LIST}`; do echo '--exclude' ${entity}; done)


backup:/s0/backup/test# wc -l list.exclude 
33992 list.exclude

backup:/s0/backup/test# ./test.sh list.exclude       
real	0m0.324s
user	0m0.111s
sys	0m0.031s

real	0m2.265s
user	0m1.091s
sys	0m0.277s

tugrik ★★ ()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.