92 lines
2.2 KiB
Bash
Executable File
92 lines
2.2 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
_printhelp()
|
|
{
|
|
cat <<EOF
|
|
Usage: ${1##*/} [-m METHOD] [-v] FILES|FOLDERS
|
|
|
|
Create an archive in current folder from a single or multiples
|
|
files/folders. Each input entry will be at archive root (no absolute path is
|
|
used). If only one file/folder is given as input, then the archive is named
|
|
after this input. Otherwise it is named after current PWD.
|
|
|
|
-h: Display this help.
|
|
-m: Choose compression method.
|
|
* gz: gzip (default).
|
|
* xz: LZMA.
|
|
-i: The archive is created without owner/group information. (GNU tar only.)
|
|
-v: Exclude VCS data. (GNU tar only.)
|
|
EOF
|
|
}
|
|
|
|
OUTPATH="$PWD"
|
|
ARCEXT="tar.gz"
|
|
ARCOPT="z"
|
|
OPTIONS=""
|
|
while getopts ":him:v" opt; do
|
|
case $opt in
|
|
h)
|
|
_printhelp "$0"
|
|
exit 1 ;;
|
|
i)
|
|
OPTIONS="$OPTIONS --owner=root --group=root --numeric-owner" ;;
|
|
m)
|
|
[ "$OPTARG" = "gz" ] && ARCEXT="tar.gz" && ARCOPT="z"
|
|
[ "$OPTARG" = "xz" ] && ARCEXT="tar.xz" && ARCOPT="J"
|
|
;;
|
|
v)
|
|
OPTIONS="$OPTIONS --exclude-vcs" ;;
|
|
?)
|
|
_printhelp "$0"
|
|
exit 1 ;;
|
|
:)
|
|
echo "Missing argument."
|
|
_printhelp "$0"
|
|
exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
|
|
shift $(($OPTIND - 1))
|
|
|
|
if [ $# -eq 0 ]; then
|
|
_printhelp "$0"
|
|
exit 1
|
|
fi
|
|
|
|
## Only one input entry: use it as base name for the archive.
|
|
if [ $# -eq 1 ]; then
|
|
REALPATH="$(realpath "$1")"
|
|
OUTFILE="${REALPATH##*/}-$(date +%F-%H%M%S).${ARCEXT}"
|
|
else
|
|
OUTFILE="${PWD##*/}-$(date +%F-%H%M%S).${ARCEXT}"
|
|
fi
|
|
|
|
REALPWD="$(realpath "$PWD")"
|
|
for i; do
|
|
REALPATH="$(realpath "$i")"
|
|
|
|
## Check if one of the arguments is current folder. We need to make sure the
|
|
## archive is not created in an input folder, otherwise it will include
|
|
## itself.
|
|
if [ "$REALPATH" = "$REALPWD" ]; then
|
|
OUTPATH="$(realpath "$REALPWD/..")"
|
|
break
|
|
fi
|
|
done
|
|
|
|
|
|
if [ ! -w "$OUTPATH" ]; then
|
|
echo "[$OUTPATH] is not writable. Exiting."
|
|
exit
|
|
fi
|
|
|
|
echo "==> [$OUTPATH/$OUTFILE]"
|
|
for i; do
|
|
REALPATH="$(realpath "$i")"
|
|
echo "${REALPATH}" >&2
|
|
echo "-C '${REALPATH%/*}'"
|
|
echo "${REALPATH##*/}"
|
|
done | tar $OPTIONS -$ARCOPT -cf "$OUTPATH/$OUTFILE" -T -
|
|
|