Skip to content

Instantly share code, notes, and snippets.

@EldonMcGuinness
Last active June 7, 2017 19:52
Show Gist options
  • Save EldonMcGuinness/c0e10a8544d7d2f7bf2e47da1d67424f to your computer and use it in GitHub Desktop.
Save EldonMcGuinness/c0e10a8544d7d2f7bf2e47da1d67424f to your computer and use it in GitHub Desktop.
Tar and Upload
#!/bin/bash
SOURCE=
DEST=
RSA_KEY=
usage() {
cat << EOF
usage: $0 options
This script tars up a given file/folder and then uploads it to a remote server
OPTIONS:
-s File or directory to tar
-r RSA key to use
-d Destination server to upload to
-h Show this message
EOF
}
while getopts 's:r:d:h' opt; do
case $opt in
s)
SOURCE="$OPTARG"
;;
r)
RSA_KEY="$OPTARG"
;;
h)
usage
exit 1
;;
d)
DEST="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [[ -z $SOURCE ]]; then
echo "You have to specify what you want to archive!"
exit 1
fi
if [[ -z $RSA_KEY ]]; then
echo "You have to specify a RSA identity to use!"
exit 1
fi
if [[ -z $DEST ]]; then
echo "You have to specify a destination server!"
exit 1
fi
TS="$(date +%Y-%m-%d-%H)"
ARCHIVE_NAME="backupDEDICATED-${TS}.tar.gz"
tar -zcf "$ARCHIVE_NAME" "$SOURCE"
if [ ! $? ]; then
echo There was a problem creating the archive!
exit 2
fi
scp -i "$RSA_KEY" "$ARCHIVE_NAME" "$DEST"
if [ ! $? ]; then
echo There was a probelm uploading the archive!
exit 3
fi
rm "$ARCHIVE_NAME"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment