#!/usr/local/bin/bash # mirrorpg-functions Version 2.7 2017-01-27 17:00 Author: Sergej Qkowlew # Functions and constants Функции и константы # This file is Public Doimain Я передаю это в общественное достояние for i in config functions generate allsites 1site abandoned childs do [ ! -s ./mirrorpg-$i ] && wget http://mirrorpg.arda.ru/mirrorpg/mirrorpg-$i [ ! -s ./mirrorpg-$i ] && echo "No file mirrorpg-$i here. Download it from http://mirrorpg.arda.ru/mirrorpg/$i" [ ! -s ./mirrorpg-$i ] && exit done # =================================== ====================================== # BEGIN FUNCTIONS DECLARATIONS НАЧАЛО ОПРЕДЕЛЕНИЙ ФУНКЦИЙ # =================================== ====================================== # First function for any site config. Первая фнукция для всякого конфига. # Build technical lines from domain. Строим технические строки из домена # This variables are used in other Сформированные перемеменные исполь- # functions зуются остальными функциями. # NowDomain NowDomReg NowDoMy NowDoOld SqBuildLines() { [ -z $1 ] && echo "Error. SqBuildLines must be called with domain name" [ -z $1 ] && exit NowDomain=$1 NowDomR=`echo "$NowDomain" | perl -pe "~s/\./\\\\\\./gi"` NowDomReg="~s/http:\/\/$NowDomR//gi" NowDoMy= NowDoOld= [ ! -z $SqRuleMyHall ] && NowDoMy=`echo "$NowDomain" | perl -pe $SqRuleMyHall` [ ! -z $SqRuleOldHall ] && NowDoOld=`echo "$NowDomain" | perl -pe $SqRuleOldHall` NowTitle= } # =================================== ====================================== # makes tmp subdir and symlink to it. Делает подкаталог для домена и симлинк SqMakeDirDom() { [ -z $1 ] && [ -z $NowDomain ] && return [ -z $1 ] && SqMakeDirDom $NowDomain [ -z $1 ] && return rm -rf /tmp/mirrorpg/$1 mkdir -p /tmp/mirrorpg/$1 [ -h $1 ] && rm $1 ln -s /tmp/mirrorpg/$1/ $1 } # =================================== ====================================== # add VirtualHost to webserver config Добавляет виртуальный хост в конфиг # if assumed directory doesn't exist. если нет каталога под него. # SqAddConf() { [ -z $1 ] && [ -z $NowDomain ] && return [ -z $1 ] && SqAddConf $SqUser $NowDomain $NowDoMy $NowDoOld [ -z $1 ] && return [ -d ${SqBasePath}/$NowDomain${SqHtmlDir} ] && return [ -x $SqAddHost ] && . $SqAddHost $1 $2 $3 $4 [ -d ${SqBasePath}/$NowDomain${SqHtmlDir} ] && return echo "$1 Warning: Directory for copying this host does not exist." } # =================================== ======================================= # Work with CSV/HTM files in MyHall Обработка CSV/HTM файлов # ----------------------------------- --------------------------------------- SqUpdateChilds() { if [ ! -z $SqBasePath ] && [ -r mirrorpg-childs.txt ] ; then cp mirrorpg-childs.txt $SqBasePath/mirrorpg.arda.ru/$SqHtmlDir/ cat mirrorpg-childs.txt | awk "(\$0 != \"$SqMyHall\") && (/\./) { print; }" | awk ' BEGIN { print "
Name Last modified Size md5">>index.html echo "AddType text/plain .md5">.htaccess echo "" cd $SqRetPath echo "`du -hs ${SqBasePath}/mirrorpg.arda.ru/${SqHtmlDir}/ | cut -f1`" >mirrorpg-size.csv } SqDlTarGzMd5() { [ -z $3 ] && return echo "===================================== ====================================== old .tar.gz archives downloading... Скачивание .tar.gz архивов" SqRetPath=`pwd` cd .$1 #echo "SqDlTarGzMd5 $1 $2 $3" wget --restrict-file-names=nocontrol $SqReferer http://$2/$3/ -O index.html wget --restrict-file-names=nocontrol -nc -np -nd -nH --base=http://$2/$3/ -F -i index.html rm index.html echo "===================================== ======================================" echo "testing md5 sums of downloaded files тестирование md5 скачанного" for j in *.tar.gz; do [ -s $j ] || continue [ -s $j.md5 ] || continue # echo -n "$j" [ "`md5 -q $j`" != "`cat $j.md5`" ] && rm $j $j.md5 && echo -n '#' [ -s $j ] && echo -n '+' done echo '' cd $SqRetPath SqUpdateIndex $1 } # =================================== ======================================= # Moves Previous .tar.gz from Latest Передвигает .tar.gz из Latest в Prevous # Packs Latest .tar.gz from fresh Создаёт Latest архив .tar.gz из свежего # If First .tar.gz not exist ln it Линкует архив на First если того нету # If site exists in webserver struc- Если сайт есть в структуре вебсервера, # ture, replaces it with fresh copy Заменяет его свежей копией # Makes .md5 files Создаёт файлы .md5 SqReplace() { [ -z $1 ] && [ -z $NowDomain ] && return [ -z $1 ] && SqReplace $NowDomain [ -z $1 ] && return if [ -z $2 ] ; then [ -d .Previous ] || [ -h .Previous ] && [ -r .Latest/$NowDomain.tar.gz ] && mv .Latest/$NowDomain.tar.gz* .Previous/ SqRetPath=`pwd` cd $NowDomain tar -czf ${SqRetPath}/.Latest/$NowDomain.tar.gz ./ md5 -q ${SqRetPath}/.Latest/$NowDomain.tar.gz >${SqRetPath}/.Latest/$NowDomain.tar.gz.md5 cd ${SqRetPath} [ ! -e .First/$NowDomain.tar.gz ] && cp -l .Latest/$NowDomain.tar.gz* .First/ fi [ -z $SqBasePath ] && return [ ! -d ${SqBasePath}/$1${SqHtmlDir} ] && return rm -rf ${SqBasePath}/$1${SqHtmlDir}/* 1>&2 2>/dev/null rm -rf ${SqBasePath}/$1${SqHtmlDir}/.* 1>&2 2>/dev/null cp -R $1/ ${SqBasePath}/$1${SqHtmlDir}/ [ `whoami` == 'root' ] && [ ! -z $SqUser ] && [ ! -z $SqGroup ] && chown -R $SqUser:$SqGroup ${SqBasePath}/$1${SqHtmlDir}/ 1>&2 2>/dev/null } # =================================== ====================================== # Creates .tar.gz copy of host in .$1 Создаёт .tar.gz архив в подкаталоге .$1 # from VIRTUALHOST content из содержимого виртуал хоста. SqCreateTarGz() { [ -z ${NowDomain} ] && return [ -z $1 ] && return [ -s .$1/$NowDomain.tar.gz ] && return [ -z $SqBasePath ] && return RetPath=`pwd` cd ${SqBasePath}/${NowDomain}${SqHtmlDir}/ tar -czf $RetPath/.$1/$NowDomain.tar.gz ./ md5 -q $RetPath/.$1/$NowDomain.tar.gz >$RetPath/.$1/$NowDomain.tar.gz.md5 cd $RetPath } # =================================== ====================================== # Replaces .tar.gz copy of host in .$1 Заменяет tar.gz архив в .$1 # from VIRTUALHOST content из содержимого виртуал хоста. SqReplaceTarGz() { [ -z ${NowDomain} ] && return [ -z $1 ] && return [ -z $SqBasePath ] && return [ -d ${SqBasePath}/${NowDomain}${SqHtmlDir} ] || return [ -s .$1/$NowDomain.tar.gz ] && rm .$1/$NowDomain.tar.gz RetPath=`pwd` cd ${SqBasePath}/${NowDomain}${SqHtmlDir}/ tar -czf $RetPath/.$1/$NowDomain.tar.gz ./ md5 -q $RetPath/.$1/$NowDomain.tar.gz >$RetPath/.$1/$NowDomain.tar.gz.md5 cd $RetPath } # =================================== ====================================== # Replaces real site with content of Заменяет реальный сайт распакованным # .tar.gz unpacking from .$1 .tar.gz архивом из каталога .$1 SqExtract() { echo " Debug \$NowDomain=\"${NowDomain}\" \$1=\"$1\" -C ${SqBasePath}/${NowDomain}${SqHtmlDir} " [ -z "${NowDomain}" ] && return [ -z "$1" ] && return [ -s ".$1/$NowDomain.tar.gz" ] || return [ -z "$SqBasePath" ] && return [ ! -d "${SqBasePath}/${NowDomain}${SqHtmlDir}" ] && return rm -rf ${SqBasePath}/${NowDomain}${SqHtmlDir}/* 1>&2 2>/dev/null rm -rf ${SqBasePath}/${NowDomain}${SqHtmlDir}/.??* 1>&2 2>/dev/null cat .$1/$NowDomain.tar.gz | gzip -d | tar -xf- -C ${SqBasePath}/${NowDomain}${SqHtmlDir} } # =================================== ====================================== # Create subdirectory in virthost Создаёт подкаталог в вирт хосте и # and .symlink in current dir симлинк на него в текущем каталоге # One of Childs First Previous Latest Один из каталогов SqCreate() { [ -z $1 ] && return if [ -d ${SqBasePath}/${NowDomain}/${SqHtmlDir} ] then mkdir -p ${SqBasePath}/${NowDomain}/${SqHtmlDir}/$1 [ ! -h .$1 ] && [ ! -d .$1 ] && [ ! -r .$1 ] && ln -s ${SqBasePath}/${NowDomain}/${SqHtmlDir}/$1/ .$1 fi } # ----------------------------------- -------------------------------------- # copy static site w/o changes to копирует статический сайт как он есть # directory named by it's domain в каталог именованный как домен сайта. # If second parameter exists - get Если есть второй параметр - то контент # content from that domain, not $1 берём с него, а не с $1 # Delete old content in subdir Старое содержимое стирается # $3 - additional params for wget $3 - доп параметры wget SqBackStatic() { [ -z $1 ] && [ -z $NowDomain ] && return [ -z $1 ] && SqBackStatic $NowDomain [ -z $1 ] && return SqMakeDirDom $1 cd $1 [ -z $2 ] && wget --restrict-file-names=nocontrol -r -l 0 -x -nH $3 http://$1/ [ -z $2 ] || wget --restrict-file-names=nocontrol -r -l 0 -x -nH $3 http://$2/ cd .. } # ----------------------------------- -------------------------------------- # Download one page as is in cur dir Скачивание одной страницы как есть в . # $1 - URI - полный URI в кавычках # $2 - alternate domain - альтернативный домен для скачивания # $3 - additional params for wget $3 - доп параметры wget Sq1Page() { [ -z $1 ] && return [ -z $2 ] && wget --restrict-file-names=nocontrol -x -nH $3 http://$NowDomain/`echo $1 | sed 's/"//g'` [ -z $2 ] || wget --restrict-file-names=nocontrol -x -nH $3 http://$2/`echo $1 | sed 's/"//g'` } # ----------------------------------- -------------------------------------- # Deletes all outside .. Удаляет всё за пределами тегов HTML SqCleanOuterHTMLTree() { for i in * do [ -d "$i" ] || continue echo -n "$i/ " cd $i SqCleanOuterHTMLTree cd .. done for i in *.htm* *.shtml* *.php* do [ -s $i ] || continue echo -n "$i " cat $i | perl -e ' $fulltext=join("",<>); $fulltext=~s/\r\n/\n/gsi; $fulltext=~s/.*?(.*?|)(
">index.html SqSumSizes=0 SqNumFiles=0 for i in *.tar.gz do [ -s $i ] || continue echo -n '.' SqSumSizes="$(( $SqSumSizes + `stat $i | cut -f8 -d\ ` ))" SqNumFiles="$(( $SqNumFiles + 1 ))" ls -lhT $i | perl -nae ' $lengthanchor=index($F[9],".tar.gz"); if ($lengthacnchor > 42) { $lengthanchor = 42; $lpitch=0; } else { $lpitch = 42-$lengthanchor; } $anchor=substr($F[9],0,$lengthanchor); printf ("%${lpitch}.${lpitch}s%s %s-%s-%s %s %5.5s", "", $F[9], $anchor, $F[8], $F[6], $F[5], $F[7], $F[4]); '>>index.html [ -z $2 ] && md5 -q $i > $i.md5 [ ! -z $2 ] && [ ! -s $i.md5 ] && md5 -q $i > $i.md5 [ -s $i.md5 ] && echo -n " `cat $i.md5 | cut -c 1-3`...`rev $i.md5 | cut -c 1-3 | rev`">>index.html echo "">>index.html done echo "`du -h | cut -f1` $SqSumSizes $SqNumFiles $SqVersion" | perl -nae ' printf ("%23.23s bytes in %6.6s files Summary size:%5.5s\n", $F[1], $F[2], $F[0]); printf ("By mirrorpg verion %3.3s ", $F[3]); '>>index.html date>>index.html echo "