Fixed df commands on Busybox.

Also replaced some spaces with tabs, as per the general coding style,
and replaced the remaining xargs instances.
This commit is contained in:
koroshiya 2015-11-10 12:58:40 +11:00
parent 4b43fd4fb1
commit 7d3c0e21d8
1 changed files with 101 additions and 59 deletions

160
osync.sh
View File

@ -690,71 +690,71 @@ function __CheckArguments {
###### realpath.sh implementation from https://github.com/mkropat/sh-realpath ###### realpath.sh implementation from https://github.com/mkropat/sh-realpath
realpath() { realpath() {
canonicalize_path "$(resolve_symlinks "$1")" canonicalize_path "$(resolve_symlinks "$1")"
} }
resolve_symlinks() { resolve_symlinks() {
_resolve_symlinks "$1" _resolve_symlinks "$1"
} }
_resolve_symlinks() { _resolve_symlinks() {
_assert_no_path_cycles "$@" || return _assert_no_path_cycles "$@" || return
local dir_context path local dir_context path
path=$(readlink -- "$1") path=$(readlink -- "$1")
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
dir_context=$(dirname -- "$1") dir_context=$(dirname -- "$1")
_resolve_symlinks "$(_prepend_dir_context_if_necessary "$dir_context" "$path")" "$@" _resolve_symlinks "$(_prepend_dir_context_if_necessary "$dir_context" "$path")" "$@"
else else
printf '%s\n' "$1" printf '%s\n' "$1"
fi fi
} }
_prepend_dir_context_if_necessary() { _prepend_dir_context_if_necessary() {
if [ "$1" = . ]; then if [ "$1" = . ]; then
printf '%s\n' "$2" printf '%s\n' "$2"
else else
_prepend_path_if_relative "$1" "$2" _prepend_path_if_relative "$1" "$2"
fi fi
} }
_prepend_path_if_relative() { _prepend_path_if_relative() {
case "$2" in case "$2" in
/* ) printf '%s\n' "$2" ;; /* ) printf '%s\n' "$2" ;;
* ) printf '%s\n' "$1/$2" ;; * ) printf '%s\n' "$1/$2" ;;
esac esac
} }
_assert_no_path_cycles() { _assert_no_path_cycles() {
local target path local target path
target=$1 target=$1
shift shift
for path in "$@"; do for path in "$@"; do
if [ "$path" = "$target" ]; then if [ "$path" = "$target" ]; then
return 1 return 1
fi fi
done done
} }
canonicalize_path() { canonicalize_path() {
if [ -d "$1" ]; then if [ -d "$1" ]; then
_canonicalize_dir_path "$1" _canonicalize_dir_path "$1"
else else
_canonicalize_file_path "$1" _canonicalize_file_path "$1"
fi fi
} }
_canonicalize_dir_path() { _canonicalize_dir_path() {
(cd "$1" 2>/dev/null && pwd -P) (cd "$1" 2>/dev/null && pwd -P)
} }
_canonicalize_file_path() { _canonicalize_file_path() {
local dir file local dir file
dir=$(dirname -- "$1") dir=$(dirname -- "$1")
file=$(basename -- "$1") file=$(basename -- "$1")
(cd "$dir" 2>/dev/null && printf '%s/%s\n' "$(pwd -P)" "$file") (cd "$dir" 2>/dev/null && printf '%s/%s\n' "$(pwd -P)" "$file")
} }
# Optionally, you may also want to include: # Optionally, you may also want to include:
@ -762,41 +762,41 @@ _canonicalize_file_path() {
### readlink emulation ### ### readlink emulation ###
readlink() { readlink() {
if _has_command readlink; then if _has_command readlink; then
_system_readlink "$@" _system_readlink "$@"
else else
_emulated_readlink "$@" _emulated_readlink "$@"
fi fi
} }
_has_command() { _has_command() {
hash -- "$1" 2>/dev/null hash -- "$1" 2>/dev/null
} }
_system_readlink() { _system_readlink() {
command readlink "$@" command readlink "$@"
} }
_emulated_readlink() { _emulated_readlink() {
if [ "$1" = -- ]; then if [ "$1" = -- ]; then
shift shift
fi fi
_gnu_stat_readlink "$@" || _bsd_stat_readlink "$@" _gnu_stat_readlink "$@" || _bsd_stat_readlink "$@"
} }
_gnu_stat_readlink() { _gnu_stat_readlink() {
local output local output
output=$(stat -c %N -- "$1" 2>/dev/null) && output=$(stat -c %N -- "$1" 2>/dev/null) &&
printf '%s\n' "$output" | printf '%s\n' "$output" |
sed "s/^[^]* -> \(.*\)/\1/ sed "s/^[^]* -> \(.*\)/\1/
s/^'[^']*' -> '\(.*\)'/\1/" s/^'[^']*' -> '\(.*\)'/\1/"
# FIXME: handle newlines # FIXME: handle newlines
} }
_bsd_stat_readlink() { _bsd_stat_readlink() {
stat -f %Y -- "$1" 2>/dev/null stat -f %Y -- "$1" 2>/dev/null
} }
###### Osync specific functions (non shared) ###### Osync specific functions (non shared)
@ -923,7 +923,15 @@ function _CheckDiskSpaceLocal {
Logger "Checking minimum disk space in [$replica_path]." "NOTICE" 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 if [ $initiator_space -lt $MINIMUM_SPACE ]; then
Logger "There is not enough free space on initiator [$initiator_space KB]." "WARN" Logger "There is not enough free space on initiator [$initiator_space KB]." "WARN"
fi fi
@ -938,7 +946,13 @@ function _CheckDiskSpaceRemote {
CheckConnectivity3rdPartyHosts CheckConnectivity3rdPartyHosts
CheckConnectivityRemoteHost 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" Logger "cmd: $cmd" "DEBUG"
eval "$cmd" & eval "$cmd" &
WaitForTaskCompletion $! 0 1800 $FUNCNAME WaitForTaskCompletion $! 0 1800 $FUNCNAME
@ -947,6 +961,8 @@ function _CheckDiskSpaceRemote {
Logger "Command output:\n$(cat $RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID)" "NOTICE" Logger "Command output:\n$(cat $RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID)" "NOTICE"
else else
local target_space=$(cat $RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID | tail -1 | awk '{print $4}') 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 if [ $target_space -lt $MINIMUM_SPACE ]; then
Logger "There is not enough free space on target [$replica_path]." "WARN" Logger "There is not enough free space on target [$replica_path]." "WARN"
fi fi
@ -1751,13 +1767,12 @@ function _SoftDeleteLocal {
Logger "Removing files older than $change_time days on $replica_type replica." "NOTICE" Logger "Removing files older than $change_time days on $replica_type replica." "NOTICE"
fi fi
if [ $_VERBOSE -eq 1 ]; then if [ $_VERBOSE -eq 1 ]; then
# Cannot launch log function from xargs, ugly hack
$FIND_CMD "$replica_deletion_path/" -type f -mtime +$change_time -print0 | while read filename; do Logger "Command output:\nWill delete file $filename" "NOTICE"; done $FIND_CMD "$replica_deletion_path/" -type f -mtime +$change_time -print0 | while read filename; do Logger "Command output:\nWill delete file $filename" "NOTICE"; done
$FIND_CMD "$replica_deletion_path/" -type d -empty -mtime +$change_time -print0 | while read filename; do Logger "Command output:\nWill delete directory $filename" "NOTICE"; done $FIND_CMD "$replica_deletion_path/" -type d -empty -mtime +$change_time -print0 | while read filename; do Logger "Command output:\nWill delete directory $filename" "NOTICE"; done
fi fi
if [ $_DRYRUN -ne 1 ]; then if [ $_DRYRUN -ne 1 ]; then
$FIND_CMD "$replica_deletion_path/" -type f -mtime +$change_time -print0 | while read filename; do rm -f "$filename" > "$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID" 2>&1 &; done $FIND_CMD "$replica_deletion_path/" -type f -mtime +$change_time -print0 | while read filename; do rm -f "$filename" > "$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID" 2>&1 & done
$FIND_CMD "$replica_deletion_path/" -type d -empty -mtime +$change_time -print0 | while read filename; do rm -rf "$filename" > "$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID" 2>&1 &; done $FIND_CMD "$replica_deletion_path/" -type d -empty -mtime +$change_time -print0 | while read filename; do rm -rf "$filename" > "$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID" 2>&1 & done
else else
Dummy & Dummy &
fi fi
@ -1790,15 +1805,14 @@ function _SoftDeleteRemote {
fi fi
if [ $_VERBOSE -eq 1 ]; then 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 -mtime +'$change_time' -print0 | while read filename; do echo Will delete file $filename; done && '$REMOTE_FIND_CMD' \"'$replica_deletion_path'/\" -type d -empty -mtime '$change_time' -print0 | while read filename; do echo Will delete directory $filename; done; 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 -mtime +'$change_time' -print0 | xargs -0 -I {} echo Will delete file {} && '$REMOTE_FIND_CMD' \"'$replica_deletion_path'/\" -type d -empty -mtime '$change_time' -print0 | xargs -0 -I {} echo Will delete directory {}; fi" > "'$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID'" 2>&1'
Logger "cmd: $cmd" "DEBUG" Logger "cmd: $cmd" "DEBUG"
eval "$cmd" & eval "$cmd" &
Logger "Command output:\n$(cat $RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID)" "NOTICE" Logger "Command output:\n$(cat $RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID)" "NOTICE"
fi fi
if [ $_DRYRUN -ne 1 ]; then if [ $_DRYRUN -ne 1 ]; then
cmd=$SSH_CMD' "if [ -w \"'$replica_deletion_path'\" ]; then '$COMMAND_SUDO' '$REMOTE_FIND_CMD' \"'$replica_deletion_path'/\" -type f -mtime +'$change_time' -print0 | xargs -0 -I {} rm -f \"{}\" && '$REMOTE_FIND_CMD' \"'$replica_deletion_path'/\" -type d -empty -mtime '$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 -mtime +'$change_time' -print0 | while read filename; do rm -f \"$filename\"; done && '$REMOTE_FIND_CMD' \"'$replica_deletion_path'/\" -type d -empty -mtime '$change_time' -print0 | while read filename; do rm -rf \"$filename\"; done; fi" > "'$RUN_DIR/osync.$FUNCNAME.$SCRIPT_PID'" 2>&1'
Logger "cmd: $cmd" "DEBUG" Logger "cmd: $cmd" "DEBUG"
eval "$cmd" & eval "$cmd" &
else else
@ -2131,18 +2145,18 @@ function Usage {
echo -e "\e[41mWARNING: This is an unstable dev build\e[0m" echo -e "\e[41mWARNING: This is an unstable dev build\e[0m"
echo "You may use Osync with a full blown configuration file, or use its default options for quick command line sync." echo "You may use Osync with a full blown configuration file, or use its default options for quick command line sync."
echo "Usage: osync.sh /path/to/config/file [OPTIONS]" echo "Usage: osync.sh /path/to/config/file [OPTIONS]"
echo "or osync.sh --initiator=/path/to/initiator/replica --target=/path/to/target/replica [OPTIONS] [QUICKSYNC OPTIONS]" echo "or osync.sh --initiator=/path/to/initiator/replica --target=/path/to/target/replica [OPTIONS] [QUICKSYNC OPTIONS]"
echo "or osync.sh --initiator=/path/to/initiator/replica --target=ssh://[backupuser]@remotehost.com[:portnumber]//path/to/target/replica [OPTIONS] [QUICKSYNC OPTIONS]" echo "or osync.sh --initiator=/path/to/initiator/replica --target=ssh://[backupuser]@remotehost.com[:portnumber]//path/to/target/replica [OPTIONS] [QUICKSYNC OPTIONS]"
echo "" echo ""
echo "[OPTIONS]" echo "[OPTIONS]"
echo "--dry Will run osync without actually doing anything; just testing" echo "--dry Will run osync without actually doing anything; just testing"
echo "--silent Will run osync without any output to stdout, used for cron jobs" echo "--silent Will run osync without any output to stdout, used for cron jobs"
echo "--verbose Increases output" echo "--verbose Increases output"
echo "--stats Adds rsync transfer statistics to verbose output" echo "--stats Adds rsync transfer statistics to verbose output"
echo "--partial Allows rsync to keep partial downloads that can be resumed later (experimental)" echo "--partial Allows rsync to keep partial downloads that can be resumed later (experimental)"
echo "--no-maxtime Disables any soft and hard execution time checks" echo "--no-maxtime Disables any soft and hard execution time checks"
echo "--force-unlock Will override any existing active or dead locks on initiator and target replica" echo "--force-unlock Will override any existing active or dead locks on initiator and target replica"
echo "--on-changes Will launch a sync task after a short wait period if there is some file activity on initiator replica. You should try daemon mode instead" echo "--on-changes Will launch a sync task after a short wait period if there is some file activity on initiator replica. You should try daemon mode instead"
echo "" echo ""
echo "[QUICKSYNC OPTIONS]" echo "[QUICKSYNC OPTIONS]"
echo "--initiator=\"\" Master replica path. Will contain state and backup directory (is mandatory)" echo "--initiator=\"\" Master replica path. Will contain state and backup directory (is mandatory)"
@ -2200,6 +2214,34 @@ 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
}
# Comand line argument flags # Comand line argument flags
_DRYRUN=0 _DRYRUN=0
_SILENT=0 _SILENT=0