removed unnecessary source, fixed logging messages, refactored functions to work in a more traditional functional style rather than relying on global values
This commit is contained in:
parent
834ba85b30
commit
ebbc407b93
1 changed files with 58 additions and 25 deletions
83
modules/archive.sh
Normal file → Executable file
83
modules/archive.sh
Normal file → Executable file
|
|
@ -1,7 +1,6 @@
|
|||
#!/bin/bash
|
||||
# This script packs a file or directory into a 7z archive with the given password.
|
||||
# It can (optionally) split the archives into volumes of a fixed size in megabytes.
|
||||
source colors.sh
|
||||
source functions.sh
|
||||
|
||||
trap 'cleanup' ERR INT
|
||||
|
|
@ -14,70 +13,104 @@ cleanup() {
|
|||
}
|
||||
|
||||
check_input() {
|
||||
if [[ ! -e "${input_file}" ]]; then
|
||||
log "ERROR" "Input file '${input_file}' does not exist"
|
||||
exit 2
|
||||
elif [[ -z "${archive_pwd}" ]]; then
|
||||
log "ERROR" "No password given"
|
||||
exit 2
|
||||
elif [[ ! "$size" =~ ^([1-9][0-9]*)?$ ]]; then
|
||||
local input_file="$1"
|
||||
local archive_pwd="$2"
|
||||
local size="$3"
|
||||
|
||||
[[ -e "$input_file" ]] || { log "ERROR" "Input file '${input_file}' does not exist" && exit 2 ;}
|
||||
[[ -n "${archive_pwd}" ]] || { log "ERROR" "No password given" && exit 2 ;}
|
||||
|
||||
if [[ ! "$size" =~ ^([1-9][0-9]*)?$ ]]; then
|
||||
log "ERROR" "Invalid size $size, must be empty or a positive number"
|
||||
exit 2
|
||||
fi
|
||||
|
||||
log "INFO" "Archiving file/directory '${input_file}' with password '${archive_pwd}' and size '$size'" >&2
|
||||
log "INFO" "Archiving '${input_file}' with password '${archive_pwd}' and size '$size'"
|
||||
}
|
||||
|
||||
# Calculates a "smart" volume size so that each volume (including the final part) will be approximately the same size
|
||||
calc_smart_volumes() {
|
||||
total_size=$(du -s -BM "${input_file}" | grep -oP '^\d+') || return 0
|
||||
# Calculates a "smart" volume size so that each volume (including the final part)
|
||||
# will be approximately the same size
|
||||
local input_file="$1"
|
||||
local size="$2"
|
||||
|
||||
total_size=$(du -s -BM "${input_file}" | grep -oP '^\d+') || return 1
|
||||
|
||||
# If total file size is smaller than volume size, don't pack it into volumes
|
||||
(( total_size < size )) && return 1
|
||||
|
||||
num_volumes=$(( (total_size + size - 1) / size ))
|
||||
size=$(( (total_size + num_volumes - 1) / num_volumes ))
|
||||
smart_size=$(( (total_size + num_volumes - 1) / num_volumes ))
|
||||
|
||||
log "INFO" "Calculated smart volume size '$size'" >&2
|
||||
log "INFO" "Calculated smart volume size '$size'"
|
||||
|
||||
echo "$smart_size"
|
||||
}
|
||||
|
||||
set_volumes() {
|
||||
[[ -n "$size" ]] && calc_smart_volumes && volumes="-v${size}m"
|
||||
local input_file="$1"
|
||||
local size="$2"
|
||||
local volumes=""
|
||||
|
||||
log "INFO" "Volumes set to '$volumes'" >&2
|
||||
if [[ -n "$size" ]]; then
|
||||
smart_size="$(calc_smart_volumes $input_file || echo -n)"
|
||||
volumes="-v${smart_size}m"
|
||||
fi
|
||||
|
||||
log "INFO" "Volumes set to '$volumes'"
|
||||
|
||||
echo "$volumes"
|
||||
}
|
||||
|
||||
# Generates a random archive name of 20 alphanumeric characters
|
||||
generate_name() {
|
||||
archive="$archive_dir/$(tr -dc [:alnum:] < /dev/urandom | head -c 20).7z"
|
||||
|
||||
log "INFO" "Generated archive name '$archive'" >&2
|
||||
log "INFO" "Generated archive name '$archive'"
|
||||
|
||||
echo "$archive"
|
||||
}
|
||||
|
||||
create_archive() {
|
||||
log "INFO" "Packing '${input_file}' into archive '$archive' with password '${archive_pwd}'"
|
||||
local input_file="$1"
|
||||
local archive_pwd="$2"
|
||||
local volumes="$3"
|
||||
local size="$4"
|
||||
local archive="$5"
|
||||
|
||||
[[ -n "$volumes" ]] && log "INFO" "Volumes size is set to $size MB"
|
||||
|
||||
7z a -mhe=on "-p${archive_pwd}" "$volumes" "$archive" "${input_file}" >&2
|
||||
7z a -mhe=on "-p${archive_pwd}" "$volumes" "$archive" "${input_file}"
|
||||
}
|
||||
|
||||
main() {
|
||||
local input_file="${1:-}"
|
||||
local archive_pwd="${2:-}"
|
||||
local size="${3:-}"
|
||||
local volumes archive archive_dir
|
||||
archive_dir="$(dirname "$0")/../archives"
|
||||
input_file="${1}"
|
||||
archive_pwd="${2}"
|
||||
size="$3"
|
||||
|
||||
check_dependencies "7z"
|
||||
check_input "$input_file" "$archive_pwd" "$size"
|
||||
|
||||
if ! mkdir -p "$archive_dir"; then
|
||||
log "ERROR" "Unable to create archive directory"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
check_input
|
||||
set_volumes
|
||||
generate_name
|
||||
create_archive
|
||||
volumes="$(set_volumes $size)"
|
||||
archive="$(generate_name)"
|
||||
|
||||
log "NOTICE" "Packing '${input_file}' into archive '$archive' with password '${archive_pwd}'"
|
||||
|
||||
create_archive \
|
||||
"$input_file" \
|
||||
"$archive_pwd" \
|
||||
"$volumes" \
|
||||
"$size" \
|
||||
"$archive"
|
||||
|
||||
log "NOTICE" "Archive created"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue