Skip to content

Instantly share code, notes, and snippets.

@seakintruth
Last active September 21, 2021 03:13
Show Gist options
  • Save seakintruth/1159a3d0c911fe2083301578e58c7925 to your computer and use it in GitHub Desktop.
Save seakintruth/1159a3d0c911fe2083301578e58c7925 to your computer and use it in GitHub Desktop.
Bash script to compress a folder with screen
#!/usr/bin/env bash
# using template from: https://betterdev.blog/minimal-safe-bash-script-template/
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-f || --folder_name] "a folder name"
[-s || --source_dir] "/source/" [-d || --destination_dir] "/destination/" arg1 [arg2...]
Script description here.
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
-f, --folder_name Folder Name (source sub-folder's name)
-s, --source_dir Source Directory
-d, --destination_dir Destination Directory
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
# script cleanup here
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
msg() {
echo >&2 -e "${1-}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "$msg"
exit "$code"
}
parse_params() {
# default values of variables set from params
flag=0
param=''
while :; do
case "${1-}" in
-h | --help) usage ;;
-v | --verbose) set -x ;;
--no-color) NO_COLOR=1 ;;
-f | --folder_name)
folder_name="${2-}"
shift
;;
-s | --source_dir)
source_dir="${2-}"
shift
;;
-d | --destination_dir) # example named parameter
destination_dir="${2-}"
shift
;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
args=("$@")
# check required params and arguments
[[ -z "${folder_name-}" ]] && die "Missing required parameter: folder_name"
[[ -z "${source_dir-}" ]] && die "Missing required parameter: source_dir"
[[ -z "${destination_dir-}" ]] && die "Missing required parameter: destination_dir"
# NO ADDITIONAL SCRIPT ARGUMENTS ->
#[[ ${#args[@]} -eq 0 ]] && die "Missing script arguments"
return 0
}
parse_params "$@"
setup_colors
############################
# script logic starts here #
############################
screen -mdS "${folder_name}"
sleep 0.1
echo "Stuffing: tar -czvf ${destination_dir}/${folder_name}.tar.gz ${source_dir}/${folder_name}/\r"
screen -S "${folder_name}" -X stuff "tar -czvf ${destination_dir}/${folder_name}.tar.gz ${source_dir}/${folder_name}/\r"
####################
# end script logic #
####################
msg "${RED}Read parameters:${NOFORMAT}"
msg "- flag: ${flag}"
msg "- param: ${param}"
msg "- arguments: ${args[*]-}"
@seakintruth
Copy link
Author

bash script to use screen in a terminal to kick off a backup of a specific folder to a file named the same as the folder.

./compress_folder.sh -f "Folder_001" -s "/media/seakin/256External/" -d "/media/seakin/123GbThumb"
./compress_folder.sh -f "Folder_002" -s "/media/seakin/256External/" -d "/media/seakin/123GbThumb"

creates files "/media/seakin/123GbThumb.Folder_001.tar.gz" and "/media/seakin/123GbThumb.Folder_002.tar.gz" from "/media/seakin/256External/Folder_001" and "/media/seakin/256External/Folder_002"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment