Skip to content

Instantly share code, notes, and snippets.

@eliashussary
Created September 14, 2018 15:29
Show Gist options
  • Save eliashussary/7fc28c78d6c863880bb4eabd12e60951 to your computer and use it in GitHub Desktop.
Save eliashussary/7fc28c78d6c863880bb4eabd12e60951 to your computer and use it in GitHub Desktop.

Revisions

  1. eliashussary created this gist Sep 14, 2018.
    68 changes: 68 additions & 0 deletions scpbak.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    #!/bin/bash
    # ====================================================================================
    #
    # Title:
    # scpbak.sh
    #
    # Description:
    # A simple shell script to transfer files to a remote server,
    # followed by adding a suffix to transfered files to mark it as complete.
    #
    #
    # Author:
    # Elias Hussary <[email protected]>
    #
    # Last Update:
    # 2018-09-14
    #
    # ====================================================================================

    # assign args
    input_dir=$1
    input_glob=$2
    remote_path=$3
    file_suffix=$4

    # print args
    echo "
    $(date)
    input_dir=$input_dir
    input_glob=$input_glob
    remote_path=$remote_path
    file_suffix=$file_suffix
    "

    # if last arg is null, throw err
    if [ -z $4 ]
    then
    >&2 echo -e "\nERROR: Missing argument; you must input all arguments."
    exit 1
    fi

    echo -e "\nTransferring files...\n"

    # do scp transfer and save return code
    scp $input_dir$input_glob $remote_path
    rc=$?


    if [ $rc -eq 0 ]
    then

    # if return code = 0 (success) then rename files
    echo -e "\nRenaming files...\n"
    for file in $input_dir$input_glob
    do
    mv $file $file$file_suffix
    echo "$file -> $file$file_suffix"
    done

    echo -e '\nSUCCESS: Operation completed'
    exit 0

    else

    # if return code != 0 (error) then exit
    >&2 echo -e '\nERROR: Transfer failed'
    exit 1
    fi