Fixed some glitches

This commit is contained in:
deajan 2013-08-24 19:41:18 +02:00
parent 84a2b3a532
commit cfb151da4e
2 changed files with 43 additions and 71 deletions

View File

@ -2,7 +2,7 @@
###### Remote (or local) backup script for files & databases ###### Remote (or local) backup script for files & databases
###### (L) 2013 by Ozy de Jong (www.badministrateur.com) ###### (L) 2013 by Ozy de Jong (www.badministrateur.com)
###### Config file rev 1808201301 ###### Config file rev 2408201301
## Backup identification, any string you want ## Backup identification, any string you want
BACKUP_ID="your backup name" BACKUP_ID="your backup name"
@ -44,7 +44,7 @@ SSH_COMPRESSION=yes
## Check for connectivity to remote host before launching remote backup tasks. Be sure the hosts responds to ping. Failing to ping will skip current task. ## Check for connectivity to remote host before launching remote backup tasks. Be sure the hosts responds to ping. Failing to ping will skip current task.
REMOTE_HOST_PING=yes REMOTE_HOST_PING=yes
## Check for internet access by pinging one or more 3rd party hosts before remote backup tasks. Leave empty if you don't want this check to be be performed. Failing to ping will skip current task. ## Check for internet access by pinging one or more 3rd party hosts before remote backup tasks. Leave empty if you don't want this check to be be performed. Failing to ping will skip current task.
REMOTE_3RD_PARTY_HOST="www.kernel.org www.google.com" REMOTE_3RD_PARTY_HOSTS="www.kernel.org www.google.com"
## Databases options ## Databases options
SQL_USER=backupuser SQL_USER=backupuser

View File

