fixed non-local variables, changed variable names, other minor fixes
This commit is contained in:
parent
96e65508d1
commit
006ecbe7d4
1 changed files with 23 additions and 21 deletions
|
|
@ -5,29 +5,28 @@ source functions.sh
|
||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
trap 'cleanup' ERR INT
|
|
||||||
|
|
||||||
cleanup() {
|
cleanup() {
|
||||||
if [[ -n "$archive" ]]; then
|
local archive_name="$1"
|
||||||
rm -f "${archive}"*
|
|
||||||
log "INFO" "Cleaning up $(basename "$archive") remains before exit"
|
if [[ -n "$archive_name" ]]; then
|
||||||
|
rm -f "${archive_name}"*
|
||||||
|
log "INFO" "Cleaning up $(basename "$archive_name") remains before exit"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
check_input() {
|
check_input() {
|
||||||
local input_file="$1"
|
local input_file="$1"
|
||||||
local archive_pwd="$2"
|
local password="$2"
|
||||||
local size="$3"
|
local size="$3"
|
||||||
|
|
||||||
[[ -e "$input_file" ]] || { log "ERROR" "Input file '${input_file}' does not exist" && exit 2 ;}
|
[[ -e "$input_file" ]] || { log "ERROR" "Input file '${input_file}' does not exist" && exit 2 ;}
|
||||||
[[ -n "${archive_pwd}" ]] || { log "ERROR" "No password given" && exit 2 ;}
|
[[ -n "${password}" ]] || { log "ERROR" "No password given" && exit 2 ;}
|
||||||
|
|
||||||
if [[ ! "$size" =~ ^([1-9][0-9]*)?$ ]]; then
|
if [[ ! "$size" =~ ^([1-9][0-9]*)?$ ]]; then
|
||||||
log "ERROR" "Invalid size $size, must be empty or a positive number"
|
log "ERROR" "Invalid size $size, must be empty or a positive number"
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
log "INFO" "Archiving '${input_file}' with password '${archive_pwd}' and size '$size'"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
calc_smart_volumes() {
|
calc_smart_volumes() {
|
||||||
|
|
@ -35,6 +34,7 @@ calc_smart_volumes() {
|
||||||
# will be approximately the same size
|
# will be approximately the same size
|
||||||
local input_file="$1"
|
local input_file="$1"
|
||||||
local size="$2"
|
local size="$2"
|
||||||
|
local total_size num_volumes smart_size
|
||||||
|
|
||||||
total_size=$(du -s -BM "${input_file}" | grep -oP '^\d+') || return 1
|
total_size=$(du -s -BM "${input_file}" | grep -oP '^\d+') || return 1
|
||||||
|
|
||||||
|
|
@ -53,6 +53,7 @@ set_volumes() {
|
||||||
local input_file="$1"
|
local input_file="$1"
|
||||||
local size="${2:-}"
|
local size="${2:-}"
|
||||||
local volumes=""
|
local volumes=""
|
||||||
|
local smart_size
|
||||||
|
|
||||||
if [[ -n "$size" ]]; then
|
if [[ -n "$size" ]]; then
|
||||||
smart_size="$(calc_smart_volumes "$input_file" "$size" || echo -n)"
|
smart_size="$(calc_smart_volumes "$input_file" "$size" || echo -n)"
|
||||||
|
|
@ -66,34 +67,34 @@ set_volumes() {
|
||||||
|
|
||||||
# Generates a random archive name of 20 alphanumeric characters
|
# Generates a random archive name of 20 alphanumeric characters
|
||||||
generate_name() {
|
generate_name() {
|
||||||
archive="$archive_dir/$(tr -dc '[:alnum:]' < /dev/urandom | head -c 20).7z"
|
local archive_name="$archive_dir/$(tr -dc '[:alnum:]' < /dev/urandom | head -c 20).7z"
|
||||||
|
|
||||||
log "INFO" "Generated archive name '$archive'"
|
log "INFO" "Generated archive name '$archive_name'"
|
||||||
|
|
||||||
echo "$archive"
|
echo "$archive_name"
|
||||||
}
|
}
|
||||||
|
|
||||||
create_archive() {
|
create_archive() {
|
||||||
local input_file="$1"
|
local input_file="$1"
|
||||||
local archive_pwd="$2"
|
local password="$2"
|
||||||
local volumes="$3"
|
local volumes="$3"
|
||||||
local size="$4"
|
local size="$4"
|
||||||
local archive="$5"
|
local archive_name="$5"
|
||||||
|
|
||||||
[[ -n "$volumes" ]] && log "INFO" "Volumes size is set to $size MB"
|
[[ -n "$volumes" ]] && log "INFO" "Volumes size is set to $size MB"
|
||||||
|
|
||||||
7z a -mhe=on "-p${archive_pwd}" "$volumes" "$archive" "${input_file}"
|
7z a -mhe=on "-p${password}" "$volumes" "$archive_name" "${input_file}"
|
||||||
}
|
}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
local input_file="${1:-}"
|
local input_file="${1:-}"
|
||||||
local archive_pwd="${2:-}"
|
local password="${2:-}"
|
||||||
local size="${3:-}"
|
local size="${3:-}"
|
||||||
local volumes archive archive_dir
|
local volumes archive_name archive_dir
|
||||||
archive_dir="$(dirname "$0")/../archives"
|
archive_dir="$(dirname "$0")/../archives"
|
||||||
|
|
||||||
check_dependencies "7z"
|
check_dependencies "7z"
|
||||||
check_input "$input_file" "$archive_pwd" "$size"
|
check_input "$input_file" "$password" "$size"
|
||||||
|
|
||||||
if ! mkdir -p "$archive_dir"; then
|
if ! mkdir -p "$archive_dir"; then
|
||||||
log "ERROR" "Unable to create archive directory"
|
log "ERROR" "Unable to create archive directory"
|
||||||
|
|
@ -101,16 +102,17 @@ main() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
volumes="$(set_volumes "$input_file" "$size")"
|
volumes="$(set_volumes "$input_file" "$size")"
|
||||||
archive="$(generate_name)"
|
archive_name="$(generate_name)"
|
||||||
|
|
||||||
log "INFO" "Packing '${input_file}' into archive '$archive' with password '${archive_pwd}'"
|
trap 'cleanup "$archive_name"' ERR INT
|
||||||
|
|
||||||
|
log "INFO" "Packing '${input_file}' into archive '$archive_name' with password '${password}'"
|
||||||
create_archive \
|
create_archive \
|
||||||
"$input_file" \
|
"$input_file" \
|
||||||
"$archive_pwd" \
|
"$password" \
|
||||||
"$volumes" \
|
"$volumes" \
|
||||||
"$size" \
|
"$size" \
|
||||||
"$archive"
|
"$archive_name"
|
||||||
|
|
||||||
log "NOTICE" "Archive created"
|
log "NOTICE" "Archive created"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue