#!/bin/bash # version 2011-09-20 OPT_VERBOSE='' OPT_REV='' OPT_PACK='' OPT_DATABASE='' OPT_USERFILES='' OPT_NOCOMPRESS='' OPT_REPOPATH='' SOURCE_REV='' CL_DEFAULT='\E[0m' CL_BLACK='\E[30m' CL_RED='\E[31m' CL_GREEN='\E[32m' CL_YELLOW='\E[33m' CL_BLUE='\E[34m' CL_WHILE='\E[37m' CL_HINT='\e[4;32m' CL_COMMAND='\e[4;35m' SCRIPT_DIR=$(dirname $0) CURRENT_DIR=$(pwd) cd $SCRIPT_DIR . _config.sh cd $CURRENT_DIR # $message $color cecho () { local message=${1:-''} local color=${2:-$CL_WHILE} echo -ne "$color" echo -ne "$message" echo -ne $CL_DEFAULT return } # str charlist trim() { local str=${1:-''} local charlist=${2:-' '} str=${str%%$charlist} str=${str##$charlist} echo $str } # Outpus text using "cecho" only if OPT_VERBOSE turned on cverbose() { if [[ -n "$OPT_VERBOSE" ]] ; then cecho "$@" fi } # $command cmd() { local debug=0 local exec=1 local command=$(trim "$1") if [[ 1 == $debug ]] ; then cecho "${command}\n" $CL_COMMAND fi if [[ 1 == $exec ]] ; then dir=$(pwd) eval $command if [[ "$?" != "0" ]] ; then cecho "Path: $dir\n" $CL_RED cecho "Failed executing command\n" $CL_RED if [[ 1 == $debug ]] ; then cecho "${command}\n" $CL_RED fi exit fi fi } doHelp() { echo "Usage: $(basename $0) [options...] [RepositoryPath]" echo "RepositoryPath relative path only inside TRUNK ! ex: trunk/site" echo "-r [--revision] ARG : ARG (some commands also take ARG1:ARG2 range)" echo " A revision argument can be one of:" echo " NUMBER revision number" echo " '{' DATE '}' revision at start of the date" echo " 'HEAD' latest in repository" echo " 'BASE' base rev of item's working copy" echo " 'COMMITTED' last commit at or before BASE" echo " 'PREV' revision just before COMMITTED" echo "-p [--pack] : Pack scripts and stylesheets" echo "-d [--database] : Include database dump" echo "-f [--userfiles] : Include userfiles" echo "-n [--no-compress] : Do not compress exported files" echo "--full : Same as -d -p -f" echo "-v : Explain what is being done" echo "--help : Show this help" echo } doProcessArgs() { for opt in $@ ; do case ${opt} in ( '--help' ) shift doHelp exit ;; ( '--full' ) shift OPT_REV='' OPT_DATABASE='y' OPT_USERFILES='y' OPT_PACK='y' ;; ( '-r' ) shift OPT_REV=$1 shift ;; ( '-p' ) shift OPT_PACK='y' ;; ( '-d' ) shift OPT_DATABASE='y' ;; ( '-f' ) shift OPT_USERFILES='y' ;; ( '-v') shift OPT_VERBOSE='y' ;; ( '-n' ) shift OPT_NOCOMPRESS='y' ;; esac done OPT_REPOPATH=$@ if [[ -n $OPT_REPOPATH ]] ; then PATH_PARAM="/$(trim ${OPT_REPOPATH} '/')" else PATH_PARAM="/trunk" fi FULL_REPO_URL="${REPOSITORY_URL}/${PROJECT_NAME}${PATH_PARAM}" # geting last source revision (according to specified parameter OPT_REV) local revParam='' if [[ -n $OPT_REV ]] ; then # get second part of revisoons range revParam=" -r ${OPT_REV#*:}" fi SOURCE_REV=$(svn info${revParam} ${FULL_REPO_URL} --non-interactive 2>&1 | grep "Last Changed Rev:" | awk '{print $4}') local lastRev="_r${SOURCE_REV}" EXPORT_DIR="export${lastRev}_$(date +%Y-%m-%d)" EXPORT_PATH="${PROJECT_PATH}/${EXPORT_DIR}" FULL_EXPORT_PATH="${EXPORT_PATH}${PATH_PARAM}" FILES_ARCHIVE="${FILES_DIRECTORY}.tar.bz2" if [[ -z $SOURCE_REV ]] ; then cecho "Error getting source revision\n" $CL_RED exit fi } doShowJobsInfo() { cecho '========================================\n' $CL_YELLOW local msgRev='(ALL)' if [[ -n $OPT_REV ]] ; then msgRev=$OPT_REV fi cecho "Export revisions: ${msgRev}\n" $CL_YELLOW local msgMerge='NO' if [[ 'y' == $OPT_PACK ]] ; then msgMerge='YES' fi cecho "Merge resources: ${msgMerge}\n" $CL_YELLOW local msgDatabase='NO' if [[ 'y' == $OPT_DATABASE ]] ; then msgDatabase='YES' fi cecho "Export database: ${msgDatabase}\n" $CL_YELLOW local msgUserFiles='NO' if [[ 'y' == $OPT_USERFILES ]] ; then msgUserFiles='YES' fi cecho "Export userfiles: ${msgUserFiles}\n" $CL_YELLOW local msgCompress='YES' if [[ 'y' == $OPT_NOCOMPRESS ]] ; then msgCompress='NO' fi cecho "Compress exported files: ${msgCompress}\n" $CL_YELLOW echo cecho "Source revision: ${SOURCE_REV}\n" $CL_YELLOW cecho "Repository path: ${OPT_REPOPATH}\n" $CL_YELLOW cecho "Destination path: ${EXPORT_PATH}\n" $CL_YELLOW cecho '========================================\n' $CL_YELLOW cecho "Press Enter to continue or Ctrl+Break to stop the script" $CL_GREEN read dummy } doPrepare() { cecho "Creating export dir...\n" $CL_HINT if [[ -a $EXPORT_PATH ]] ; then cmd "rm -Rf ${EXPORT_PATH}" fi cmd "mkdir -p ${FULL_EXPORT_PATH}" cverbose "${FULL_EXPORT_PATH}\n" $CL_WHILE } doSvnExport() { cecho "Exporting files from svn...\n" $CL_HINT local verboseParam=' -q' if [[ 'y' == $OPT_VERBOSE ]] ; then verboseParam='' fi if [[ -z $OPT_REV ]] ; then #export using svn export cmd "svn export ${FULL_REPO_URL} --force${verboseParam} ${FULL_EXPORT_PATH}" else #export files specified by revisions local svnPath local file local dir local fullRepoUrl="${FULL_REPO_URL}/" local revParam=" -r ${SOURCE_REV}" local diffCmd="svn diff --summarize -r ${OPT_REV} ${FULL_REPO_URL}" local list=$($diffCmd | egrep '[M|A] ' | awk '{print $2};') for svnPath in $list ; do file=${svnPath/"$fullRepoUrl"/''} file="${FULL_EXPORT_PATH}/${file}" dir=$(dirname ${file}) if [[ ! -d $dir ]] ; then cmd "mkdir -p ${dir}" fi if [[ ! -d $file ]] ; then cverbose "${file}\n" $CL_WHITE cmd "svn cat${revParam} ${svnPath} > ${file}" fi done fi #creating rev file with exported revision cmd "echo -n ${SOURCE_REV} > ${EXPORT_PATH}/rev" } doPackResources() { if [[ 'y' != $OPT_PACK ]] ; then return fi cecho "Packing resources...\n" $CL_HINT doPackCss() { local path=$1 list=$(find ${path} -name '*.css' -type f | grep -v pack | grep -v merge) for f in $(echo -e $list); do ext=$(echo $f | sed 's/.*\.//') o=$(echo $f | sed "s/\.$ext$/\.pack\.$ext/") cverbose "$f -> $o\n" $CL_WHITE java -jar $BASE_PATH/site/bin/yuicompressor.jar $f -o $o done; } doPackJs() { local path=$1 list=$(find ${path} -name '*.js' -type f | grep -v pack | grep -v merge | grep -v './js/ckeditor/') for f in $(echo -e $list); do ext=$(echo $f | sed 's/.*\.//') o=$(echo $f | sed "s/\.$ext$/\.pack\.$ext/") cverbose "$f -> $o\n" $CL_WHITE java -jar $BASE_PATH/site/bin/yuicompressor.jar $f -o $o done; } cmd "cd ${EXPORT_PATH}" local cssDirectories cssDir jsDirectories jsDir cssDirectories=$(find ./ -maxdepth 5 -type d -path '*/www/css') for cssDir in $cssDirectories ; do doPackCss "$cssDir" done jsDirectories=$(find ./ -maxdepth 5 -type d -path '*/www/js') for jsDir in $jsDirectories ; do doPackJs "$jsDir" done echo -n cd $CURRENT_DIR } doExportDatabase() { if [[ 'y' != $OPT_DATABASE ]] ; then return fi cecho "Exporting database...\n" $CL_HINT cmd "mysqldump -Q --hex-blob -h ${DB_HOST} -u ${DB_USER} -P ${DB_PORT} -p${DB_PASS} ${DB_NAME} > ${EXPORT_PATH}/${DB_NAME}.sql" } doExportUserFiles() { if [[ 'y' != $OPT_USERFILES ]] ; then return fi cecho "Exporting userfiles...\n" $CL_HINT local filesDir="${PUB_BASE_PATH}/www/${FILES_DIRECTORY}" if [[ -d $filesDir ]] ; then cmd "cd ${PUB_BASE_PATH}/www" cmd "tar -cjf ${EXPORT_PATH}/${FILES_ARCHIVE} ${FILES_DIRECTORY}" else cecho "Wrong UserFiles directory \"${filesDir}\"" $CL_RED fi cd $CURRENT_DIR } doCompressExportedFiles() { if [[ -z $OPT_NOCOMPRESS ]] ; then cecho "Compressing exported files...\n" $CL_HINT cmd "cd ${EXPORT_PATH}" cmd "tar -cjf ${EXPORT_DIR}.tar.bz2 --remove-files --exclude=${FILES_ARCHIVE} *" cd $CURRENT_DIR fi } # ======================================== doProcessArgs $@ doShowJobsInfo doPrepare doSvnExport doPackResources doExportDatabase doExportUserFiles doCompressExportedFiles cecho "\nExported to: ${EXPORT_PATH}\n" $CL_HINT