This commit is contained in:
koroshiya 2015-11-11 02:51:57 +00:00
commit 0ed511e8fa
1 changed files with 146 additions and 64 deletions

104
osync.sh
View File

@ -923,7 +923,15 @@ function _CheckDiskSpaceLocal {
Logger "Checking minimum disk space in [$replica_path]." "NOTICE"
local initiator_space=$(df -P "$replica_path" | tail -1 | awk '{print $4}')
local isbusybox=$(ls --help 2>&1 | grep BusyBox)
local pFlag=""
if [[ $isbusybox != *"BusyBox"* ]]; then
pFlag="-P "
fi
local initiator_space=$(df $pFlag"$replica_path" | tail -1 | awk '{print $4}')
initiator_space=$(GetSpace "$initiator_space")
if [ $initiator_space -lt $MINIMUM_SPACE ]; then
Logger "There is not enough free space on initiator [$initiator_space KB]." "WARN"
fi
@ -938,7 +946,13 @@ function _CheckDiskSpaceRemote {
CheckConnectivity3rdPartyHosts
CheckConnectivityRemoteHost
cmd=$SSH_CMD' "'$COMMAND_SUDO' df -P \"'$replica_path'\"" > "'$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID'" 2>&1'
local isbusybox=$(echo "ls --help 2>&1 | grep BusyBox" | $SSH_CMD)
local pFlag=""
if [[ $isbusybox != *"BusyBox"* ]]; then
pFlag="-P "
fi
cmd=$SSH_CMD' "'$COMMAND_SUDO' df $pFlag\"'$replica_path'\"" > "'$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID'" 2>&1'
Logger "cmd: $cmd" "DEBUG"
eval "$cmd" &
WaitForTaskCompletion $! 0 1800 $FUNCNAME
@ -947,6 +961,8 @@ function _CheckDiskSpaceRemote {
Logger "Command output:\n$(cat $RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID)" "NOTICE"
else
local target_space=$(cat $RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID | tail -1 | awk '{print $4}')
target_space=$(GetSpace "$target_space")
if [ $target_space -lt $MINIMUM_SPACE ]; then
Logger "There is not enough free space on target [$replica_path]." "WARN"
fi
@ -1751,14 +1767,32 @@ function _SoftDeleteLocal {
Logger "Removing files older than $change_time days on $replica_type replica." "NOTICE"
fi
if [ $_VERBOSE -eq 1 ]; then
# Cannot launch log function from xargs, ugly hack
$FIND_CMD "$replica_deletion_path/" -type f -ctime +$change_time -print0 | xargs -0 -I {} echo "Will delete file {}" > "$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID"
Logger "Command output:\n$(cat $RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID)" "NOTICE"
$FIND_CMD "$replica_deletion_path/" -type d -empty -ctime +$change_time -print0 | xargs -0 -I {} echo "Will delete directory {}" > "$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID"
Logger "Command output:\n$(cat $RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID)" "NOTICE"
$FIND_CMD "$replica_deletion_path/" -type f -print0 | while read filename
do
if DaysHavePassed $filename $change_time; then
Logger "Command output:\nWill delete file $filename" "NOTICE"
fi
done
$FIND_CMD "$replica_deletion_path/" -type d -empty -print0 | while read filename
do
if DaysHavePassed $filename $change_time; then
Logger "Command output:\nWill delete directory $filename" "NOTICE"
fi
done
fi
if [ $_DRYRUN -ne 1 ]; then
$FIND_CMD "$replica_deletion_path/" -type f -ctime +$change_time -print0 | xargs -0 -I {} rm -f "{}" && $FIND_CMD "$replica_deletion_path/" -type d -empty -ctime +$change_time -print0 | xargs -0 -I {} rm -rf "{}" > "$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID" 2>&1 &
$FIND_CMD "$replica_deletion_path/" -type f -print0 | while read filename
do
if DaysHavePassed $filename $change_time; then
rm -f "$filename" > "$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID" 2>&1 &
fi
done
$FIND_CMD "$replica_deletion_path/" -type d -empty -print0 | while read filename
do
if DaysHavePassed $filename $change_time; then
rm -rf "$filename" > "$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID" 2>&1 &
fi
done
else
Dummy &
fi
@ -1791,15 +1825,14 @@ function _SoftDeleteRemote {
fi
if [ $_VERBOSE -eq 1 ]; then
# Cannot launch log function from xargs, ugly hack
cmd=$SSH_CMD' "if [ -w \"'$replica_deletion_path'\" ]; then '$COMMAND_SUDO' '$REMOTE_FIND_CMD' \"'$replica_deletion_path'/\" -type f -ctime +'$change_time' -print0 | xargs -0 -I {} echo Will delete file {} && '$REMOTE_FIND_CMD' \"'$replica_deletion_path'/\" -type d -empty -ctime '$change_time' -print0 | xargs -0 -I {} echo Will delete directory {}; fi" > "'$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID'" 2>&1'
cmd=$SSH_CMD' "if [ -w \"'$replica_deletion_path'\" ]; then '$COMMAND_SUDO' '$REMOTE_FIND_CMD' \"'$replica_deletion_path'/\" -type f -print0 | while read filename; do; if DaysHavePassed $filename $change_time; then echo Will delete file $filename; fi; done && '$REMOTE_FIND_CMD' \"'$replica_deletion_path'/\" -type d -empty -print0 | while read filename; do; if DaysHavePassed $filename $change_time; then echo Will delete directory $filename; fi; done; fi" > "'$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID'" 2>&1'
Logger "cmd: $cmd" "DEBUG"
eval "$cmd" &
Logger "Command output:\n$(cat $RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID)" "NOTICE"
fi
if [ $_DRYRUN -ne 1 ]; then
cmd=$SSH_CMD' "if [ -w \"'$replica_deletion_path'\" ]; then '$COMMAND_SUDO' '$REMOTE_FIND_CMD' \"'$replica_deletion_path'/\" -type f -ctime +'$change_time' -print0 | xargs -0 -I {} rm -f \"{}\" && '$REMOTE_FIND_CMD' \"'$replica_deletion_path'/\" -type d -empty -ctime '$change_time' -print0 | xargs -0 -I {} rm -rf \"{}\"; fi" > "'$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID'" 2>&1'
cmd=$SSH_CMD' "if [ -w \"'$replica_deletion_path'\" ]; then '$COMMAND_SUDO' '$REMOTE_FIND_CMD' \"'$replica_deletion_path'/\" -type f -print0 | while read filename; do; if DaysHavePassed $filename $change_time; then rm -f \"$filename\"; fi; done && '$REMOTE_FIND_CMD' \"'$replica_deletion_path'/\" -type d -empty -print0 | while read filename; do; if DaysHavePassed $filename $change_time; then rm -rf \"$filename\"; fi; done; fi" > "'$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID'" 2>&1'
Logger "cmd: $cmd" "DEBUG"
eval "$cmd" &
else
@ -2201,6 +2234,55 @@ function SyncOnChanges {
}
#Floating point sizes may not be 100% accurate, as Busybox may round the number
function GetSpace {
__CheckArguments 0 $# $FUNCNAME "$*" #__WITH_PARANOIA_DEBUG
local num=$1
local unit="${num: -1}"
local size="${num:0:-1}"
case $unit in
"K")
awk -v size="$size" 'BEGIN{printf "%.0f", size * 1024}'
;;
"M")
awk -v size="$size" 'BEGIN{printf "%.0f", size * 1024 * 1024}'
;;
"G")
awk -v size="$size" 'BEGIN{printf "%.0f", size * 1024 * 1024 * 1024}'
;;
"T")
awk -v size="$size" 'BEGIN{printf "%.0f", size * 1024 * 1024 * 1024 * 1024}'
;;
*)
echo $num
;;
esac
return 1
}
function DaysHavePassed {
__CheckArguments 0 $# $FUNCNAME "$*" #__WITH_PARANOIA_DEBUG
local filename=$1
local days=$2
local seconds_since_epoch
seconds_since_epoch=$(date +%s)
local file_last_changed
file_last_changed=$(stat -c %Z -- "$filename")
file_last_changed=$(($file_last_changed - $seconds_since_epoch))
file_last_changed=$(($file_last_changed / 86400000)) #Days since last changed
if [[ $days -gt $file_last_changed ]]; then
return 1
else
return 0
fi
}
# Comand line argument flags
_DRYRUN=0
_SILENT=0