1
0
mirror of https://github.com/deajan/obackup.git synced 2024-11-14 11:43:41 +01:00

Code cleanup

This commit is contained in:
deajan 2013-07-20 14:48:06 +02:00
parent 58a601154a
commit ed30118e36

View File

@ -3,7 +3,7 @@
###### Remote (or local) backup script for files & databases ###### Remote (or local) backup script for files & databases
###### (L) 2013 by Orsiris "Ozy" de Jong (www.netpower.fr) ###### (L) 2013 by Orsiris "Ozy" de Jong (www.netpower.fr)
OBACKUP_VERSION=1.84RC1 OBACKUP_VERSION=1.84RC1
OBACKUP_BUILD=1807201301 OBACKUP_BUILD=2007201302
DEBUG=no DEBUG=no
SCRIPT_PID=$$ SCRIPT_PID=$$
@ -261,17 +261,24 @@ function WaitForTaskCompletion
if [ $EXEC_TIME -gt $3 ] && [ $3 != 0 ] if [ $EXEC_TIME -gt $3 ] && [ $3 != 0 ]
then then
LogError "Max hard execution time exceeded for task. Stopping task execution." LogError "Max hard execution time exceeded for task. Stopping task execution."
kill -9 $1 kill -s SIGTERM $1
if [ $? == 0 ] if [ $? == 0 ]
then then
LogError "Task stopped succesfully" LogError "Task stopped succesfully"
else else
LogError "Could not stop task." LogError "Sending SIGTERM to process failed. Trying the hard way."
kill -9 $1
if [ $? != 0 ]
then
LogError "Could not stop task."
fi
fi fi
return 1 return 1
fi fi
fi fi
done done
wait $child_pid
return $?
} }
@ -279,7 +286,7 @@ function WaitForTaskCompletion
function RunLocalCommand function RunLocalCommand
{ {
CheckConnectivity3rdPartyHosts CheckConnectivity3rdPartyHosts
$1 > /dev/shm/obackup_run_local_$SCRIPT_PID & $1 > /dev/shm/obackup_run_local_$SCRIPT_PID 2>&1 &
child_pid=$! child_pid=$!
WaitForTaskCompletion $child_pid 0 $2 WaitForTaskCompletion $child_pid 0 $2
retval=$? retval=$?
@ -290,8 +297,10 @@ function RunLocalCommand
Log "Running command [$1] on local host failed." Log "Running command [$1] on local host failed."
fi fi
Log "Command output:" if [ $verbose -eq 1 ]
Log "$(cat /dev/shm/obackup_run_local_$SCRIPT_PID)" then
Log "Command output:\n$(cat /dev/shm/obackup_run_local_$SCRIPT_PID)"
fi
} }
## Runs remote command $1 and waits for completition in $2 seconds ## Runs remote command $1 and waits for completition in $2 seconds
@ -306,7 +315,7 @@ function RunRemoteCommand
LogError "Connectivity test failed. Cannot run remote command." LogError "Connectivity test failed. Cannot run remote command."
return 1 return 1
else else
$(which ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT "$1" > /dev/shm/obackup_run_remote_$SCRIPT_PID & eval "$SSH_CMD \"$1\" > /dev/shm/obackup_run_remote_$SCRIPT_PID 2>&1 &"
fi fi
child_pid=$! child_pid=$!
WaitForTaskCompletion $child_pid 0 $2 WaitForTaskCompletion $child_pid 0 $2
@ -318,9 +327,9 @@ function RunRemoteCommand
LogError "Running command [$1] failed." LogError "Running command [$1] failed."
fi fi
if [ -f /dev/shm/obackup_run_remote_$SCRIPT_PID ] if [ -f /dev/shm/obackup_run_remote_$SCRIPT_PID ] && [ $verbose -eq 1 ]
then then
Log "Command output: $(cat /dev/shm/obackup_run_remote_$SCRIPT_PID)" Log "Command output:\n$(cat /dev/shm/obackup_run_remote_$SCRIPT_PID)"
fi fi
fi fi
} }
@ -351,48 +360,6 @@ function RunAfterHook
fi fi
} }
function SetCompressionOptions
{
if [ "$COMPRESSION_PROGRAM" == "xz" ] && type -p xz > /dev/null 2>&1
then
COMPRESSION_EXTENSION=.xz
elif [ "$COMPRESSION_PROGRAM" == "lzma" ] && type -p lzma > /dev/null 2>&1
then
COMPRESSION_EXTENSION=.lzma
elif [ "$COMPRESSION_PROGRAM" == "gzip" ] && type -p gzip > /dev/null 2>&1
then
COMPRESSION_EXTENSION=.gz
COMPRESSION_OPTIONS=--rsyncable
else
COMPRESSION_EXTENSION=
fi
if [ "$SSH_COMPRESSION" == "yes" ]
then
SSH_COMP=-C
else
SSH_COMP=
fi
}
function SetSudoOptions
{
## Add this to support prior config files without RSYNC_EXECUTABLE option
if [ "$RSYNC_EXECUTABLE" == "" ]
then
RSYNC_EXECUTABLE=rsync
fi
if [ "$SUDO_EXEC" == "yes" ]
then
RSYNC_PATH="sudo $(which $RSYNC_EXECUTABLE)"
COMMAND_SUDO="sudo"
else
RSYNC_PATH="$(which $RSYNC_EXECUTABLE)"
COMMAND_SUDO=""
fi
}
function CreateLocalStorageDirectories function CreateLocalStorageDirectories
{ {
if [ ! -d $LOCAL_SQL_STORAGE ] && [ "$BACKUP_SQL" != "no" ] if [ ! -d $LOCAL_SQL_STORAGE ] && [ "$BACKUP_SQL" != "no" ]
@ -545,7 +512,7 @@ function ListDatabases
LogError "Connectivity test failed. Stopping current task." LogError "Connectivity test failed. Stopping current task."
Dummy & Dummy &
else else
$(which ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT "mysql -u $SQL_USER -Bse 'SELECT table_schema, round(sum( data_length + index_length ) / 1024) FROM information_schema.TABLES GROUP by table_schema;'" > /dev/shm/obackup_dblist_$SCRIPT_PID & eval "$SSH_CMD \"mysql -u $SQL_USER -Bse 'SELECT table_schema, round(sum( data_length + index_length ) / 1024) FROM information_schema.TABLES GROUP by table_schema;'\" > /dev/shm/obackup_dblist_$SCRIPT_PID &"
fi fi
else else
mysql -u $SQL_USER -Bse 'SELECT table_schema, round(sum( data_length + index_length ) / 1024) FROM information_schema.TABLES GROUP by table_schema;' > /dev/shm/obackup_dblist_$SCRIPT_PID & mysql -u $SQL_USER -Bse 'SELECT table_schema, round(sum( data_length + index_length ) / 1024) FROM information_schema.TABLES GROUP by table_schema;' > /dev/shm/obackup_dblist_$SCRIPT_PID &
@ -618,7 +585,7 @@ function BackupDatabase
LogError "Connectivity test failed. Stopping current task." LogError "Connectivity test failed. Stopping current task."
exit 1 exit 1
fi fi
$(which ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT mysqldump -u $SQL_USER --skip-lock-tables --single-transaction --database $1 | $COMPRESSION_PROGRAM -$COMPRESSION_LEVEL $COMPRESSION_OPTIONS > $LOCAL_SQL_STORAGE/$1.sql$COMPRESSION_EXTENSION eval "$SSH_CMD mysqldump -u $SQL_USER --skip-lock-tables --single-transaction --database $1 | $COMPRESSION_PROGRAM -$COMPRESSION_LEVEL $COMPRESSION_OPTIONS > $LOCAL_SQL_STORAGE/$1.sql$COMPRESSION_EXTENSION"
elif [ "$REMOTE_BACKUP" == "yes" ] && [ "$COMPRESSION_REMOTE" == "yes" ] elif [ "$REMOTE_BACKUP" == "yes" ] && [ "$COMPRESSION_REMOTE" == "yes" ]
then then
CheckConnectivityRemoteHost CheckConnectivityRemoteHost
@ -627,7 +594,7 @@ function BackupDatabase
LogError "Connectivity test failed. Stopping current task." LogError "Connectivity test failed. Stopping current task."
exit 1 exit 1
fi fi
$(which ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT "mysqldump -u $SQL_USER --skip-lock-tables --single-transaction --database $1 | $COMPRESSION_PROGRAM -$COMPRESSION_LEVEL $COMPRESSION_OPTIONS" > $LOCAL_SQL_STORAGE/$1.sql$COMPRESSION_EXTENSION eval "$SSH_CMD \"mysqldump -u $SQL_USER --skip-lock-tables --single-transaction --database $1 | $COMPRESSION_PROGRAM -$COMPRESSION_LEVEL $COMPRESSION_OPTIONS\" > $LOCAL_SQL_STORAGE/$1.sql$COMPRESSION_EXTENSION"
else else
mysqldump -u $SQL_USER --skip-lock-tables --single-transaction --database $1 | $COMPRESSION_PROGRAM -$COMPRESSION_LEVEL $COMPRESSION_OPTIONS > $LOCAL_SQL_STORAGE/$1.sql$COMPRESSION_EXTENSION mysqldump -u $SQL_USER --skip-lock-tables --single-transaction --database $1 | $COMPRESSION_PROGRAM -$COMPRESSION_LEVEL $COMPRESSION_OPTIONS > $LOCAL_SQL_STORAGE/$1.sql$COMPRESSION_EXTENSION
fi fi
@ -673,10 +640,10 @@ function ListDirectories
LogError "Connectivity test failed. Stopping current task." LogError "Connectivity test failed. Stopping current task."
Dummy & Dummy &
else else
$(which ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT "$COMMAND_SUDO find $i/ -mindepth 1 -maxdepth 1 -type d" > /dev/shm/obackup_dirs_recurse_list_$SCRIPT_PID & eval "$SSH_CMD \"$COMMAND_SUDO find $i/ -mindepth 1 -maxdepth 1 -type d\" > /dev/shm/obackup_dirs_recurse_list_$SCRIPT_PID &"
fi fi
else else
$COMMAND_SUDO find $i/ -mindepth 1 -maxdepth 1 -type d > /dev/shm/obackup_dirs_recurse_list_$SCRIPT_PID & c $COMMAND_SUDO find $i/ -mindepth 1 -maxdepth 1 -type d > /dev/shm/obackup_dirs_recurse_list_$SCRIPT_PID &
fi fi
child_pid=$! child_pid=$!
WaitForTaskCompletion $child_pid $SOFT_MAX_EXEC_TIME_FILE_TASK $HARD_MAX_EXEC_TIME_FILE_TASK WaitForTaskCompletion $child_pid $SOFT_MAX_EXEC_TIME_FILE_TASK $HARD_MAX_EXEC_TIME_FILE_TASK
@ -746,7 +713,7 @@ function GetDirectoriesSize
LogError "Connectivity test failed. Stopping current task." LogError "Connectivity test failed. Stopping current task."
Dummy & Dummy &
else else
$(which ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT "echo $dir_list | xargs $COMMAND_SUDO du -cs | tail -n1 | cut -f1" > /dev/shm/obackup_fsize_$SCRIPT_PID & eval "$SSH_CMD \"echo $dir_list | xargs $COMMAND_SUDO du -cs | tail -n1 | cut -f1\" > /dev/shm/obackup_fsize_$SCRIPT_PID &"
fi fi
else else
echo $dir_list | xargs $COMMAND_SUDO du -cs | tail -n1 | cut -f1 > /dev/shm/obackup_fsize_$SCRIPT_PID & echo $dir_list | xargs $COMMAND_SUDO du -cs | tail -n1 | cut -f1 > /dev/shm/obackup_fsize_$SCRIPT_PID &
@ -832,12 +799,12 @@ function Rsync
LogError "Connectivity test failed. Stopping current task." LogError "Connectivity test failed. Stopping current task."
exit 1 exit 1
fi fi
rsync_cmd="$(which $RSYNC_EXECUTABLE) $RSYNC_ARGS --delete $RSYNC_EXCLUDE --rsync-path=\"$RSYNC_PATH\" -e \"$(which ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY -p $REMOTE_PORT\" \"$REMOTE_USER@$REMOTE_HOST:$1\" \"$local_file_storage_path\" > /dev/shm/obackup_rsync_output_$SCRIPT_PID 2>&1" rsync_cmd="$(which $RSYNC_EXECUTABLE) $RSYNC_ARGS --delete $RSYNC_EXCLUDE --rsync-path=\"$RSYNC_PATH\" -e \"$RSYNC_SSH_CMD\" \"$REMOTE_USER@$REMOTE_HOST:$1\" \"$local_file_storage_path\" > /dev/shm/obackup_rsync_output_$SCRIPT_PID 2>&1"
else else
rsync_cmd="$(which $RSYNC_EXECUTABLE) $RSYNC_ARGS --delete $RSYNC_EXCLUDE --rsync-path=\"$RSYNC_PATH\" \"$1\" \"$local_file_storage_path\" > /dev/shm/obackup_rsync_output_$SCRIPT_PID 2>&1" rsync_cmd="$(which $RSYNC_EXECUTABLE) $RSYNC_ARGS --delete $RSYNC_EXCLUDE --rsync-path=\"$RSYNC_PATH\" \"$1\" \"$local_file_storage_path\" > /dev/shm/obackup_rsync_output_$SCRIPT_PID 2>&1"
fi fi
#### Eval is used so the full command is processed without bash adding single quotes round variables #### Eval is used so the full command is processed without bash adding single quotes round variables
if [ "$DEBUG" == "yes" ] if [ $verbose -eq 1 ]
then then
Log $rsync_cmd Log $rsync_cmd
fi fi
@ -945,15 +912,52 @@ function Init
LOG_FILE=/var/log/obackup_$OBACKUP_VERSION-$BACKUP_ID.log LOG_FILE=/var/log/obackup_$OBACKUP_VERSION-$BACKUP_ID.log
MAIL_ALERT_MSG="Warning: Execution of obackup instance $BACKUP_ID (pid $SCRIPT_PID) as $LOCAL_USER@$LOCAL_HOST produced errors." MAIL_ALERT_MSG="Warning: Execution of obackup instance $BACKUP_ID (pid $SCRIPT_PID) as $LOCAL_USER@$LOCAL_HOST produced errors."
## Set SSH command
if [ "$SSH_COMPRESSION" == "yes" ]
then
SSH_COMP=-C
else
SSH_COMP=
fi
SSH_CMD="$(which ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT"
RSYNC_SSH_CMD="$(which ssh) $SSH_COMP -i $SSH_RSA_PRIVATE_KEY -p $REMOTE_PORT"
## Support for older config files without RSYNC_EXECUTABLE option
if [ "$RSYNC_EXECUTABLE" == "" ]
then
RSYNC_EXECUTABLE=rsync
fi
## Sudo execution option
if [ "$SUDO_EXEC" == "yes" ]
then
RSYNC_PATH="sudo $(which $RSYNC_EXECUTABLE)"
COMMAND_SUDO="sudo"
else
RSYNC_PATH="$(which $RSYNC_EXECUTABLE)"
COMMAND_SUDO=""
fi
## Set compression executable and extension
if [ "$COMPRESSION_PROGRAM" == "xz" ] && type -p xz > /dev/null 2>&1
then
COMPRESSION_EXTENSION=.xz
elif [ "$COMPRESSION_PROGRAM" == "lzma" ] && type -p lzma > /dev/null 2>&1
then
COMPRESSION_EXTENSION=.lzma
elif [ "$COMPRESSION_PROGRAM" == "gzip" ] && type -p gzip > /dev/null 2>&1
then
COMPRESSION_EXTENSION=.gz
COMPRESSION_OPTIONS=--rsyncable
else
COMPRESSION_EXTENSION=
fi
} }
function DryRun function DryRun
{ {
Log "/!\ DRY RUN as $LOCAL_USER@$LOCAL_HOST (PID $SCRIPT_PID)" Log "/!\ DRY RUN as $LOCAL_USER@$LOCAL_HOST (PID $SCRIPT_PID)"
SetCompressionOptions
SetSudoOptions
if [ "$BACKUP_SQL" != "no" ] if [ "$BACKUP_SQL" != "no" ]
then then
ListDatabases ListDatabases
@ -976,9 +980,6 @@ function Main
{ {
Log "Backup launched as $LOCAL_USER@$LOCAL_HOST (PID $SCRIPT_PID)" Log "Backup launched as $LOCAL_USER@$LOCAL_HOST (PID $SCRIPT_PID)"
SetCompressionOptions
SetSudoOptions
if [ "$BACKUP_SQL" != "no" ] if [ "$BACKUP_SQL" != "no" ]
then then
ListDatabases ListDatabases
@ -1017,7 +1018,7 @@ function Usage
{ {
echo "Obackup $OBACKUP_VERSION $OBACKUP_BUILD" echo "Obackup $OBACKUP_VERSION $OBACKUP_BUILD"
echo "" echo ""
echo "usage: obackup backup_name [--dry] [--silent]" echo "usage: obackup backup_name [--dry] [--silent] [--verbose]"
echo "" echo ""
echo "--dry: will run obackup without actually doing anything, just testing" echo "--dry: will run obackup without actually doing anything, just testing"
echo "--silent: will run obackup without any output to stdout, usefull for cron backups" echo "--silent: will run obackup without any output to stdout, usefull for cron backups"
@ -1027,6 +1028,7 @@ function Usage
# Command line argument flags # Command line argument flags
dryrun=0 dryrun=0
silent=0 silent=0
verbose=0
# Alert flags # Alert flags
soft_alert_total=0 soft_alert_total=0
error_alert=0 error_alert=0
@ -1045,7 +1047,10 @@ do
--silent) --silent)
silent=1 silent=1
;; ;;
--help|-h) --verbose)
verbose=1
;;
--help|-h|--version|-v)
Usage Usage
;; ;;
esac esac