script-sammlung/backup/thetown/ftp.sh
2017-02-28 17:29:58 +01:00

94 lines
2.1 KiB
Bash

#!/bin/bash
# /home/the-town/tools/ftp.sh -s /home/the-town/server/build/backups/ -n MapsBackup -d true
#Einstellungen
STARTPATH=${pwd}
DATE=$(date +%Y-%m-%d_%H-%M-%S)
#Params with Arguments
while getopts "s:n:d:t" opts; do
case ${opts} in
s) SOURCE=${OPTARG} ;;
n) BACKUPNAME=${OPTARG} ;;
d) DELETE=true ;;
t) TARBALL=true ;;
\?) echo "Invalid option: -$OPTARG"
exit 1
;;
:) echo "-$OPTARG bruacht Argumente"
exit 1
;;
esac
done
FTP_HOST=<host>
FTP_NAME=<user>
FTP_PASSWD=<password>
#Checks
if [ -z "$SOURCE" ]; then
if [ -z "$BACKUPNAME" ]; then
echo " === EasyFTP Uploader by Niklas Schütrumpf ==="
echo " Available Parameter: -s, -n, -d, -t"
echo " -s Absolute Source Path"
echo " -n Backup Path on FTP Server"
echo " -d Delete File after upload"
echo " -t Create Tarball before upload"
exit 1
fi
fi
if [[ "$SOURCE" != /* ]]; then
echo "Use Absolute Path"
exit 1
fi
if [[ ! -d "$SOURCE" ]]; then
if [[ ! -f "$SOURCE" ]]; then
echo "File or Directory does not exists"
exit 1
fi
fi
if [ ! -z "$BACKUPNAME" ]; then
if [[ "$BACKUPNAME" != */ ]]; then
BACKUPNAME=$BACKUPNAME"/"
fi
else
BACKUPNAME=""
fi
BACKUPNAME="${BACKUPNAME/DATE/$DATE}"
#logik
if [[ -d "$SOURCE" ]]; then
echo "Uploading Directory... $SOURCE"
cd $SOURCE
if [ "$TARBALL" = true ]; then
TAR=`basename "$SOURCE"`
TARBALL=$TAR".tar.gz"
tar cjf $TARBALL *
curl -T $TARBALL ftp://$FTP_HOST/$BACKUPNAME --user $FTP_NAME:$FTP_PASSWD --ftp-create-dirs
rm $TARBALL
else
if [ "$DELETE" = true ]; then
find . -type f -exec sh -c 'curl --ftp-ssl -k -P - --user '$FTP_NAME':'$FTP_PASSWD' --ftp-create-dirs -T $0 ftp://'$FTP_HOST'/'$BACKUPNAME'"$0" && rm "$0"' {} \;
else
find . -type f -exec curl --ftp-ssl -k -P - --user $FTP_NAME:$FTP_PASSWD --ftp-create-dirs -T {} ftp://$FTP_HOST/$BACKUPNAME{} \;
fi
fi
elif [[ -f "$SOURCE" ]]; then
echo "Uploading File... $SOURCE"
curl --ftp-ssl -k -P - -T $SOURCE ftp://$FTP_HOST/$BACKUPNAME --user $FTP_NAME:$FTP_PASSWD --ftp-create-dirs
if [ "$DELETE" = true ]; then
rm $SOURCE
fi
else
echo "$SOURCE is not valid"
exit 1
fi
cd $STARTPATH