Initial import - (formaly a SVN project named rcopy)

This commit is contained in:
Christoph 2017-01-24 15:15:01 +01:00
commit 76433059ad
16 changed files with 3079 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
/conf/logrotate.conf
/conf/rcopy.conf
/hosts/*.sh
/hosts/BAK/*
/hosts/deleted/*

8
README.convert Normal file
View File

@ -0,0 +1,8 @@
# - Convert host files and rcopy.conf file created on
# - rcopy SVN versions eralier than revision 50
# -
cd /root/crontab/rcopy
sed -i '/^default_config_file=/d' hosts/*.sh
sed -i '/^rcopy_functions_file=/d' hosts/*.sh
perl -i -n -p -e "s#\.\s+\\\$default_config_file.*#. \\\$rcopy_conf_file#" hosts/*.sh
perl -i -n -p -e "s#^crontab_base_dir=.*#crontab_base_dir=\\\$rcopy_base_dir#" conf/rcopy.conf

View File

@ -0,0 +1,62 @@
# I put everything in one block and added sharedscripts, so that mysql gets
# flush-logs'd only once.
# Else the binary logs would automatically increase by n times every day.
/var/log/rsync.log {
daily
rotate 7
missingok
compress
su root root
prerotate
filesystem=`df`
label=`tune2fs -l $backup_partition | grep -e "Filesystem volume name:" | awk '{print$4}'`
#label="Root filesystem - /backup"
content_type='Content-Type: text/plain;\n charset="utf-8"'
extra_error_email=""
## - These variables will be exportet from backup script (rcopy.sh)
## -
## - admin_email from_address company logFile logDuration
#admin_email="argus@oopen.de"
#from_address="root@`hostname -f`"
#company="Warenform INET"
#logFile=/backup/log/rsync.log
durations_top="\nBackup durations of the certain hosts:\n"
durations="${durations_top}`cat $logDuration`\n"
send=`cat $logFile`
datum=`date +"%d.%m.%Y"`
echo -e "To:${admin_email}\n${content_type}\nSubject:Backup $company -- $datum\n$filesystem\n\ndisk label: $label\n$durations\n\n$send\n" | /usr/sbin/sendmail -F "BACKUP $company" -f $from_address $admin_email
declare -i error=`grep -e"\[ERROR" $logFile | wc -l`
declare -i warning=`grep -e"\[WARN" $logFile | wc -l`
if [ $error -gt 0 -o $warning -gt 0 ] ; then
err_msg="`grep -A 2 -B 1 -e\"\[ERROR\" $logFile`\n"
warn_msg="`grep -A 2 -B 2 -e\"\[WARN\" $logFile`\n"
msg_top="Backup $company vom $datum.\nBackupprozess (disk label: $label) beendet mit:"
msg_body_01="\t$error Error(s)\n\t$warning Warning(s)\n"
msg_body_02="\n\n--- ERROR(S) ---\n${err_msg}\n\n--- WARNING(S) ...\n${warn_msg}"
msg="${msg_top}\n${msg_body_01}\n${msg_body_02}"
echo -e "To:${admin_email}\n${content_type}\nSubject:Backup Errors $company -- $datum\n${msg}" | /usr/sbin/sendmail -F "Errors BACKUP $company" -f $from_address $admin_email
if [ "X$extra_error_email" != "X" ]; then
echo -e "To:${extra_error_email}\n${content_type}\nSubject:Backup Errors $company -- $datum\n${msg}" | /usr/sbin/sendmail -F "Errors BACKUP $company" -f $from_address $extra_error_email
fi
fi
endscript
}

236
conf/rcopy.conf.sample Normal file
View File

@ -0,0 +1,236 @@
#######################################################
## Configuration-file for remote backupscript ##
#######################################################
# --------------------------------------------------- #
# -------------------- Variable --------------------- #
#
## - Lock directory exists, until the script ends. So
## - we can check, if a previos instanze is already running.
## -
## - That directory is also userd for temporary files
## -
LOCK_DIR=/tmp/rcopy.lock
## --------------------------------------
## - variables only used in parent script
## -
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
BACKUP_HOST=`hostname -f`
HOME_DIR=$HOME
AGENT=/tmp/.agent-$USER.sh
admin_email="<ADMIN_EMAIL>"
from_address="backup@`hostname -f`"
company="<COMPANY>"
content_type='Content-Type: text/plain;\n charset="utf-8"'
restart_samba_service=false
samba_exe=/usr/local/samba/sbin/samba
samba_init_script=/etc/init.d/samba4
## --------------------------------
## - variables, used in subshells..
## -
#REMOTE_USER=back
crontab_base_dir=$rcopy_base_dir
hosts_base_dir=$crontab_base_dir/hosts
extern_usb_disk=false
extern_sata_disk=false
intern_disk=false
## - if backup partion cannot be determined, try this one..
## -
_backup_partition="<BACKUP_PARTITION>"
backup_mountpoint="<BACKUP_MOUNTPOINT>"
## - Should check, if backup partion is mounted ? usefull if
## - backup partion is not automounted after restart.
## -
check_mountpoint=false
base_label_name="<BACKUP-AH>"
## - if encrypted backup partition
## -
crypto_backup_device=false
crypto_key_file="<CRYPTO_KEY_FILE>"
backup_partition_name=backup
#mount_flags="-o user_xattr,acl,barrier=1"
mount_flags=""
if $extern_usb_disk || $extern_sata_disk ; then
backup_partition=
backup_raw_partition=
disk_identifier=
_found=false
## - In case of unencrypted (and labeled) extern backup disc
## -
if [ -d "/dev/disk/by-label" ]; then
backup_partition=$(realpath /dev/disk/by-label/`ls /dev/disk/by-label | grep $base_label_name`)
[[ "X" != "X$backup_raw_partition" ]] && _found=true
fi
## - Otherwise, assuming the backup disc is the alphabetic last attached device
## - in device list
## -
if ! $_found ; then
for file in `ls /dev/disk/by-uuid` ; do
_disk=`basename $(realpath /dev/disk/by-uuid/$file) | grep -e "^sd"`
[[ "X" = "${_disk}X" ]] && continue
[[ "$disk_identifier" < "$_disk" ]] && disk_identifier=$_disk
done
if [ "X$disk_identifier" = "X" ]; then
## - Backup device could not be detected, so we will try the (above) given one..
## -
backup_raw_partition=$_backup_partition
else
backup_raw_partition=/dev/$disk_identifier
fi
if $crypto_backup_device ; then
backup_partition="/dev/mapper/${backup_partition_name}"
else
backup_partition=$backup_raw_partition
fi
fi
elif $intern_disk ; then
if $crypto_backup_device ; then
backup_raw_partition=$_backup_partition
backup_partition="/dev/mapper/${backup_partition_name}"
else
backup_partition=$_backup_partition
fi
fi
## - send reminder for changing the backup disk
## -
#send_reminder=true
send_reminder=false
reminder_day="Montag" ## - maybe you have set to "Monday"
reminder_email="<REMINDER_EMAIL>"
reminder_email_2=""
backup_base_dir=${backup_mountpoint}/backup
#backup_base_dir=/backup
## - location of the logfile
## -
logFile="/var/log/rsync.log"
disk_label_log_file="/var/log/backup_disk-label.log"
## - logging backup durations in an different file
## - NOTICE:
## - because of the process number in the filename, take care, not to
## - override it by child processes, which also read this file
## -
## - for a better looking, define a right hand tab stop
## -
if [ -z "$logDuration" ];then
logDuration=${LOCK_DIR}/duration.log
fi
right_tabstop=60
info_file="/tmp/LAST_BACKUP_archiv.info"
> $info_file
## - force to create level 1 backups. the backups are not cascaded, they
## - all are direct related to the complete one.
## -
force_level_1=true
## - What to do
## - Note: 1) without mirroring no archiving will be done
## - 2) in case of database backup only the parameter
## - "MIRROR" will be considered
## -
MIRROR=true
ARCHIVE=true
## - how long to hold backup-files ?
## -
days=15
# a bigger (integer-)value for_DEBUG "1"results in more
# infomation, written to the logFile
#
# possible values: 0 , 1 or 2
#
_DEBUG=0
# if _TEST is set to "1", nothing will be done. instead
# rsync will just report the actions it would have
# taken to the $logFile
#
_TEST=0
export LOCK_DIR admin_email from_address content_type company hosts_base_dir intern_disk extern_usb_disk extern_sata_disk crypto_backup_device backup_base_dir backup_partition check_mountpoint info_file logFile logDuration right_tabstop disk_label_log_file force_level_1 MIRROR ARCHIVE days _DEBUG _TEST
#
# ------------------ Ende Variable ------------------ #
# --------------------------------------------------- #
# --------------------------------------------------- #
# -------------------- Programme -------------------- #
#
basename=`which basename`
dirname=`which dirname`
awk=`which awk`
cat=`which cat`
cryptsetup=`which cryptsetup`
cut=`which cut`
date=`which date`
dd=`which dd`
e2fsck=`which e2fsck`
find=`which find`
fdisk=`which fdisk`
grep=`which grep`
gzip=`which gzip`
hdparm=`which hdparm`
logrotate=`which logrotate`
mkdir=`which mkdir`
mount=`which mount`
mv=`which mv`
parted=`which parted`
ps=`which ps`
rm=`which rm`
rsync=`which rsync`
scp=`which scp`
sgdisk=`which sgdisk`
#ssh=`which ssh`
ssh="`which ssh` -n"
sort=`which sort`
tar=`which tar`
tune2fs=`which tune2fs`
umount=`which umount`
sfdisk=`which sfdisk`
su=`which su`
sudo=`which sudo`
sort=`which sort`
sync=`which sync`
touch=`which touch`
export basename dirname awk cat cryptsetup cut date dd e2fsck find fdisk grep gzip hdparm logrotate mkdir mount mv parted ps rm rsync scp ssh sort tar tune2fs umount sfdisk sgdisk su sudo sort sync touch
#
# ------------------ Ende Programme ----------------- #
# --------------------------------------------------- #

54
conf/rcopy_functions.conf Normal file
View File

@ -0,0 +1,54 @@
#######################################################
## Functions for remote backupscript ##
#######################################################
# --------------------------------------------------- #
# ------------------- Funktionen -------------------- #
#
echolog(){
if [ -n "$2" ];then
case $_DEBUG in
0)
if [ "$2" -eq 0 ]; then
echo -e "$1" >> $logFile
fi
;;
1)
if [ "$2" -lt 2 ]; then
echo -e "$1" >> $logFile
fi
;;
2)
echo -e "$1" >> $logFile
;;
*)
;;
esac
else
echo -e "$1" >> $logFile
fi
}
#
#
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
# echo "$*\\c" 1>&2
echo "$*\\c" >> $logFile
else
# echo -e -n "$*" 1>&2
echo -e -n "$*" >> $logFile
fi
rm /tmp/shprompt$$
}
#
#
fatal(){
echolog "!!! The backup procedure was interrupted !!!"
echolog "\nError:\n\t $*"
echolog "\n*** End Update -- no backups are done ***\n\n"
exit 1
}
#
# ------------------ Ende Funktionen ---------------- #
# --------------------------------------------------- #

15
convert.sh Executable file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
# - Convert user created files from rcopy svn versions eralier than revision 50
# -
# - Change your existing hosts files and rcopy.conf file.
# -
base_path=/root/crontab/rcopy
sed -i '/^default_config_file=/d' ${base_path}/hosts/*.sh
sed -i '/^rcopy_functions_file=/d' ${base_path}/hosts/*.sh
perl -i -n -p -e "s#\.\s+\\\$default_config_file.*#. \\\$rcopy_conf_file#" ${base_path}/hosts/*.sh
perl -i -n -p -e "s#^crontab_base_dir=.*#crontab_base_dir=\\\$rcopy_base_dir#" ${base_path}/conf/rcopy.conf
exit 0

View File

@ -0,0 +1,175 @@
#!/usr/bin/env bash
# --------------------------------------------------- #
# -------------------- Variable --------------------- #
#
## --------------------------- ##
## - - ##
## - USER SETTINGS - ##
## - - ##
## --------------------------- ##
## - load default configuration
## -
. $rcopy_conf_file
. $rcopy_functions_file
## - set hostname, which is wanted to backup
## -
srcHost=<hostname (fqdn) or localhost>
## - sync via ssh tunnel
## -
## - if syncing via ssh sh_tunnel, create/modify file
## - ~/.ssh/config with the following contents
## -
## - host localhost
## - user back
## - Port 9999
## - ForwardAgent yes
## - StrictHostKeyChecking no
## - LogLevel FATAL
## -
## - NOTE !!
## - you cannot have different entries for localhost.
## -
## - A ssh tunnel localhost -> $target_host
## - will be created.
## -
ssh_tunnel=false
ssh_tunnel_local_port=9999
ssh_tunnel_target_port=22
ssh_tunnel_key_file=
## - if syncing via ssh hop-host, create/modify file
## - ~/.ssh/config with the following contents
## -
## - host localhost
## - user back
## - Port 9999
## - ForwardAgent yes
## - StrictHostKeyChecking no
## - LogLevel FATAL
## -
## - NOTE !!
## - you cannot have different entries for localhost.
## -
## - A ssh tunnel localhost -> $hop_host -> $target_host
## - will be created.
## -
ssh_hopping=false
ssh_hop_host=shell.so36.net
hop_host_port=1036
target_port=1036
local_port=9999
ssh_keyfile=$HOME/.ssh/id_dsa
## - remote user
## -
ssh_user=back
## - what to store ?
## -
## - these directories will be archived
## - into one file
##
dir_backup="<blank seperatet list of directories>"
## - backup network filesystems like nfs or smbfs
## -
## - NOTICE!
## - - mounting network devices is only possible on localhost - YET
## - - if network directory should mount, you need an entry in
## - /etc/fstab
## -
mount_netdir=false
net_mounted_dir_backup="<blank seperatet list of network directories>"
## - also store mysql databases ?
## -
mysql_backup=false
## - Since Version 5.6, giving password on command line is considered as insecure.
## - To avoid giving the password on command line, you can use an
## - encrypted option file instead.
## -
## - 1.) Create (encrypted) option file:
## - $ mysql_config_editor set --login-path=local --socket=/var/run/mysqld/mysqld.sock --user=backup --password
## - $ Password:
## -
## - 2.) Set environment variable mysql_credential_args="--login-path=local"
## - Now, the backup script uses the encrypted option file instead of (unencrypt) password
## - on command line.
mysql_credential_args="--login-path=local"
mysql_user=backup
mysql_password=backup
## - gzip mysql dump files?
## -
mysql_gzip=false
## - restart apache after mysqldump
## -
restart_apache=false
## - also store postresql databases ?
## -
pgsql_backup=false
pgsql_user=postgres
## - gzip postgres dump files?
## -
pgsql_gzip=false
## - store disk settings
## -
disksetting_backup=false
## - store svn repositories ?
## -
svn_backup=false
svn_source_base_path="<path-to-svn i.e. /data/svn>"
## - gzip SVN backup files?
## -
svn_gzip=false
## - give global rsync arguments here. maybe you wish
## - to exclude some files or diredtories
## -
#rsync_progArgs="--exclude *.log --exclude *.log.*.gz --delete-excluded "
#rsync_progArgs="--exclude **/dev/* --delete-excluded"
rsync_progArgs="--exclude **/dev/*random --exclude **/log/* --exclude **/logs/* --exclude **/sessions/* --delete-excluded"
## -----------------------------------------------------
## -
## - the following parameters overwrites the values from
## - the golbal configuration file
## - how long to hold backup-files ?
## -
#days=15
# a bigger (integer-)value for_DEBUG "1"results in more
# infomation, written to the logFile
#
# possible values: 0 , 1 or 2
#
#_DEBUG=0
# if _TEST is set to "1", nothing will be done. instead
# rsync will just report the actions it would have
# taken to the $logFile
#
#_TEST=0
export rcopy_functions_file srcHost ssh_user MIRROR ARCHIVE days _DEBUG _TEST ssh_hopping
## - Do the stuff - include main part of host script
## -
. $hosts_base_dir/scripts/main_part.include

226
hosts/scripts/dir_backup.sh Executable file
View File

@ -0,0 +1,226 @@
#!/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
## - back ALL=(root)NOPASSWD:/usr/bin/find
## - back ALL=(root)NOPASSWD:/usr/bin/realpath
## -
## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
## - load functions
## -
. $rcopy_functions_file
# --------------------------------------------------- #
# -------------------- Variable --------------------- #
#
err_Log=${LOCK_DIR}/dir.err.log
> $err_Log
#ssh="/usr/bin/ssh -n"
_backupSrcDir="$backup_dir"
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
find=`$ssh ${ssh_user}@$srcHost which find`
sudo=`$ssh ${ssh_user}@$srcHost which sudo`
realpath=`$ssh ${ssh_user}@$srcHost which realpath`
fi
#
# ------------------ Ende Variable ------------------ #
# --------------------------------------------------- #
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
if ! $ssh ${ssh_user}@${srcHost} "$sudo $find '$_backupSrcDir' -maxdepth 0" > /dev/null 2>&1 ; then
echolog "\t[ERROR]: $_backupSrcDir not found!\n"
exit 0
fi
if [ -n "$realpath" ] ; then
real_backupSrcDir=`$ssh ${ssh_user}@$srcHost "$sudo $realpath '$_backupSrcDir'"`
else
real_backupSrcDir="$_backupSrcDir"
fi
backupSrcParentDir=`$dirname "$real_backupSrcDir"`
real_dir_name=`$basename "$real_backupSrcDir"`
backupSrcDir="${ssh_user}@${srcHost}:\"$real_backupSrcDir\""
else
#if [ ! -e $_backupSrcDir ] ; then
if ! $sudo $find "$_backupSrcDir" -maxdepth 0 > /dev/null 2>&1 ; then
echolog "\t[ERROR]: $_backupSrcDir not found!\n"
exit 0
fi
realpath=`which realpath`
if [ -n "$realpath" ] ; then
real_backupSrcDir=`$realpath "$_backupSrcDir"`
else
real_backupSrcDir="$_backupSrcDir"
fi
backupSrcParentDir=`$dirname "$real_backupSrcDir"`
real_dir_name="`$basename \"$real_backupSrcDir\"`"
backupSrcDir=$real_backupSrcDir
fi
#if [ $destHost != "localhost" ];then
# backup_mirror_dir="${ssh_user}@${destHost}:$backup_mirror_dir"
# `$ssh $mkdir -p $backup_mirror_dir/$backupSrcParentDir`
#else
# $mkdir -p $backup_mirror_dir/$backupSrcParentDir
#fi
$mkdir -p $backup_mirror_dir/$backupSrcParentDir
filedate=`$date +"%Y-%m-%d-%H%M"`
if $MIRROR ;then
if [ "$_backupSrcDir" != "$real_backupSrcDir" ]; then
echolog "\t[ Notice ]: $_backupSrcDir --> $real_backupSrcDir"
fi
## - some checks, to avoid backup directories twice
## -
if [ -f $dirs_backup_done ];then
for _dir in `cat "$dirs_backup_done"` ; do
if [ "$real_backupSrcDir" = "$_dir" ];then
echolog "\t[ Notice ]: $real_backupSrcDir already backuped\n"
exit 0
elif echo $real_backupSrcDir | $grep `echo $_dir/` > /dev/null 2>&1 ; then
echolog "\t[ Notice ]: $real_backupSrcDir already backuped\n"
exit 0
fi
done
fi
for _dir in $backup_dirs ; do
[ -n "$orig_backup_dir" -a "$orig_backup_dir" = $_dir ] && continue
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
if ! $ssh ${ssh_user}@${srcHost} "[ -e $_dir ]" ; then
continue
fi
if [ -n "$realpath" ] ; then
_dir_realpath=`$ssh ${ssh_user}@$srcHost $realpath $_dir`
else
_dir_realpath=$_dir
fi
else
if [ ! -e $_dir ] ; then
continue
fi
if [ -n "$realpath" ] ; then
_dir_realpath=`$realpath $_dir`
else
_dir_realpath=$_dir
fi
fi
## - check, if the directory to save is a subdirectory of an already
## - backuped directory
## -
if echo $real_backupSrcDir | $grep -e "^`echo $_dir_realpath/`" > /dev/null 2>&1 ; then
msg=""
if [ "$_dir_realpath" != "$_dir" ];then
msg="(--> $_dir_realpath)"
fi
echolog "\t[ Notice ]: $real_backupSrcDir will be backuped within $_dir $msg\n"
exit 0
fi
done
## -
## - End: some checks, to avoid backup directories twice
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
## - begin timestamp
## -
b_timestamp=`$date +"%s"`
if [ $_DEBUG -gt 1 -a $_TEST -eq 0 ]; then
echolog "$rsync --rsync-path='sudo rsync' $progArgs $logArgs="$formats" $backupSrcDir $backup_mirror_dir/$backupSrcParentDir" 2
$(
$rsync --rsync-path='sudo -i -u root rsync' $progArgs $logArgs="$formats" \
"$backupSrcDir" $backup_mirror_dir/$backupSrcParentDir >> $logFile 2> $err_Log
exit $?
)
else
$(
$rsync --rsync-path='sudo rsync' $progArgs \
"$backupSrcDir" $backup_mirror_dir/$backupSrcParentDir >> $logFile 2> $err_Log
exit $?
)
fi
retval=$?
## - 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
if [ "$retval" = "24" ]; then
echolog "\t[ Notice ] \"$real_backupSrcDir\": \n\t`$cat $err_Log`\n"
else
echolog "\t[ERROR] Cannot mirror directory \"$real_backupSrcDir\" [ $duration ]\n\t`$cat $err_Log`\n"
fi
else
## - print durations right-aligned
## -
[ -z $right_tabstop ] && right_tabstop=60
info_msg="Directory \"`basename $real_backupSrcDir`\" successfully mirrored"
_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 "\t$info_msg"
echononl "$_str_blanks"
echolog "[ $duration ]"
fi
echo " $real_backupSrcDir" >> $dirs_backup_done
else
echolog "\tMIRROR is set to $MIRROR. So nothing to do."
fi
echolog ""
exit 0

View File

@ -0,0 +1,282 @@
#!/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/which
## - back ALL=(root)NOPASSWD:/sbin/hdparm -I /dev/*
## - back ALL=(root)NOPASSWD:/sbin/fdisk
## - back ALL=(root)NOPASSWD:/sbin/sfdisk -d /dev/*
## - back ALL=(root)NOPASSWD:/bin/dd if=/dev/*
## - back ALL=(root)NOPASSWD:/sbin/parted
## -
## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
## - load functions
## -
. $rcopy_functions_file
# --------------------------------------------------- #
# -------------------- Variable --------------------- #
#
err_Log=${LOCK_DIR}/disksettings.err.log
> $err_Log
dirname_diskinfo=diskinfo
target_dir=$backup_mirror_dir/$dirname_diskinfo
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
sudo=`$ssh ${ssh_user}@$srcHost which sudo`
fdisk=`$ssh ${ssh_user}@$srcHost $sudo which fdisk`
sfdisk=`$ssh ${ssh_user}@$srcHost $sudo which sfdisk`
hdparm=`$ssh ${ssh_user}@$srcHost $sudo which hdparm`
dd=`$ssh ${ssh_user}@$srcHost which dd`
rm=`$ssh ${ssh_user}@$srcHost which rm`
parted=`$ssh ${ssh_user}@$srcHost sudo which parted`
sgdisk=`$ssh ${ssh_user}@$srcHost sudo which sgdisk`
fi
#
# ------------------ Ende Variable ------------------ #
# --------------------------------------------------- #
if [ $destHost != "localhost" ] ;then
`$ssh $mkdir -p $backup_mirror_dir/$dirname_diskinfo`
target_dir="${ssh_user}@${destHost}:$target_dir"
else
$mkdir -p $target_dir
fi
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
filedate=`$date +"%Y-%m-%d-%H%M"`
## - Ermittle Partitions Typ
## -
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
disks=$($ssh ${ssh_user}@$srcHost "sed -ne 's/^.*\([shm]d[a-zA-Z]\+ *$\)/\1/p' /proc/partitions")
else
disks=$(sed -ne 's/^.*\([shm]d[a-zA-Z]\+ *$\)/\1/p' /proc/partitions)
fi
for _disk in $disks ; do
disk=/dev/$_disk
echolog "\n\n\t========"
echolog "\t$disk"
echolog "\t========"
echolog "\n\tBackup identification info for disk $disk.."
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
$ssh ${ssh_user}@$srcHost "$sudo $hdparm -I $disk" > ${LOCK_DIR}/info_${_disk}.txt 2> $err_Log
else
$hdparm -I $disk > ${LOCK_DIR}/info_${_disk}.txt 2> $err_Log
fi
if [ "$?" != "0!" ]; then
$(
$rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/info_${_disk}.txt $target_dir/info_${_disk}.txt >> $logFile 2> $err_Log
exit $?
)
retval=$?
if [ "$retval" = "0" ]; then
echolog "\t--- \"info_${_disk}.txt\" successfully saved ---"
else
echolog "\t[ERROR] Cannot save identification info for disk $disk: \n\t`$cat $err_Log`\n"
fi
else
echolog "\t[ERROR] Cannot determin identification info for disk $disk: \n\t`$cat $err_Log`\n"
fi
if [ "X$parted" = "X" ]; then
echolog "\t[ERROR] program \"parted\" is needed to determin partition type. please install \"parted\"."
continue
fi
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
#pt_type=$($ssh ${ssh_user}@$srcHost "sudo $parted -s $disk print|sed -ne 's@^.*Table: \(.*\)\$@\1@p'")
pt_type=$($ssh ${ssh_user}@$srcHost "sudo $parted -s $disk print | grep -E \"^.*(Table:|tabelle:)\" | cut -d':' -f2 | tr -d ' '")
else
#pt_type=$($parted -s $disk print|sed -ne 's@^.*Table: \(.*\)$@\1@p')
pt_type=$($parted -s $disk print | grep -E "^.*(Table:|tabelle:)" | cut -d':' -f2 | tr -d ' ')
fi
if [ "$pt_type" = "msdos" ]; then
echolog "\n\n\tPartitiontype ${disk}: $pt_type"
echolog "\t-----------------------\n"
echolog "\tBackup partition tables for $disk (info file).."
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
$ssh ${ssh_user}@$srcHost "$sudo $fdisk -lu $disk" > ${LOCK_DIR}/fdisk_${_disk}.txt 2>$err_Log
else
$fdisk -lu $disk > ${LOCK_DIR}/fdisk_${_disk}.txt 2>$err_Log
fi
if [ "$?" != "0" ]; then
echolog "\t[ERROR] Cannot determin partition tables for $disk (info file): \n\t`$cat $err_Log`\n"
else
$(
$rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/fdisk_${_disk}.txt $target_dir/fdisk_${_disk}.txt >> $logFile 2> $err_Log
exit $?
)
retval=$?
if [ "$retval" = "0" ]; then
echolog "\t--- \"fdisk_${_disk}.txt\" successfully saved ---"
else
echolog "\t[ERROR] Cannot save partition tables for $disk (info file): \n\t`$cat $err_Log`\n"
fi
fi
echolog "\n\tBackup master boot record of $disk.."
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
$ssh ${ssh_user}@$srcHost "$sudo $dd if=$disk bs=512 count=1" > ${LOCK_DIR}/${_disk}.mbr 2> $err_Log
else
$dd if=$disk bs=512 count=1 > ${LOCK_DIR}/${_disk}.mbr 2> $err_Log
fi
if [ "$?" != "0!" ]; then
$(
$rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/${_disk}.mbr $target_dir/${_disk}.mbr >> $logFile 2> $err_Log
exit $?
)
retval=$?
if [ "$retval" = "0" ]; then
echolog "\t--- \"${_disk}.mbr\" successfully saved ---"
else
echolog "\t[ERROR] Cannot save master boot record of disk $disk: \n\t`$cat $err_Log`\n"
fi
else
echolog "\t[ERROR] Cannot copy master boot record of $disk: \n\t`$cat $err_Log`\n"
fi
echolog "\n\tBackup partition table of $disk (sfdisk).."
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
$ssh ${ssh_user}@$srcHost "$sudo $sfdisk -d $disk" > ${LOCK_DIR}/${_disk}-partitions.sfdisk 2> $err_Log
else
$sfdisk -d $disk > ${LOCK_DIR}/${_disk}-partitions.sfdisk 2> $err_Log
fi
if [ "$?" != "0!" ]; then
$(
$rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/${_disk}-partitions.sfdisk $target_dir/${_disk}-partitions.sfdisk >> $logFile 2> $err_Log
exit $?
)
retval=$?
if [ "$retval" = "0" ]; then
echolog "\t--- \"${_disk}-partitions.sfdisk\" successfully saved ---"
else
echolog "\t[ERROR] Cannot save partition table of disk $disk: \n\t`$cat $err_Log`\n"
fi
else
echolog "\t[ERROR] Cannot create partition table of of $disk using sfdisk: \n\t`$cat $err_Log`\n"
fi
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
partitions=`$ssh ${ssh_user}@$srcHost "$sudo $fdisk -l 2>/dev/null" | grep -e"^$disk" | awk '{print$1}'` >> $logFile 2> $err_Log
else
partitions=`$fdisk -l 2>/dev/null | grep -e"^$disk" | awk '{print$1}'` >> $logFile 2> $err_Log
fi
for partition in $partitions ; do
_part=`echo $partition | cut -d"/" -f3`
echolog "\n\tBackup boot sector of $partition .."
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
$ssh ${ssh_user}@$srcHost "$sudo $dd if=$partition bs=512 count=1" > ${LOCK_DIR}/boot_sec-${_part}.bin 2> $err_Log
else
$dd if=$partition bs=512 count=1 > ${LOCK_DIR}/boot_sec-${_part}.bin 2> $err_Log
fi
if [ "$?" != "0!" ]; then
$(
$rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/boot_sec-${_part}.bin $target_dir/boot_sec-${_part}.bin >> $logFile 2> $err_Log
exit $?
)
retval=$?
if [ "$retval" = "0" ]; then
echolog "\t--- \"boot_sec-${_part}.bin\" successfully saved ---"
else
echolog "\t[ERROR] Cannot save boot record of partition $partition: \n\t`$cat $err_Log`\n"
fi
else
echolog "\t[ERROR] Cannot copy boot record of ipartition $partition: \n\t`$cat $err_Log`\n"
fi
done
elif [ "$pt_type" = "gpt" ]; then
echolog "\n\n\tPartitiontype ${disk}: $pt_type"
echolog "\t-----------------------\n"
if [ "X$sgdisk" = "X" ]; then
echolog "\t[ERROR] program \"sgdisk\" is needed to determin partition settings. please install \"sgdisk\"."
continue
fi
echolog "\tBackup partition tables for $disk (info file).."
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
$ssh ${ssh_user}@$srcHost "$sudo $sgdisk -p $disk" > ${LOCK_DIR}/sgdisk_${_disk}.txt 2>$err_Log
else
$sgdisk -p $disk > ${LOCK_DIR}/sgdisk_${_disk}.txt 2>$err_Log
fi
if [ "$?" != "0" ]; then
echolog "\t[ERROR] Cannot determin partition tables for $disk (info file): \n\t`$cat $err_Log`\n"
else
$(
$rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/sgdisk_${_disk}.txt $target_dir/sgdisk_${_disk}.txt >> $logFile 2> $err_Log
exit $?
)
retval=$?
if [ "$retval" = "0" ]; then
echolog "\t--- \"fdisk_${_disk}.txt\" successfully saved ---"
else
echolog "\t[ERROR] Cannot save partition tables for $disk (info file): \n\t`$cat $err_Log`\n"
fi
fi
echolog "\n\tBackup partition table of $disk (sgdisk).."
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
$ssh ${ssh_user}@$srcHost "$sudo $sgdisk -b /tmp/${_disk}-partitions.sgdisk $disk" > /dev/null
$scp -p ${ssh_user}@$srcHost:/tmp/${_disk}-partitions.sgdisk ${LOCK_DIR}/${_disk}-partitions.sgdisk > /dev/null 2>&1
else
$sgdisk -b ${LOCK_DIR}/${_disk}-partitions.sgdisk $disk > /dev/null
fi
if [ "$?" != "0!" ]; then
$(
$rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/${_disk}-partitions.sgdisk $target_dir/${_disk}-partitions.sgdisk >> $logFile 2> $err_Log
exit $?
)
retval=$?
if [ "$retval" = "0" ]; then
echolog "\t--- \"${_disk}-partitions.sgdisk\" successfully saved ---"
else
echolog "\t[ERROR] Cannot save partition table of disk $disk: \n\t`$cat $err_Log`\n"
fi
else
echolog "\t[ERROR] Cannot create partition table of of $disk using sgdisk: \n\t`$cat $err_Log`\n"
fi
else
echolog "\t[ERROR] Cannot determin partition type for disk $disk !!"
fi
done
echolog ""
exit 0

View File

@ -0,0 +1,383 @@
## --------------------------- ##
## - - ##
## - !! DON'T CHANGE THIS !! - ##
## - - ##
## --------------------------- ##
destHost=localhost
## - where to store backuped data
## -
script_backup_dir=$backup_base_dir/$srcHost
backup_archiv_dir=$script_backup_dir/archive
backup_mirror_dir=$script_backup_dir/mirror
## - where to find backup scripts
## -
script_dir=$hosts_base_dir/scripts
## - All backuped directories will be written to that file, to
## - avoid multiple execution.
## -
dirs_backup_done="$script_backup_dir/backup_dirs.lst"
export destHost script_backup_dir backup_archiv_dir backup_mirror_dir script_dir dirs_backup_done
#
# ------------------ Ende Variable ------------------ #
# --------------------------------------------------- #
## - Works even if "ssh_hopping" is not set
## -
if [ "X$ssh_hopping" = "X" ] ; then
ssh_hopping=false
fi
## - Works even if "ssh_tunnel" is not set
## -
if [ "X$ssh_tunnel" = "X" ] ; then
ssh_tunnel=false
fi
if ! $ssh_hopping && ! $ssh_tunnel ; then
_via_ssh_tunnel=false
export _via_ssh_tunnel
if [ "$srcHost" = "localhost" -a "$destHost" = "localhost" ] ; then
progArgs="$rsync_progArgs -a -p --delete"
else
progArgs="$rsync_progArgs -a -p -e ssh --delete"
fi
else
## - create ssh-tunnel localhost -> $hop_host -> $target_host
## -
progArgs="-a -p -e ssh --delete"
target_host=$srcHost
srcHost=localhost
_via_ssh_tunnel=true
export _via_ssh_tunnel
## - establish ssh tunnel
## -
if $ssh_hopping ; then
_ssh_keyfile_arg=""
if [ "X$ssh_keyfile" != "X" ]; then
_ssh_keyfile_arg=" -i $ssh_keyfile"
fi
## - delete existing ssh tunnels
## -
tunnel_pid_old=`ps x | grep -e "$local_port:[^:]*:$target_port" | grep -v grep | awk '{print$1}'`
if [ -n "$tunnel_pid_old" ]; then
kill $tunnel_pid_old
fi
$ssh -fN -p $hop_host_port $_ssh_keyfile_arg \
-l $ssh_user \
-L $local_port:$target_host:$target_port \
$ssh_hop_host 2> /dev/null
tunnel_pid=`ps x | grep -e "$local_port:$target_host:$target_port" | grep -v grep | awk '{print$1}'`
else
if [ "X$ssh_tunnel_target_port" = "X" ]; then
ssh_tunnel_target_port=22
fi
_ssh_keyfile_arg=""
if [ "X$ssh_tunnel_key_file" != "X" ]; then
_ssh_keyfile_arg="-i $ssh_tunnel_key_file"
fi
## - delete existing ssh tunnels
## -
tunnel_pid_old=`ps x | grep -e "$ssh_tunnel_local_port:$target_host:$ssh_tunnel_target_port" | grep -v grep | awk '{print$1}'`
if [ -n "$tunnel_pid_old" ]; then
kill $tunnel_pid_old
fi
$ssh -fN -p $ssh_tunnel_target_port $_ssh_keyfile_arg \
-l $ssh_user \
-L $ssh_tunnel_local_port:$target_host:$ssh_tunnel_target_port \
$target_host 2> /dev/null
tunnel_pid=`ps x | grep -e "$ssh_tunnel_local_port:$target_host:$ssh_tunnel_target_port" | grep -v grep | awk '{print$1}'`
fi
fi
logArgs="--log-format"
formats=""
backup_dirs=""
for dir in $dir_backup ; do
backup_dirs="$backup_dirs $dir"
done
found=true
if [ "X$backup_dirs" = "X" ]; then
found=false
fi
if [ "$found" = "true" -o "$pgsql_backup" = "true" -o "$mysql_backup" = "true" -o "$disksetting_backup" = "true" ] ; then
begin_h=`date +%H`
begin_m=`date +%M`
backup_date=`date +"%d.%m.%Y"`
backup_begin_timestamp=`date +"%s"`
if $_via_ssh_tunnel ; then
echolog "\nBegin saving host \"$target_host\" : $backup_date ( ${begin_h}:${begin_m} h )\n"
else
echolog "\nBegin saving host \"$srcHost\" : $backup_date ( ${begin_h}:${begin_m} h )\n"
fi
if ! $found; then
echolog "[ Notice ]: No directories for backup given..\n"
fi
## - BACKUP ..
## -
## - we want run the scripts in a subshell, so we export needed environment
## -
export srcHost progArgs logArgs formats backup_dirs
if $ARCHIVE ;then
echolog "\nGoing to handle archives.. ( `$date +%H`:`$date +%M` h )"
_done=false
if [ ! -d $backup_archiv_dir ]; then
mkdir -p $backup_archiv_dir;
fi
## - remove backups older then $days
## -
archive_dirs=`find $backup_archiv_dir -maxdepth 1 -mindepth 1 -type d -name "Backup_*"`
_today=`date --date "now" +"%Y-%m-%d"`
_timestamp_today=`date --date "$_today" "+%s"`
for _dir in $archive_dirs ; do
_bakup_day=`cat $_dir/BACKUP-DATE`
_backup_timestamp=`date --date "$_bakup_day" +"%s"`
_days=$(echo "scale=0;($_timestamp_today-$_backup_timestamp)/86400" | bc)
if [ $_days -gt $days ]; then
echolog "\n\tRemoving backup from $_bakup_day.."
## - begin timestamp
## -
b_timestamp=`$date +"%s"`
rm -rf $_dir
retval=$?
## - 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
if [ ! "$retval" = 0 ]; then
echo -e "\t[ Error ]: Removing backup from $_bakup_day failed"
else
echolog "\t--- Backup from $_bakup_day successfully removed.. [ $duration ]"
fi
fi
_done=true
done
## - Archive last backup..
## -
if [ -f $backup_mirror_dir/BACKUP-DATE ]; then
_last_backup_date=`head -n1 $backup_mirror_dir/BACKUP-DATE`
if [ ! -d $backup_archiv_dir/Backup_$_last_backup_date ] ; then
echolog "\n\tArchiving (last) backup from $_last_backup_date.. ( `$date +%H`:`$date +%M` h )"
## - begin timestamp
## -
b_timestamp=`$date +"%s"`
cp -ral $backup_mirror_dir $backup_archiv_dir/Backup_$_last_backup_date
## - 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
if [ ! "$?" = 0 ]; then
echo -e "\t[ Error ]: Archiving backup from $_last_backup_date failed"
else
echolog "\t--- Backup from $_last_backup_date successfully archived.. [ $duration ]"
fi
fi
_done=true
fi
if ! $_done ; then
echolog "\n\tNothing to do. - No last backup found, no backup older then $days days found."
fi
echolog ""
fi
if $disksetting_backup ; then
echolog "\nGoing to backup disk settings.. ( `$date +%H`:`$date +%M` h )"
$script_dir/disksettings_backup.sh
fi
if $svn_backup;then
echolog "\nGoing to backup svn repositories.. ( `$date +%H`:`$date +%M` h )"
export svn_source_base_path svn_gzip
$script_dir/svn_backup.sh
fi
if $mysql_backup ;then
echolog "\nGoing to backup mysql databases.. ( `$date +%H`:`$date +%M` h )"
export mysql_user mysql_password mysql_credential_args mysql_gzip
$script_dir/mysql_backup.sh
## - !!
if $restart_apache ;then
$ssh ${ssh_user}@$srcHost "sudo /etc/init.d/apache2 restart"
fi
fi
if $pgsql_backup;then
echolog "\nGoing to backup postgres databases.. ( `$date +%H`:`$date +%M` h )"
export pgsql_user pgsql_gzip
$script_dir/pgsql_backup.sh
fi
echolog "\nGoing to backup directories.. ( `$date +%H`:`$date +%M` h )"
_done=false
for dir in $dir_backup ; do
echolog "\n\tDirectory $dir"
backup_dir=$dir $script_dir/dir_backup.sh
_done=true
done
if ! $_done ; then
echo -e "\t[ Warning ]: No directory for backup selected !!"
fi
if $mount_netdir; then
echolog "\nGoing to backup network directories.. ( `$date +%H`:`$date +%M` h )"
for dir in $net_mounted_dir_backup ; do
echolog "\n\tBackup network directory $dir"
export mount_netdir
backup_dir=$dir $script_dir/net_mounted_dir_backup.sh
done
fi
end_h=`$date +%H`
end_m=`$date +%M`
backup_date=`$date +"%d.%m.%Y"`
backup_end_timestamp=`date +"%s"`
backup_time=`expr $backup_end_timestamp - $backup_begin_timestamp`
backup_h=`expr $backup_time / 60 / 60`
backup_rest_h=`expr $backup_time - $backup_h \\* 60 \\* 60`
backup_m=`expr $backup_rest_h / 60`
backup_s=`expr $backup_rest_h - $backup_m \\* 60`
if [ $backup_m -lt 10 ] ;then
backup_m="0$backup_m"
fi
if [ $backup_s -lt 10 ] ;then
backup_s="0$backup_s"
fi
rm -f $dirs_backup_done
if $_via_ssh_tunnel ; then
echolog "\nEnd saving host \"$target_host\" : $backup_date ( ${end_h}:${end_m} h ) - duration: ${backup_h} h :${backup_m} min : ${backup_s} sec\n\n"
else
echolog "\nEnd saving host \"$srcHost\" : $backup_date ( ${end_h}:${end_m} h ) - duration: ${backup_h} h :${backup_m} min : ${backup_s} sec\n\n"
fi
## - write duration time (a little bit formatted) to logfile of durations
## -
## - determin duration
## -
duration=""
if [ $backup_h -gt 0 ]; then
duration="$backup_h h : $backup_m min : $t_s sec"
elif [ $backup_m -gt 0 ];then
duration="$backup_m min : $backup_s sec"
else
duration="$backup_s sec"
fi
if $_via_ssh_tunnel ; then
_host=$target_host
else
_host=$srcHost
fi
_tmp_string="${_host}${duration}"
_strlen=${#_tmp_string}
_count_blank=`expr $right_tabstop - $_strlen`
echo -n -e " $_host:" >> $logDuration
_str_blanks=""
while [ $_count_blank -gt 1 ]; do
_str_blanks="$_str_blanks "
_count_blank=`expr $_count_blank - 1`
done
echo -n "$_str_blanks" >> $logDuration
echo -n " " >> $logDuration
echo -n -e "$duration\n" >> $logDuration
## - Set backup date
## -
## - Notice!!
## - We ave to remove olf file and set a new one, because we are
## - working with hard links
## -
[ -f $backup_mirror_dir/BACKUP-DATE ] && rm -f $backup_mirror_dir/BACKUP-DATE
[ -d $backup_mirror_dir ] && \
echo `date --date "now" +"%Y-%m-%d"` > $backup_mirror_dir/BACKUP-DATE
else
echolog "\n[WARNING HOST \"$srcHost\"]: Nothing was marked for backup !!\n"
fi
if $_via_ssh_tunnel ; then
kill $tunnel_pid
fi

625
hosts/scripts/mysql_backup.sh Executable file
View File

@ -0,0 +1,625 @@
#!/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
## - Event_priv
## - Process_priv (since MySQL 5.5)
## -
## - INSERT INTO user (Host,User,Password,Select_priv,Lock_tables_priv,Show_view_priv,Event_priv) VALUES('localhost','backup',password('backup'),'Y','Y','Y','Y');
## -
## - Since MySQL 5.5 - you also have to add process privileges (Process_priv = 'Y')
## - INSERT INTO user (Host,User,Password,Select_priv,Process_priv,Lock_tables_priv,Show_view_priv,Event_priv) VALUES('localhost','backup',password('backup'),'Y','Y','Y','Y','Y');
## -
## -
## - Since MySQL 5.7.x - you also have to add process privileges (Execute_priv = 'Y')
## - Password field is now: "authentication_string"
## - INSERT INTO user (Host,User,authentication_string,Select_priv,Process_priv,Lock_tables_priv,Show_view_priv,Event_priv,Execute_priv,ssl_cipher,x509_issuer,x509_subject) VALUES('localhost','backup',password('backup'),'Y','Y','Y','Y','Y','Y','','','');
## -
## - or if updating from older mysql version:
## -
## - UPDATE user SET Execute_priv = 'Y' WHERE User = 'backup';
## -
## -
## - FLUSH PRIVILEGES;
## -
## -
## - Since Version 5.6, giving password on command line is considered as insecure.
## - To avoid giving the password on command line, you can use an
## - encrypted option file instead.
## -
## - 1.) Create (encrypted) option file:
## -
## - Debian package installation:
## - $ mysql_config_editor set --login-path=local --socket=/var/run/mysqld/mysqld.sock --user=backup --password
## - $ Password:
## -
## - Source installation:
## - $ mysql_config_editor set --login-path=local --socket=/tmp/mysql.sock --user=backup --password
## - $ Password:
## -
## - 2.) Set environment variable mysql_credential_args="--login-path=local"
## - Now, the backup script uses the encrypted option file instead of (unencrypt) password
## - on command line.
## -
## -
## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
err_Log=${LOCK_DIR}/mysql.err.log
> $err_Log
## - load functions
## -
. $rcopy_functions_file
# --------------------------------------------------- #
# -------------------- Variable --------------------- #
#
_backupDestArchiveDir="${script_backup_dir}/MySQL"
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
mysql=`$ssh ${ssh_user}@$srcHost which mysql`
mysqldump=`$ssh ${ssh_user}@$srcHost which mysqldump`
ssh_options="-o BatchMode=yes -o ConnectTimeout=360"
else
mysql=`which mysql`
mysqldump=`which mysqldump`
fi
if [ -z "$mysql" ]; then
mysql="/usr/local/mysql/bin/mysql"
fi
if [ -z "$mysqldump" ]; then
mysqldump="/usr/local/mysql/bin/mysqldump"
fi
#
# ------------------ Ende Variable ------------------ #
# --------------------------------------------------- #
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
if [ -n "$mysql_credential_args" ] ; then
DATABASES=`$ssh ${ssh_user}@$srcHost "$mysql $mysql_credential_args -N -s -e \"show databases\""`
MYSQL_VERSION=`$ssh ${ssh_user}@$srcHost "$mysql $mysql_credential_args -N -s -e \"SELECT VERSION()\""`
else
DATABASES=`$ssh ${ssh_user}@$srcHost "$mysql -u$mysql_user -p$mysql_password -N -s -e \"show databases\""`
MYSQL_VERSION=`$ssh ${ssh_user}@$srcHost "$mysql -u$mysql_user -p$mysql_password -N -s -e \"SELECT VERSION()\""`
fi
else
if [ -n "$mysql_credential_args" ] ; then
DATABASES=`$mysql $mysql_credential_args -N -s -e "show databases"`
MYSQL_VERSION=`$mysql $mysql_credential_args -N -s -e "SELECT VERSION()"`
else
DATABASES=`$mysql -u$mysql_user -p$mysql_password -N -s -e "show databases"`
MYSQL_VERSION=`$mysql -u$mysql_user -p$mysql_password -N -s -e "SELECT VERSION()"`
fi
fi
MYSQL_VERSION_NUM=`echo $MYSQL_VERSION | awk -F \. '{ printf "%02d", $1; printf "%02d", $2; printf "%02d", $3 }'`
if [ ! -d $_backupDestArchiveDir ]; then
mkdir -p $_backupDestArchiveDir
fi
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 SQL Grants for users
## -
info_msg="save SQL Grants for users"
echononl "\t$info_msg"
## - begin timestamp
## -
b_timestamp=`$date +"%s"`
filedate=`$date +"%Y-%m-%d-%H%M"`
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
if [ -n "$mysql_credential_args" ] ; then
$(
$ssh $ssh_options ${ssh_user}@$srcHost "$mysql $mysql_credential_args -N -s -A -e\"SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user WHERE user<>''\" | $mysql $mysql_credential_args -N -s -A | sed 's/\$/;/g'" \
> ${_backupDestArchiveDir}/MySQLGrants.sql-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
else
$(
$ssh $ssh_options ${ssh_user}@$srcHost "$mysql -u$mysql_user -p$mysql_password -N -s -A -e\"SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user WHERE user<>''\" | $mysql -u$mysql_user -p$mysql_password -N -s -A | sed 's/\$/;/g'" \
> ${_backupDestArchiveDir}/MySQLGrants.sql-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
fi
else
if [ -n "$mysql_credential_args" ] ; then
$(
$mysql $mysql_credential_args -N -s -A -e"SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user WHERE user<>''" | $mysql $mysql_credential_args -N -s -A | sed 's/\$/;/g' \
> ${_backupDestArchiveDir}/MySQLGrants.sql-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
else
$(
$mysql -u$mysql_user -p$mysql_password -N -s -A -e"SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user WHERE user<>''" | $mysql -u$mysql_user -p$mysql_password -N -s -A | sed 's/\$/;/g' \
> ${_backupDestArchiveDir}/MySQLGrants.sql-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
fi
fi
err_msg="Cannot save SQL Grants for users"
keep_backup_on_error=false
if [ "$retval" = 0 ];then
[ -z $mysql_gzip ] && mysql_gzip=false
if $mysql_gzip ; then
$gzip ${_backupDestArchiveDir}/MySQLGrants.sql-${filedate}.sql 2> $err_Log
retval=$?
err_msg="Cannot gzip \"MySQLGrants.sql-${filedate}.sql\""
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}/MySQLGrants.sql-${filedate}.sql
fi
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
## - End: Backup SQL Grants for users
## ----------------------------------
## -----------------------------
## - Backup/Create Database Craetion sql statements
## -
info_msg="create database creation SQL file"
echononl "\t$info_msg"
filedate=`$date +"%Y-%m-%d-%H%M"`
> ${_backupDestArchiveDir}/MySQL_Create_Databases-${filedate}.sql
## - begin timestamp
## -
b_timestamp=`$date +"%s"`
retval=0
for i in $DATABASES ; do
if [ "$i" = "information_schema" -o "$i" = "performance_schema" ];then
continue
fi
echo "CREATE DATABASE IF NOT EXISTS $i CHARACTER SET utf8 COLLATE utf8_general_ci;" \
>> ${_backupDestArchiveDir}/MySQL_Create_Databases-${filedate}.sql
if [ "$?" != 0 ]; then
retval=1
fi
done
err_msg="Cannot create Database Creation SQL file"
keep_backup_on_error=false
if [ "$retval" = 0 ];then
[ -z $mysql_gzip ] && mysql_gzip=false
if $mysql_gzip ; then
$gzip ${_backupDestArchiveDir}/MySQL_Create_Databases-${filedate}.sql 2> $err_Log
retval=$?
err_msg="Cannot gzip \"MySQLGrants.sql-${filedate}.sql\""
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}/MySQL_Create_Databases-${filedate}.sql
fi
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
## - End: Create Database Craetion sql statements
## -----------------------------------------------------
## - backup all databases except the those from midgard..
## -
for i in $DATABASES ; do
## ------------------------------------
## - Save Functions seperatly if exists
## -
declare -i COUNT_FUNCTION=0
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
if [ -n "$mysql_credential_args" ] ; then
COUNT_FUNCTION=`$ssh ${ssh_user}@$srcHost "$mysql $mysql_credential_args -N -s -e \"SELECT COUNT(*) FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = '${i}'\""`
else
COUNT_FUNCTION=`$ssh ${ssh_user}@$srcHost "$mysql -u$mysql_user -p$mysql_password -N -s -e \"SELECT COUNT(*) FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = '${i}'\""`
fi
else
if [ -n "$mysql_credential_args" ] ; then
COUNT_FUNCTION=`$mysql $mysql_credential_args -N -s -e "SELECT COUNT(*) FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = '${i}'"`
else
COUNT_FUNCTION=`$mysql -u$mysql_user -p$mysql_password -N -s -e "SELECT COUNT(*) FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = '${i}'"`
fi
fi
if [ $COUNT_FUNCTION -gt 0 ] ; then
## - begin timestamp
## -
b_timestamp=`$date +"%s"`
filedate=`$date +"%Y-%m-%d-%H%M"`
mysqldump_flags="--no-data --no-create-info --routines"
info_msg="save functions of database $i.."
echononl "\t$info_msg"
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
if [ -n "$mysql_credential_args" ] ; then
$(
$ssh $ssh_options ${ssh_user}@$srcHost "$mysqldump $mysql_credential_args $mysqldump_flags $i" \
> ${_backupDestArchiveDir}/${i}/MySQLStoredProcedures.${i}-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
else
$(
$ssh $ssh_options ${ssh_user}@$srcHost "$mysqldump -u$mysql_user -p$mysql_password $mysqldump_flags $i" \
> ${_backupDestArchiveDir}/${i}/MySQLStoredProcedures.${i}-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
fi
else
if [ -n "$mysql_credential_args" ] ; then
$(
$mysqldump $mysql_credential_args $mysqldump_flags $i \
> ${_backupDestArchiveDir}/${i}/MySQLStoredProcedures.${i}-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
else
$(
$mysqldump -u$mysql_user -p$mysql_password $mysqldump_flags $i \
> ${_backupDestArchiveDir}/${i}/MySQLStoredProcedures.${i}-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
fi
fi
err_msg="Cannot save functions of database \"$i\""
keep_backup_on_error=false
if [ "$retval" = 0 ];then
[ -z $mysql_gzip ] && mysql_gzip=false
if $mysql_gzip ; then
$gzip ${_backupDestArchiveDir}/MySQLStoredProcedures.${i}-${filedate}.sql 2> $err_Log
retval=$?
err_msg="Cannot gzip \"MySQLStoredProcedures.${i}-${filedate}.sql\""
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}/MySQLStoredProcedures.${i}-${filedate}.sql
fi
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
fi
## - End: Save Functions seperatly if exists
## -----------------------------------------
## --------------
## - Dump Databas
filedate=`$date +"%Y-%m-%d-%H%M"`
## - begin timestamp
## -
b_timestamp=`$date +"%s"`
## - Like --opt but without --extended-insert. So
mysqldump_flags="--protocol=SOCKET --max_allowed_packet=128M --skip-opt --add-drop-table --add-locks --create-options --quick --set-charset --disable-keys --lock-tables --routines"
if [ "$i" = "information_schema" -o "$i" = "performance_schema" ];then
mysqldump_flags="$mysqldump_flags --single-transaction"
if [ "$i" = "information_schema" -a "$MYSQL_VERSION_NUM" -ge 050700 ]; then
## - As of MySQL 5.7.6, the Performance Schema also contains system and status variable tables.
## -
## - The Performance Schema tables are intended to replace the INFORMATION_SCHEMA tables,
## - which are deprecated as of MySQL 5.7.6 and will be removed in a future MySQL release.
## -
## - As of MySQL 5.7.6, the value of the "show_compatibility_56" system variable
## - affects the information available from "information_schema". If tis variable
## - is set to "OFF" (the default), mysqldump produces the tollowing error:
## -
## - Couldn't execute 'SELECT /*!40001 SQL_NO_CACHE */ * FROM `GLOBAL_STATUS`':
## - The 'INFORMATION_SCHEMA.GLOBAL_STATUS' feature is disabled; see the documentation
## - for 'show_compatibility_56' (3167)
## -
## - We will ignore errors (force) and also we don#t want error messages
## -
mysqldump_flags="$mysqldump_flags --force --log-error=/dev/null"
fi
elif [ "$i" = "mysql" -a "$MYSQL_VERSION_NUM" -ge 050500 ]; then
mysqldump_flags="$mysqldump_flags --events"
fi
if [ "$destHost" = "localhost" ]; then
if [ ! -d ${_backupDestArchiveDir}/${i} ] ; then
$mkdir -p ${_backupDestArchiveDir}/${i}
fi
fi
info_msg="save database $i.."
echononl "\t$info_msg"
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
if [ -n "$mysql_credential_args" ] ; then
$(
$ssh $ssh_options ${ssh_user}@$srcHost "$mysqldump $mysql_credential_args $mysqldump_flags $i" \
> ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
else
$(
$ssh $ssh_options ${ssh_user}@$srcHost "$mysqldump -u$mysql_user -p$mysql_password $mysqldump_flags $i" \
> ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
fi
else
if [ -n "$mysql_credential_args" ] ; then
$(
$mysqldump $mysql_credential_args $mysqldump_flags $i \
> ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
else
$(
$mysqldump -u$mysql_user -p$mysql_password $mysqldump_flags $i \
> ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
fi
fi
err_msg="Cannot save database \"$i\""
keep_backup_on_error=false
if [ "$i" = "information_schema" -a "$MYSQL_VERSION_NUM" -ge 050700 ]; then
## - As sayed befor: in that case, we want ignore all errors taken from that dump
## -
retval=0
fi
if [ "$retval" = 0 ];then
[ -z $mysql_gzip ] && mysql_gzip=false
if $mysql_gzip ; then
$gzip ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log
retval=$?
err_msg="Cannot gzip database \"$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}.sql
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
## - End: Dump Databas
## -------------------
done
## /usr/bin/killall -HUP mysqld
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 {} \;

View File

@ -0,0 +1,59 @@
#!/usr/bin/env bash
## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
## -
## - If network directory have to mount (its only possible if backuping
## - localhost) you need an entry in your /etc/fstab, becaus we mount thuch
## - remote directories with "mount <remote directory)
## -
## - i.e. for a samba mounted directory you need an entry like:
## -
## - //192.168.100.20/qinter /mnt/file-win7/qinter smbfs noauto,user=Administrator,password=chnarzfoo5 0 0
## -
## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
## - load functions
## -
. $rcopy_functions_file
if $mount_netdir ; then
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
echolog "\t[ERROR] Mounting network directories is only possible for localhost - yet"
exit 1
else
## - Network directory mounted ?
## -
if ! df | grep "$backup_dir" > /dev/null 2>&1 ;then
$mount $backup_dir > /dev/null 2>&1
fi
sleep 5
if ! df | grep "$backup_dir" > /dev/null 2>&1 ;then
echolog "\t[ERROR] Cannot mount network directory to \"$backup_dir\""
exit 1
fi
fi
fi
## - Check if directory is empty
## -
if [ ! "$(ls -A $backup_dir)" ]; then
echolog "\t[ NOTICE ]: Directory \"$backup_dir\" is empty. So nothing to do..\n"
exit 0
fi
echolog "\n\tBackup network directory \"$backup_dir\" ( `$date +%H`:`$date +%M` h )"
backup_dir=$backup_dir $script_dir/dir_backup.sh
if $mount_netdir ; then
if [ $srcHost != "localhost" ];then
echolog "\t[ERROR] Mounting network directories is only possible for localhost - yet"
exit 1
else
$umount $backup_dir 2> /dev/null
fi
fi
exit 0

376
hosts/scripts/pgsql_backup.sh Executable file
View File

@ -0,0 +1,376 @@
#!/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 lines:
## - back ALL = (postgres) NOPASSWD: /usr/bin/psql
## - back ALL = (postgres) NOPASSWD: /usr/bin/pg_dump
## - back ALL = (postgres) NOPASSWD: /usr/bin/pg_dumpall
## -
## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
err_Log=${LOCK_DIR}/pgsql.err.log
> $err_Log
## - load functions
## -
. $rcopy_functions_file
# --------------------------------------------------- #
# -------------------- Variable --------------------- #
#
_backupDestArchiveDir="${script_backup_dir}/PostgreSQL"
if [ ! -d $_backupDestArchiveDir ]; then
mkdir -p $_backupDestArchiveDir
fi
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
psql=`$ssh ${ssh_user}@$srcHost which psql`
pg_dump=`$ssh ${ssh_user}@$srcHost which pg_dump`
pg_dumpall=`$ssh ${ssh_user}@$srcHost which pg_dumpall`
sudo=`$ssh ${ssh_user}@$srcHost which sudo`
su=`$ssh ${ssh_user}@$srcHost which su`
ssh_options="-o BatchMode=yes -o ConnectTimeout=360"
else
psql=`which psql`
pg_dump=`which pg_dump`
pg_dumpall=`which pg_dumpall`
fi
if [ -z "$psql" ]; then
psql="/usr/local/pgsql/bin/psql"
fi
if [ -z "$pg_dump" ]; then
pg_dump="/usr/local/pgsql/bin/pg_dump"
fi
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
DATABASES=`$ssh ${ssh_user}@$srcHost "$sudo -u $pgsql_user $psql -lt" \
| $grep -v -e"^$" | $awk '{print$1}'`
else
DATABASES=`$su - $pgsql_user -c "$psql -lt" | $grep -v -e"^$" | $awk '{print$1}'`
fi
# ------------------ Ende Variable ------------------ #
# --------------------------------------------------- #
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 PostgreSQL's Roles
## -
info_msg="save PostgreSQL's Roles"
echononl "\t$info_msg"
## - begin timestamp
## -
b_timestamp=`$date +"%s"`
filedate=`$date +"%Y-%m-%d-%H%M"`
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
$(
$ssh $ssh_options ${ssh_user}@$srcHost "$sudo -u $pgsql_user $pg_dumpall -g -c " \
> ${_backupDestArchiveDir}/PostreSQL_Roles-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
else
$(
$su $pgsql_user -c "$pg_dumpall -g -c" \
> ${_backupDestArchiveDir}/PostreSQL_Roles-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
fi
err_msg="Cannot save PostgreSQL's Roles"
keep_backup_on_error=false
if [ "$retval" = 0 ];then
[ -z $pgsql_gzip ] && pgsql_gzip=false
if $pgsql_gzip ; then
$gzip ${_backupDestArchiveDir}/PostreSQL_Roles-${filedate}.sql 2> $err_Log
retval=$?
err_msg="Cannot gzip PostreSQL_Roles-${filedate}.sql"
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}/PostreSQL_Roles-${filedate}.sql
fi
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
## - END: Backup PostgreSQL's Roles
## ---------------------------------
## -----------------------------------
## - Create Database Creation SQL file
## -
info_msg="create Database Creation SQL file"
echononl "\t$info_msg"
## - begin timestamp
## -
b_timestamp=`$date +"%s"`
filedate=`$date +"%Y-%m-%d-%H%M"`
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
$(
$ssh $ssh_options ${ssh_user}@$srcHost "$sudo -u $pgsql_user $pg_dumpall -s " | \
grep -e "^CREATE DATABASE" \
> ${_backupDestArchiveDir}/PostreSQL_Create_Databases-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
else
$(
$su - $pgsql_user -c "$pg_dumpall -s" | \
grep -e "^CREATE DATABASE" \
> ${_backupDestArchiveDir}/PostreSQL_Create_Databases-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
fi
err_msg="Cannot create Database Creation SQL file"
keep_backup_on_error=false
if [ "$retval" = 0 ];then
[ -z $pgsql_gzip ] && pgsql_gzip=false
if $pgsql_gzip ; then
$gzip ${_backupDestArchiveDir}/PostreSQL_Create_Databases-${filedate}.sql 2> $err_Log
retval=$?
err_msg="Cannot gzip PostreSQL_Databases-${filedate}.sql"
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}/PostreSQL_Create_Databases-${filedate}.sql
fi
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
## - End: Create Database Creation SQL file
## ----------------------------------------
## - backup all databases..
## -
for i in $DATABASES ; do
if [ "$i" = "template0" ]; then
continue
fi
if [ "$i" = ":" -o "$i" = "|" ]; then
continue
fi
filedate=`$date +"%Y-%m-%d-%H%M"`
## - begin timestamp
## -
b_timestamp=`$date +"%s"`
if [ "$destHost" = "localhost" ]; then
if [ ! -d ${_backupDestArchiveDir}/${i} ] ; then
$mkdir -p ${_backupDestArchiveDir}/${i}
fi
fi
info_msg="save database $i.."
echononl "\t$info_msg"
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
$(
$ssh $ssh_options ${ssh_user}@$srcHost "$sudo -u $pgsql_user $pg_dump -c $i" \
> ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
else
$(
$su - $pgsql_user -c "$pg_dump -c $i" \
> ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log
exit $?
)
retval=$?
fi
err_msg="Cannot save database \"$i\""
keep_backup_on_error=false
if [ "$retval" = 0 ];then
[ -z $pgsql_gzip ] && pgsql_gzip=false
if $pgsql_gzip ; then
$gzip ${_backupDestArchiveDir}/${i}/${i}-${filedate}.sql 2> $err_Log
retval=$?
err_msg="Cannot gzip database \"$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}.sql
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
if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then
$ssh ${ssh_user}@$srcHost "$sudo -u $pgsql_user $psql -lt" > ${_backupDestArchiveDir}/database-list_${filedate}.txt
else
$su - $pgsql_user -c "$psql -lt" > ${_backupDestArchiveDir}/database-list_${filedate}.txt
fi
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 {} \;

187
hosts/scripts/svn_backup.sh Executable file
View File

@ -0,0 +1,187 @@
#!/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 {} \;

378
rcopy.sh Executable file
View File

@ -0,0 +1,378 @@
#!/usr/bin/env bash
manual=$1
manual=${manual:=false}
if [[ $manual != false && $manual != true ]]; then
echo -e "\n[ Error ]: Usage: `basename $0` [true|false]\n"
exit 2
fi
## - if you plan to run the backup scripts as a cronjob,
## - you have to set this variable manually
## -
USER=$LOGNAME
## - global configuration for the remote backup-script
## -
rcopy_base_dir="$(realpath $(dirname $0))"
rcopy_conf_file="$rcopy_base_dir/conf/rcopy.conf"
rcopy_functions_file=$rcopy_base_dir/conf/rcopy_functions.conf
export rcopy_base_dir rcopy_conf_file rcopy_functions_file
if $manual ; then
## - this is for manual start, we don't want inform anyone and
## - also don't restart any service
## -
send_reminder=false
restart_samba_service=false
fi
## - Note: the file "/var/lib/logrotate/status" must have
## - write permissions for the script-user
## -
logrotate_conf_file="$rcopy_base_dir/conf/logrotate.conf"
## - load configuration
## -
[ -f $rcopy_conf_file ] || exit 1
. $rcopy_conf_file
[ -f $rcopy_functions_file ] || exit 1
. $rcopy_functions_file
## - if backup job already runs, stop execution..
## -
if mkdir "$LOCK_DIR" 2> /dev/null ; then
## - Remove lockdir when the script finishes, or when it receives a signal
trap 'rm -rf "$LOCK_DIR"' 0 2 15
else
msg="[ Error ]: A previos instance of the backup script seems already be running.\n\tExiting now.."
if $manual ; then
echo -e "\n$msg\n"
else
datum=`date +"%d.%m.%Y"`
echo -e "To:${admin_email}\n${content_type}\nSubject:Backup Errors $company -- $datum\n${msg}\n" | /usr/sbin/sendmail -F "Errors BACKUP $company" -f $from_address $admin_email
fi
echo ""
echo "[ Error ]: A previos instance of the backup script seems already be running."
echo ""
echo -e "\tExiting now.."
echo ""
exit 1
fi
if [ "X$check_mountpoint" = "X" ] ; then
check_mountpoint=false
fi
if $check_mountpoint ; then
if [[ ! -d $backup_mountpoint ]];then
msg00="[ Error ]: Mountpoint \"$backup_mountpoint\" for Backup Partion \"$backup_partition\" not found.\n"
msg01=" exiting now.."
msg="${msg00}\n${msg01}"
if $manual ; then
echo -e "\n$msg\n"
else
datum=`date +"%d.%m.%Y"`
echo -e "To:${admin_email}\nSubject:Backup Errors $company -- $datum\n${msg}\n" | /usr/sbin/sendmail -F "Errors BACKUP $company" -f $from_address $admin_email
fi
exit 1
fi
fi
if $extern_usb_disk || $extern_sata_disk || $intern_disk ; then
## - Backup Device mounted ?
## -
if ! df | grep "$backup_partition" > /dev/null 2>&1 ;then
if $crypto_backup_device ;then
$cryptsetup --key-file $crypto_key_file luksOpen $backup_raw_partition $backup_partition_name > /dev/null 2>&1
fi
mount $mount_flags $backup_partition $backup_mountpoint > /dev/null 2>&1
fi
sleep 2
if ! df | grep "$backup_partition" > /dev/null 2>&1 ;then
msg="[ Error ]: Cannot mount Backup Partion \"$backup_partition\". exiting now.."
if $manual ; then
echo -e "\n$msg\n"
else
datum=`date +"%d.%m.%Y"`
echo -e "To:${admin_email}\n${content_type}\nSubject:Backup Errors $company -- $datum\n${msg}\n" | /usr/sbin/sendmail -F "Errors BACKUP $company" -f $from_address $admin_email
fi
exit 1
fi
elif $check_mountpoint ; then
if ! df | grep "$backup_mountpoint" > /dev/null 2>&1 ;then
msg00="[ Error ]: Mountpoint \"$backup_mountpoint\" for Backup Partion \"$backup_partition\" not found.\n"
msg01=" exiting now.."
msg="${msg00}\n${msg01}"
if $manual ; then
echo -e "\n$msg\n"
else
datum=`date +"%d.%m.%Y"`
echo -e "To:${admin_email}\nSubject:Backup Errors $company -- $datum\n${msg}\n" | /usr/sbin/sendmail -F "Errors BACKUP $company" -f $from_address $admin_email
fi
exit 1
fi
fi
## - $AGENT muss (root) gehören und darf nicht beschreibbar sein
## -
if [ ! -O $AGENT ] || ! ps ax | grep ssh-agent | grep -v grep > /dev/null ; then
killall ssh-agent > /dev/null 2> /dev/null
rm -f $AGENT
ssh-agent > $AGENT; . $AGENT > /dev/null ; ssh-add > /dev/null 2>&1
fi
if [ ! -O $AGENT ]; then
msg_00="\n[ Error ]: No file \"$AGENT\" for ssh-agent informations found or i'm not the owner !!"
msg_01="\t ======================="
msg_02="\t !! BACKUP INTERUPTED !!"
msg_03="As user \"$USER\" do:\n\tssh-agent > $AGENT; . $AGENT; ssh-add"
msg="${msg_00}\n\n${msg_01}\n${msg_02}\n${msg_01}\n\n${msg_03}"
if $manual ; then
echo -e "\n$msg\n"
else
datum=`date +"%d.%m.%Y"`
echo -e "To:${admin_email}\n${content_type}\nSubject:Backup Errors $company -- $datum\n${msg}\n" | /usr/sbin/sendmail -F "Errors BACKUP $company" -f $from_address $admin_email
fi
exit 1
fi
. $AGENT > /dev/null 2>&1
if [ ! `$ps aux | $grep -e"^$USER" | $grep "$SSH_AGENT_PID" | $grep ssh-agent | awk '{print$2}'` ]; then
msg_00="\n[ Error ]: No Prozess for ssh-agent with Process ID \"$SSH_AGENT_PID\" found !!"
msg_01="\t ======================="
msg_02="\t !! BACKUP INTERUPTED !!"
msg_03="As user \"$USER\" do:\n\tssh-agent > $AGENT; . $AGENT; ssh-add"
msg="${msg_00}\n\n${msg_01}\n${msg_02}\n${msg_01}\n\n${msg_03}"
if $manual ; then
echo -e "$msg\n"
else
datum=`date +"%d.%m.%Y"`
echo -e "To:${admin_email}\n${content_type}\nSubject:Backup Errors $company -- $datum\n${msg}\n" | /usr/sbin/sendmail -F "Errors BACKUP $company" -f $from_address $admin_email
fi
exit 1
fi
/usr/bin/ssh-add -l > /dev/null 2>&1
if [ ! $? -eq 0 ]; then
msg_00="\n[ Error ]: No Keys associated with ssh-agent !!"
msg_01="\t ======================="
msg_02="\t !! BACKUP INTERUPTED !!"
msg_03="As user \"$USER\" do:\n\tssh-agent > $AGENT; . $AGENT; ssh-add"
msg="${msg_00}\n\n${msg_01}\n${msg_02}\n${msg_01}\n\n${msg_03}"
if $manual ; then
echo -e "\n$msg\n"
else
datum=`date +"%d.%m.%Y"`
echo -e "To:${admin_email}\n${content_type}\nSubject:Backup Errors $company -- $datum\n${msg}\n" | /usr/sbin/sendmail -F "Errors BACKUP $company" -f $from_address $admin_email
fi
exit 1
fi
## - searching hosts for backup
## -
if $manual ; then
host_scripts="${hosts_base_dir}/localhost.sh"
else
host_scripts=`$find $hosts_base_dir -maxdepth 1 -type f -perm -700 | $sort`
fi
found=true
if [ "X$host_scripts" == "X" ]; then
found=false
echo "[Warning]: no hosts for backup found"
echolog "\n[Warning]: no hosts for backup found\n"
fi
if $found ; then
root_begin_h=`date +%H`
root_begin_m=`date +%M`
root_backup_date=`date +"%d.%m.%Y"`
root_begin_timestamp=`date +"%s"`
echolog "\nbegin backup \"$company\" : $root_backup_date ( ${root_begin_h}:${root_begin_m} h )\n"
## - only for output into logfile
## -
echolog "\nfound host scripts:\n"
for host_script in $host_scripts ; do
host_script=`$basename $host_script`
echolog "\t$host_script"
done;
## - for each host exec a script..
## -
for script in $host_scripts ; do
#host=`$basename $script | $cut -d. -f1`
host=`$basename $script | sed -e "s/\.sh$//g"`
$script
done
root_end_h=`$date +%H`
root_end_m=`$date +%M`
root_backup_date=`$date +"%d.%m.%Y"`
root_end_timestamp=`date +"%s"`
root_time=`expr $root_end_timestamp - $root_begin_timestamp`
root_h=`expr $root_time / 60 / 60`
rest_h=`expr $root_time - $root_h \\* 60 \\* 60`
root_m=`expr $rest_h / 60`
root_s=`expr $rest_h - $root_m \\* 60`
if [ $root_m -lt 10 ] ;then
root_m="0$root_m"
fi
if [ $root_s -lt 10 ] ;then
root_s="0$root_s"
fi
if $extern_usb_disk || $extern_sata_disk ; then
## - write backup info to
## -
label=`tune2fs -l $backup_partition | grep -e "Filesystem volume name:" | awk '{print$4}'`
echo >> $info_file
echo -e "\tBackup Host.........: $BACKUP_HOST" >> $info_file
echo -e "\tBackup Date.........: `/bin/date +'%d.%m.%Y'`" >> $info_file
echo -e "\tBackup Time.........: `/bin/date +'%H:%M'` h" >> $info_file
echo >> $info_file
echo -e "\tbackup_disk_label...: $label" >> $info_file
echo >> $info_file
#echo -e "\tBackup Size.........: `du -sm $backup_base_dir | awk '{print$1}' ` MB" >> $info_file
echo >> $info_file
## - save current backup_disk_label
## -
echo "$label" > $disk_label_log_file
if cp $info_file $backup_base_dir > /dev/null ; then
echolog "write backup info to $backup_base_dir/`basename $info_file` .. [ ok ] \n"
else
echolog "[Error]: write backup info to $backup_base_dir/`basename $info_file` ..[ failed ]\n"
fi
rm -f $info_file
fi
echolog "\nend backup \"$company\" : $root_backup_date ( ${root_end_h}:${root_end_m} h ) - duration: ${root_h} h : ${root_m} min : ${root_s} sec\n\n"
if $restart_samba_service ; then
echolog "\n\n[`date`] stoping samba service ..."
$samba_init_script stop > /dev/null
sleep 5
PIDS=""
PIDS=`ps aux | grep "$samba_exe" | grep -v grep | awk '{print$2}'`
while [ "X${PIDS}X" != "XX" ]; do
for pid in $PIDS ; do
kill -9 $pid > /dev/null 2>&1
done
done
echolog "[`date`] starting samba service ..."
$samba_init_script start > /dev/null
sleep 2
NEW_PIDS=`ps aux | grep "$samba_exe" | grep -v grep | awk '{print$2}'`
if [ "X${NEW_PIDS}X" == "XX" ]; then
echolog ""
echolog "\t[Error]: Restarting samba services failed !!"
echo -e "\n\t[Error]: Restarting samba services failed !!\n"
echolog ""
else
PIDS=""
for pid in $NEW_PIDS ; do
PIDS="$PIDS $pid"
done
echolog
echolog "\tI have restarted teh samba services. The new PIDs are $PIDS"
echolog
fi
fi
[ -x $logrotate ] || exit 2
[ -f $logrotate_conf_file ] || exit 3
#export backup_partition logDuration logFile admin_email from_address company
$logrotate $logrotate_conf_file
rm -f $logDuration
fi
retval=2
if $extern_usb_disk || $extern_sata_disk ; then
#$sync
#sleep 10
$umount $backup_partition 2> /dev/null
retval=$?
sleep 2
if [ "$retval" != "0" ] ;then
echo -e "\n[ERROR] something went wrong with unmounting $backup_partition..\n"
else
if ! df | grep "$backup_partition" > /dev/null 2>&1 ;then
declare -i mount_count=`$tune2fs -l $backup_partition | $grep "Mount count" | $cut -d ":" -f 2`
declare -i max_mount_count=`$tune2fs -l $backup_partition | $grep "Maximum mount count" | $cut -d ":" -f 2`
if [ $mount_count -ge $max_mount_count ]; then
$e2fsck -p $backup_partition
if [ $? -gt 0 ]; then
echo -e "\n[ERROR] filecheck on $backup_partition failed.."
fi
$tune2fs -C 0 $backup_partition
fi
fi
fi
else
$umount $backup_partition 2> /dev/null
fi
if $send_reminder ; then
if [ "$retval" = "0" -a "`date +%A`" = "$reminder_day" ] ; then
datum=`date +"%d.%m.%Y"`
label=`tune2fs -l $backup_partition | grep -e "Filesystem volume name:" | awk '{print$4}'`
msg_00="Don't reply on this mail - its autogenerated !!"
msg_01="\t =========================="
msg_02="\t Please change backup disk "
msg_03="Current Disk Label: $label "
msg="${msg_00}\n\n${msg_01}\n${msg_02}\n${msg_01}\n\n${msg_03}\n"
echo -e "To:${reminder_email}\nSubject:Reminder - please change backup-disk\n${msg}\n" | /usr/sbin/sendmail -F "BACKUP $company" -f $from_address $reminder_email
echo -e "To:${admin_email}\nSubject:Reminder - please change backup-disk\n${msg}\n" | /usr/sbin/sendmail -F "BACKUP $company" -f $from_address $admin_email
if [ "X$reminder_email_2" != "X" ]; then
echo -e "To:${reminder_email}\nSubject:Reminder - please change backup-disk\n${msg}\n" | /usr/sbin/sendmail -F "BACKUP $company" -f $from_address $reminder_email_2
fi
fi
fi
if $crypto_backup_device ;then
$cryptsetup luksClose $backup_partition_name
fi

8
rcopy_manual.sh Executable file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
manual=${manual:=true}
script_dir=$(realpath `dirname $0`)
$script_dir/rcopy.sh true
exit $?