#!/bin/sh
# SITE RUN backup <folder>: a tar.gz of a folder of the user, on standard output (uFTP saves it as uftp-job-<id>.out)
set -eu
cd "${UFTP_HOME:?}"                                            # the folder of the user
folder=${1:-.}                                                 # the parameter: hostile
case "$folder" in
    /*|-*|*..*) echo "not allowed: $folder" >&2; exit 2;;
esac
printf '%s' "$folder" | grep -Eq '^[A-Za-z0-9._@:/+=-]{1,128}$' || { echo "not allowed" >&2; exit 2; }
[ -d "$folder" ] || { echo "no such folder: $folder" >&2; exit 2; }
# a link that goes outside is not followed (tar keeps links as links); the job files of uFTP are not part of the backup
exec tar czf - --exclude='./uftp-job-*' --exclude='uftp-job-*' -- "$folder"