@ -2,8 +2,8 @@
###### 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.84RC2 OBACKUP_VERSION=1.84preRC3
OBACKUP_BUILD=1808201302 OBACKUP_BUILD=2408201302
DEBUG=no DEBUG=no
SCRIPT_PID=$$ SCRIPT_PID=$$
@ -151,8 +151,6 @@ function CleanUp
function SendAlert function SendAlert
{ {
CheckConnectivityRemoteHost
CheckConnectivity3rdPartyHosts
cat "$LOG_FILE" | gzip -9 > /tmp/obackup_lastlog.gz cat "$LOG_FILE" | gzip -9 > /tmp/obackup_lastlog.gz
if type -p mutt > /dev/null 2>&1 if type -p mutt > /dev/null 2>&1
then then
@ -287,7 +285,6 @@ function WaitForTaskCompletion
## Runs local command $1 and waits for completition in $2 seconds ## Runs local command $1 and waits for completition in $2 seconds
function RunLocalCommand function RunLocalCommand
{ {
CheckConnectivity3rdPartyHosts
$1 > /dev/shm/obackup_run_local_$SCRIPT_PID 2>&1 & $1 > /dev/shm/obackup_run_local_$SCRIPT_PID 2>&1 &
child_pid=$! child_pid=$!
WaitForTaskCompletion $child_pid 0 $2 WaitForTaskCompletion $child_pid 0 $2
@ -309,30 +306,27 @@ function RunLocalCommand
function RunRemoteCommand function RunRemoteCommand
{ {
CheckConnectivity3rdPartyHosts CheckConnectivity3rdPartyHosts
if [ "$REMOTE_BACKUP" == "yes" ] CheckConnectivityRemoteHost
if [ $? != 0 ]
then then
CheckConnectivityRemoteHost LogError "Connectivity test failed. Cannot run remote command."
if [ $? != 0 ] return 1
then else
LogError "Connectivity test failed. Cannot run remote command." eval "$SSH_CMD \"$1\" > /dev/shm/obackup_run_remote_$SCRIPT_PID 2>&1 &"
return 1 fi
else child_pid=$!
eval "$SSH_CMD \"$1\" > /dev/shm/obackup_run_remote_$SCRIPT_PID 2>&1 &" WaitForTaskCompletion $child_pid 0 $2
fi retval=$?
child_pid=$! if [ $retval -eq 0 ]
WaitForTaskCompletion $child_pid 0 $2 then
retval=$? Log "Running command [$1] succeded."
if [ $retval -eq 0 ] else
then LogError "Running command [$1] failed."
Log "Running command [$1] succeded." fi
else
LogError "Running command [$1] failed." if [ -f /dev/shm/obackup_run_remote_$SCRIPT_PID ] && [ $verbose -eq 1 ]
fi then
Log "Command output:\n$(cat /dev/shm/obackup_run_remote_$SCRIPT_PID)"
if [ -f /dev/shm/obackup_run_remote_$SCRIPT_PID ] && [ $verbose -eq 1 ]
then
Log "Command output:\n$(cat /dev/shm/obackup_run_remote_$SCRIPT_PID)"
fi
fi fi
} }
@ -449,7 +443,7 @@ function CheckSpaceRequirements
function CheckTotalExecutionTime function CheckTotalExecutionTime
{ {
#### Check if max execution time of whole script as been reached #### Check if max execution time of whole script as been reached
if [ $SECONDS -gt $SOFT_MAX_EXEC_TIME_TOTAL ] if [ $SECONDS -gt $SOFT_MAX_EXEC_TIME_TOTAL ]
then then
if [ $soft_alert_total -eq 0 ] if [ $soft_alert_total -eq 0 ]
@ -483,7 +477,7 @@ function CheckConnectivity3rdPartyHosts
if [ "$REMOTE_3RD_PARTY_HOSTS" != "" ] if [ "$REMOTE_3RD_PARTY_HOSTS" != "" ]
then then
remote_3rd_party_success=0 remote_3rd_party_success=0
for $i in $REMOTE_3RD_PARTY_HOSTS for i in $REMOTE_3RD_PARTY_HOSTS
do do
ping $i -c 2 > /dev/null 2>&1 ping $i -c 2 > /dev/null 2>&1
if [ $? != 0 ] if [ $? != 0 ]
@ -506,16 +500,10 @@ function ListDatabases
SECONDS_BEGIN=$SECONDS SECONDS_BEGIN=$SECONDS
Log "Listing databases." Log "Listing databases."
CheckConnectivity3rdPartyHosts CheckConnectivity3rdPartyHosts
if [ "$REMOTE_BACKUP" == "yes" ] CheckConnectivityRemoteHost
if [ "$REMOTE_BACKUP" != "no" ]
then then
CheckConnectivityRemoteHost 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 &"
if [ $? != 0 ]
then
LogError "Connectivity test failed. Stopping current task."
Dummy &
else
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
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 &
fi fi
@ -631,7 +619,7 @@ function ListDirectories
Log "Listing directories to backup." Log "Listing directories to backup."
OLD_IFS=$IFS OLD_IFS=$IFS
IFS=$PATH_SEPARATOR_CHAR IFS=$PATH_SEPARATOR_CHAR
for i in $DIRECTORIES_RECURSE_LIST for dir in $DIRECTORIES_RECURSE_LIST
do do
CheckConnectivity3rdPartyHosts CheckConnectivity3rdPartyHosts
if [ "$REMOTE_BACKUP" == "yes" ] if [ "$REMOTE_BACKUP" == "yes" ]
@ -642,24 +630,24 @@ function ListDirectories
LogError "Connectivity test failed. Stopping current task." LogError "Connectivity test failed. Stopping current task."
Dummy & Dummy &
else else
eval "$SSH_CMD \"$COMMAND_SUDO find $i/ -mindepth 1 -maxdepth 1 -type d\" > /dev/shm/obackup_dirs_recurse_list_$SCRIPT_PID &" eval "$SSH_CMD \"$COMMAND_SUDO find $dir/ -mindepth 1 -maxdepth 1 -type d\" > /dev/shm/obackup_dirs_recurse_list_$SCRIPT_PID &"
fi fi
else else
c $COMMAND_SUDO find $i/ -mindepth 1 -maxdepth 1 -type d > /dev/shm/obackup_dirs_recurse_list_$SCRIPT_PID & $COMMAND_SUDO find $dir/ -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
retval=$? retval=$?
if [ $retval != 0 ] if [ $retval != 0 ]
then then
LogError "Could not enumerate recursive directories in $i." LogError "Could not enumerate recursive directories in $dir."
if [ -f /dev/shm/obackup_dirs_recurse_list_$SCRIPT_PID ] if [ -f /dev/shm/obackup_dirs_recurse_list_$SCRIPT_PID ]
then then
LogError "Command output: $(cat /dev/shm/obackup_dirs_recurse_list_$SCRIPT_PID)" LogError "Command output: $(cat /dev/shm/obackup_dirs_recurse_list_$SCRIPT_PID)"
fi fi
return 1 return 1
else else
Log "Listing of recursive directories succeeded for $i." Log "Listing of recursive directories succeeded for $dir."
fi fi
while read line while read line
@ -688,13 +676,13 @@ c $COMMAND_SUDO find $i/ -mindepth 1 -maxdepth 1 -type d > /dev/shm/obackup_di
done done
DIRECTORIES_TO_BACKUP_RECURSE=$DIRECTORIES_TO_BACKUP DIRECTORIES_TO_BACKUP_RECURSE=$DIRECTORIES_TO_BACKUP
for i in $DIRECTORIES_SIMPLE_LIST for dir in $DIRECTORIES_SIMPLE_LIST
do do
if [ "$DIRECTORIES_TO_BACKUP" == "" ] if [ "$DIRECTORIES_TO_BACKUP" == "" ]
then then
DIRECTORIES_TO_BACKUP="'$i'" DIRECTORIES_TO_BACKUP="'$dir'"
else else
DIRECTORIES_TO_BACKUP="$DIRECTORIES_TO_BACKUP$PATH_SEPARATOR_CHAR'$i'" DIRECTORIES_TO_BACKUP="$DIRECTORIES_TO_BACKUP$PATH_SEPARATOR_CHAR'$dir'"
fi fi
done done
@ -976,28 +964,6 @@ function Init
fi fi
} }
function DryRun
{
Log "/!\ DRY RUN as $LOCAL_USER@$LOCAL_HOST (PID $SCRIPT_PID)"
if [ "$BACKUP_SQL" != "no" ]
then
ListDatabases
fi
if [ "$BACKUP_FILES" != "no" ]
then
ListDirectories
GetDirectoriesSize
fi
Log "DB backup list: $DATABASES_TO_BACKUP"
Log "DB exclude list: $DATABASES_EXCLUDED_LIST"
Log "Dirs backup list: $DIRECTORIES_TO_BACKUP"
Log "Dirs exclude list: $DIRECTORIES_EXCLUDED_LIST"
CheckSpaceRequirements
}
function Main function Main
{ {
if [ "$BACKUP_SQL" != "no" ] if [ "$BACKUP_SQL" != "no" ]
@ -1012,7 +978,13 @@ function Main
if [ $dryrun -ne 1 ] if [ $dryrun -ne 1 ]
then then
CreateLocalStorageDirectories CreateLocalStorageDirectories
else
Log "DB backup list: $DATABASES_TO_BACKUP"
Log "DB exclude list: $DATABASES_EXCLUDED_LIST"
Log "Dirs backup list: $DIRECTORIES_TO_BACKUP"
Log "Dirs exclude list: $DIRECTORIES_EXCLUDED_LIST"
fi fi
CheckSpaceRequirements CheckSpaceRequirements
# Actual backup process # Actual backup process