backup-rcopy/hosts/scripts/svn_backup.sh

188 lines
5.2 KiB
Bash
Executable File

#!/usr/bin/env bash
## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
## -
## - assuming your remote user is called "back", you have
## - to give sudoer rights to him as follow:
## -
## - visudo (edits /etc/sudoers file)
## -
## - add line:
## - back ALL = (root)NOPASSWD:/usr/bin/rsync
## -
## - on the remote database allow the congigured mysql user to
## - access the database from localhost an give him the follwing
## - privileges:
## - Select_priv
## - Lock_tables_priv
## - Show_view_priv
## -
## - INSERT INTO user (Host,User,Password,Select_priv,Lock_tables_priv,Show_view_priv) VALUES('localhost','backup',password('backup'),'Y','Y','Y');
## - FLUSH PRIVILEGES;
## -
## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
err_Log=${LOCK_DIR}/svn.err.log
> $err_Log
## - load functions
## -
. $rcopy_functions_file
# --------------------------------------------------- #
# -------------------- Variable --------------------- #
#
_backupDestArchiveDir="${script_backup_dir}/Subversion"
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
svnadmin_bin=`$ssh ${ssh_user}@$srcHost which svnadmin`
find=`$ssh ${ssh_user}@$srcHost which find`
sudo=`$ssh ${ssh_user}@$srcHost which sudo`
realpath=`$ssh ${ssh_user}@$srcHost which realpath`
ssh_options="-o BatchMode=yes -o ConnectTimeout=360"
else
svnadmin_bin=`which svnadmin`
fi
if [ -z "$svnadmin_bin" ]; then
fatal "cannot find binary \"svnadmin\""
fi
#
# ------------------ Ende Variable ------------------ #
# --------------------------------------------------- #
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
_REPOSITORIES=`ssh ${ssh_user}@$srcHost "$sudo $find $svn_source_base_path -maxdepth 1 -type d -exec basename 'realpath {}' \;"`
else
_REPOSITORIES=`find $svn_source_base_path -maxdepth 1 -type d -exec basename 'realpath {}' \;`
fi
for repos in $_REPOSITORIES ; do
_dir=`basename $svn_source_base_path`
[ "$repos" = "$_dir" ] && continue
REPOSITORIES="$REPOSITORIES $repos"
done
echolog ""
if $ARCHIVE ;then
if [ "$_TEST" = "1" ];then
progArgs="-n $progArgs --stats"
elif [ "$_DEBUG" -gt 0 ];then
progArgs="$progArgs --stats"
if [ "$_DEBUG" -gt 1 ];then
formats="%t -- %o %f %b Bytes[%l]"
fi
fi
## - backup all svn repositories.
## -
for i in $REPOSITORIES ; do
filedate=`$date +"%Y-%m-%d-%H%M"`
if [ "$destHost" = "localhost" ]; then
if [ ! -d ${_backupDestArchiveDir}/${i} ] ; then
$mkdir -p ${_backupDestArchiveDir}/${i}
fi
fi
## - begin timestamp
## -
b_timestamp=`$date +"%s"`
info_msg="save svn repository $i.."
echononl "\t$info_msg"
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
$(
$ssh $ssh_options ${ssh_user}@$srcHost "$sudo $svnadmin_bin dump $svn_source_base_path/$i 2> /dev/null" \
> ${_backupDestArchiveDir}/${i}/${i}-${filedate}.svn 2> $err_Log
exit $?
)
retval=$?
else
$(
$sudo $svnadmin_bin dump $svn_source_base_path/$i 2> /dev/null \
> ${_backupDestArchiveDir}/${i}/${i}-${filedate}.svn 2> $err_Log
exit $?
)
retval=$?
fi
err_msg="Cannot save repository \"$i\""
keep_backup_on_error=false
if [ "$retval" = 0 ];then
[ -z $svn_gzip ] && svn_gzip=false
if $svn_gzip ; then
$gzip ${_backupDestArchiveDir}/${i}/${i}-${filedate}.svn 2> $err_Log
retval=$?
err_msg="Cannot gzip repository \"$i\""
keep_backup_on_error=true
fi
fi
## - end timestamp
## -
e_timestamp=`$date +"%s"`
## - determin duration
## -
_time=`expr $e_timestamp - $b_timestamp`
t_h=`expr $_time / 60 / 60`
t_rest_h=`expr $_time - $t_h \\* 60 \\* 60`
t_m=`expr $t_rest_h / 60`
t_s=`expr $t_rest_h - $t_m \\* 60`
duration=""
if [ $t_h -gt 0 ]; then
duration="$t_h h : $t_m min : $t_s sec"
elif [ $t_m -gt 0 ];then
duration="$t_m min : $t_s sec"
else
duration="$t_s sec"
fi
## - look about errors..
## -
if [ "$retval" != "0" ]; then
echolog ""
echolog "\t[ERROR] ${err_msg} [ $duration ]\n\t`$cat $err_Log`\n"
if ! $keep_backup_on_error ; then
rm -f ${_backupDestArchiveDir}/${i}/${i}-${filedate}.svn
fi
continue
else
## - print durations right-aligned
## -
[ -z $right_tabstop ] && right_tabstop=65
_tmp_string="${info_msg}${duration}"
_strlen=${#_tmp_string}
_count_blank=`expr $right_tabstop - $_strlen`
_str_blanks=""
while [ $_count_blank -gt 1 ]; do
_str_blanks="$_str_blanks "
_count_blank=`expr $_count_blank - 1`
done
echononl "$_str_blanks"
echolog " [ $duration ]"
fi
done
else
echolog "\tARCHIVE is set to $ARCHIVE. So nothing to do."
fi
echolog ""
## - remove all files older than $days days..
## -
$find $_backupDestArchiveDir -type f -mtime +${days} -exec rm {} \;