#!/bin/bash declare thisScriptCallCommand thisScriptAbsPath thisScriptFileName thisScriptBaseName thisScriptCallCommand="${BASH_SOURCE[0]}" thisScriptAbsPath="$(realpath -s "${thisScriptCallCommand}" 2>/dev/null || { pushd "$(dirname "${0}")" &>/dev/null || exit 1; echo -n "$(cd "$(dirname "${0}")" &>/dev/null || true; pwd -P)/$(basename "${BASH_SOURCE[0]}")"; })" thisScriptFileName=$(basename "${thisScriptAbsPath}") thisScriptBaseName="${thisScriptFileName%\.*}" declare lockFileDescriptor lockFileDir lockFileNameAbsPath lockFileDescriptor='200' lockFileDir='/data/data/com.termux/files/usr/run/lock/quico-ops/scripts' lockFileDir="${lockFileDir%/}"'/'"${thisScriptBaseName}" mkdir -p "${lockFileDir}" lockFileNameAbsPath="${lockFileDir}/${thisScriptBaseName}.lock" # Locking mechanism is pretty much # http://www.kfirlavi.com/blog/2012/11/06/elegant-locking-of-bash-program/ # shellcheck disable=SC2120 function lock () { local fd="${1:-$lockFileDescriptor}" # Create lock file eval "exec ${fd}> ${lockFileNameAbsPath}" # Acquire lock flock -n "${fd}" && return 0 || return 1 } function eexit () { local errorStr="${*}" echo "${errorStr}" exit 1 } lock || eexit "Only one instance of ${thisScriptAbsPath} can run at a time." function cleanup () { rm -f "${lockFileNameAbsPath}" &> /dev/null } trap cleanup EXIT declare target sourceDirBasePath sshFilesBaseDir sshPrivateKeyFileName sshKnownHostsFileName declare -a sourceDirChildPathComponents [[ ! "${1}" ]] && { printf -- '%s\n' 'Argument 1 is empty. Must be rsync [USER@]HOST:DEST string. Exiting 1 ...' exit 1 } target="${1}" sourceDirBasePath='/storage/3430-3833' sourceDirBasePath="${sourceDirBasePath%/}" sshFilesBaseDir='/data/data/com.termux/files/home/.ssh' sshFilesBaseDir="${sshFilesBaseDir%/}" sshPrivateKeyFileName='android-copy-backups.sh.ssh-private-key' sshKnownHostsFileName='android-copy-backups.sh.known_hosts' sourceDirChildPathComponents[0]='DCIM' sourceDirChildPathComponents[1]='Signal' sourceDirChildPathComponents[2]='User' sourceDirChildPathComponents[3]='SwiftBackup' for sourceDir in "${sourceDirChildPathComponents[@]}"; do rsync -a --delete -e 'ssh -o IdentitiesOnly=yes -o StrictHostKeyChecking=yes -o PasswordAuthentication=no -o ChallengeResponseAuthentication=no -o PubkeyAut hentication=yes -o IdentityFile='"${sshFilesBaseDir}"'/'"${sshPrivateKeyFileName}"' -o UserKnownHostsFile='"${sshFilesBaseDir}"'/'"${sshKnownHostsFileName}" "${ sourceDirBasePath}"'/'"${sourceDir%/}" "${target}"'/' if [[ "${?}" -eq '0' ]]; then printf -- '%s\n' 'Successfully copied '"'${sourceDir}'"' data' fi done