Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unspecified port and user #237

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions rsync_tmbackup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,28 +174,36 @@ fn_expire_backups() {

fn_parse_ssh() {
# To keep compatibility with bash version < 3, we use grep
if echo "$DEST_FOLDER"|grep -Eq '^[A-Za-z0-9\._%\+\-]+@[A-Za-z0-9.\-]+\:.+$'
if echo "$DEST_FOLDER"|grep -Eq '^[A-Za-z0-9\._%\+\-]*@?[A-Za-z0-9.\-]+\:.+$'
then
SSH_USER=$(echo "$DEST_FOLDER" | sed -E 's/^([A-Za-z0-9\._%\+\-]+)@([A-Za-z0-9.\-]+)\:(.+)$/\1/')
SSH_HOST=$(echo "$DEST_FOLDER" | sed -E 's/^([A-Za-z0-9\._%\+\-]+)@([A-Za-z0-9.\-]+)\:(.+)$/\2/')
SSH_DEST_FOLDER=$(echo "$DEST_FOLDER" | sed -E 's/^([A-Za-z0-9\._%\+\-]+)@([A-Za-z0-9.\-]+)\:(.+)$/\3/')
SSH_USER=$(echo "$DEST_FOLDER" | sed -E 's/^(([A-Za-z0-9\._%\+\-]+)@)?([A-Za-z0-9.\-]+)\:(.+)$/\2/')
SSH_HOST=$(echo "$DEST_FOLDER" | sed -E 's/^(([A-Za-z0-9\._%\+\-]+)@)?([A-Za-z0-9.\-]+)\:(.+)$/\3/')
SSH_DEST_FOLDER=$(echo "$DEST_FOLDER" | sed -E 's/^(([A-Za-z0-9\._%\+\-]+)@)?([A-Za-z0-9.\-]+)\:(.+)$/\4/')
local id_rsa_option=""
if [ -n "$ID_RSA" ] ; then
SSH_CMD="ssh -p $SSH_PORT -i $ID_RSA ${SSH_USER}@${SSH_HOST}"
else
SSH_CMD="ssh -p $SSH_PORT ${SSH_USER}@${SSH_HOST}"
id_rsa_option="-i $ID_RSA"
fi
local ssh_user_option=""
if [ -n "$SSH_USER" ] ; then
ssh_user_option="${SSH_USER}@"
fi
SSH_DEST_FOLDER_PREFIX="${SSH_USER}@${SSH_HOST}:"
elif echo "$SRC_FOLDER"|grep -Eq '^[A-Za-z0-9\._%\+\-]+@[A-Za-z0-9.\-]+\:.+$'
SSH_CMD="ssh $SSH_PORT_OPTION ${id_rsa_option} ${ssh_user_option}${SSH_HOST}"
SSH_DEST_FOLDER_PREFIX="${ssh_user_option}${SSH_HOST}:"
elif echo "$SRC_FOLDER"|grep -Eq '^[A-Za-z0-9\._%\+\-]*@?[A-Za-z0-9.\-]+\:.+$'
then
SSH_USER=$(echo "$SRC_FOLDER" | sed -E 's/^([A-Za-z0-9\._%\+\-]+)@([A-Za-z0-9.\-]+)\:(.+)$/\1/')
SSH_HOST=$(echo "$SRC_FOLDER" | sed -E 's/^([A-Za-z0-9\._%\+\-]+)@([A-Za-z0-9.\-]+)\:(.+)$/\2/')
SSH_SRC_FOLDER=$(echo "$SRC_FOLDER" | sed -E 's/^([A-Za-z0-9\._%\+\-]+)@([A-Za-z0-9.\-]+)\:(.+)$/\3/')
SSH_USER=$(echo "$SRC_FOLDER" | sed -E 's/^(([A-Za-z0-9\._%\+\-]+)@)?([A-Za-z0-9.\-]+)\:(.+)$/\2/')
SSH_HOST=$(echo "$SRC_FOLDER" | sed -E 's/^(([A-Za-z0-9\._%\+\-]+)@)?([A-Za-z0-9.\-]+)\:(.+)$/\3/')
SSH_SRC_FOLDER=$(echo "$SRC_FOLDER" | sed -E 's/^(([A-Za-z0-9\._%\+\-]+)@)?([A-Za-z0-9.\-]+)\:(.+)$/\4/')
local id_rsa_option=""
if [ -n "$ID_RSA" ] ; then
SSH_CMD="ssh -p $SSH_PORT -i $ID_RSA ${SSH_USER}@${SSH_HOST}"
else
SSH_CMD="ssh -p $SSH_PORT ${SSH_USER}@${SSH_HOST}"
id_rsa_option="-i $ID_RSA"
fi
SSH_SRC_FOLDER_PREFIX="${SSH_USER}@${SSH_HOST}:"
local ssh_user_option=""
if [ -n "$SSH_USER" ] ; then
ssh_user_option="${SSH_USER}@"
fi
SSH_CMD="ssh $SSH_PORT_OPTION ${id_rsa_option} ${ssh_user_option}${SSH_HOST}"
SSH_SRC_FOLDER_PREFIX="${ssh_user_option}${SSH_HOST}:"
fi
}

Expand Down Expand Up @@ -268,7 +276,7 @@ SSH_SRC_FOLDER=""
SSH_CMD=""
SSH_DEST_FOLDER_PREFIX=""
SSH_SRC_FOLDER_PREFIX=""
SSH_PORT="22"
SSH_PORT=""
ID_RSA=""

SRC_FOLDER=""
Expand Down Expand Up @@ -349,6 +357,12 @@ if [[ -z "$SRC_FOLDER" || -z "$DEST_FOLDER" ]]; then
exit 1
fi

# Only pass the port option if a port was specified
SSH_PORT_OPTION=""
if [ -n "$SSH_PORT" ]; then
SSH_PORT_OPTION="-p $SSH_PORT"
fi

# Strips off last slash from dest. Note that it means the root folder "/"
# will be represented as an empty string "", which is fine
# with the current script (since a "/" is added when needed)
Expand Down Expand Up @@ -546,9 +560,9 @@ while : ; do
if [ -n "$SSH_CMD" ]; then
RSYNC_FLAGS="$RSYNC_FLAGS --compress"
if [ -n "$ID_RSA" ] ; then
CMD="$CMD -e 'ssh -p $SSH_PORT -i $ID_RSA -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'"
CMD="$CMD -e 'ssh $SSH_PORT_OPTION -i $ID_RSA -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'"
else
CMD="$CMD -e 'ssh -p $SSH_PORT -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'"
CMD="$CMD -e 'ssh $SSH_PORT_OPTION -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'"
fi
fi
CMD="$CMD $RSYNC_FLAGS"
Expand Down