Finished functions and refactored argument section

Help function uses command name in usage
(Required handling required arguments outside b3bp)
This commit is contained in:
2Shirt 2018-06-03 15:32:56 -06:00
parent 3ed53c6ece
commit 011e2af296

View file

@ -4,101 +4,129 @@
# Usage # Usage
read -r -d '' __usage <<-'EOF' || true read -r -d '' __usage <<-'EOF' || true
Usage: backup-git-repos-to-b2 [options] repo(s)...
Options: Options:
-b --b2-bucket [arg] Backblaze B2 bucket name. Required. -b --b2-bucket [arg] Backblaze B2 bucket name.
-i --b2-id [arg] Backblaze B2 account ID. Required. -i --b2-id [arg] Backblaze B2 account ID.
-k --b2-key [arg] Backblaze B2 application key. Required. -k --b2-key [arg] Backblaze B2 application key.
-g --github-account [arg] GitHub account name. Required. -g --github-account [arg] GitHub account name.
-t --temp-dir [arg] Dir to use for temp files. -t --temp-dir [arg] Dir to use for temp files.
-h --help This page. -h --help This page.
EOF EOF
__helptext='NOTE: --temp-dir defaults to "$(mktemp -d)" if not specified.' __helptext='NOTE: --temp-dir defaults to "$(mktemp -d)" if not specified.'
# Source BASH3 Boilerplate # Load BASH3 Boilerplate functions
source bash3boilerplate/main.sh source bash3boilerplate/main.sh
# Color fix for rxvt-unicode (tell b3bp we're xterm) # Color fix for rxvt-unicode (tell b3bp we're xterm)
TERM="xterm" TERM="xterm"
# Functions # Functions
function clone_github_repo() { function compress_repository() {
repo="$(fix_repo_name "${1:-}")" # Move to repository dir so relative paths are used in the archive
git clone --mirror --verbose \ local repo_source="${1:-}"
"https://github.com/${github_account}/${repo}.git" \ local archive_dest="${2:-}"
"${repo}-${date}.git" local repo_path="$(dirname "$(realpath "${repo_source}")")"
local repo_source="$(basename "$(realpath "${repo_source}")")"
local archive_dest="$(realpath "${archive_dest}")"
pushd "${repo_path}" >/dev/null
# Create archive
tar cpzf "${archive_dest}" "${repo_source}"
# Done
popd >/dev/null
} }
function find_b2_cmd() { function find_b2_cmd() {
if which backblaze-b2 >/dev/null 2>&1; then if which backblaze-b2 >/dev/null 2>&1; then
# Arch Linux uses this name # Arch Linux uses this name
echo "backblaze-b2" echo "backblaze-b2"
elif which b2 > /dev/null 2>&1; then elif which b2 > /dev/null 2>&1; then
echo "b2" echo "b2"
else else
__b3bp_log error "B2 command-line tool not found" __b3bp_log error "B2 command-line tool not found"
exit 1 exit 1
fi fi
} }
function fix_repository_name() { function fix_repository_name() {
# Remove '.git' from the name (if present) # Remove '.git' from the name (if present)
repository="${1:-}" local repository="${1:-}"
if [[ "${repository: -4:4}" == ".git" ]]; then if [[ "${repository: -4:4}" == ".git" ]]; then
repository="${repository:0: -4}" repository="${repository:0: -4}"
fi fi
echo "${repository}" echo "${repository}"
}
function main() {
# Set env
local b2_bucket="${arg_b}"
local b2_cmd="$(find_b2_cmd)"
local b2_id="${arg_i}"
local b2_key="${arg_k}"
local date="$(date '+%F_%H%M%z')"
local github_account="${arg_g}"
local temp_dir="${arg_t:-}"
if [[ "$temp_dir" == "" ]]; then
temp_dir="$(mktemp -d)"
fi
# Create the temp_dir
mkdir -p "${temp_dir}"
# Authorize B2 account
"${b2_cmd}" authorize-account "${b2_id}" "${b2_key}"
# Loop over repos
for repo in "${@}"; do
repo="$(fix_repository_name "${repo}")"
echo "Backing up ${repo}"
# Clone repo
echo "Cloning ${repo}"
git clone --mirror \
"https://github.com/${github_account}/${repo}.git" \
"${temp_dir}/${repo}-${date}.git"
# Compress repo
echo "Compressing ${repo}"
compress_repository "${temp_dir}/${repo}-${date}.git" \
"${temp_dir}/${repo}-${date}.git.tgz"
# Upload repo
echo "Uploading ${repo}"
"${b2_cmd}" upload-file "${b2_bucket}" \
"${temp_dir}/${repo}-${date}.git.tgz" \
"${github_account}/${repo}-${date}.git.tgz"
# Cleanup
/bin/rm "${temp_dir}/${repo}-${date}.git.tgz"
/bin/rm -rf "${temp_dir}/${repo}-${date}.git"
# Done
echo "Successfully backed up ${repo}"
done
# Remove temp_dir (if empty)
/bin/rmdir --ignore-fail-on-non-empty "${temp_dir}"
} }
b2_bucket="${arg_b}" # Check args
b2_cmd="$(find_b2_cmd)" __run="yes"
b2_id="${arg_i}" if [[ "${arg_h}" == "0" ]]; then
b2_key="${arg_k}" # Show missing arg warnings only if help arg isn't specified
date="$(date '+%F_%H%M%z')" [[ -z "${arg_b:-}" ]] && echo "B2 bucket not specified" && __run="no"
github_account="${arg_g}" [[ -z "${arg_i:-}" ]] && echo "B2 ID not specified" && __run="no"
temp_dir="${arg_t:-}" [[ -z "${arg_k:-}" ]] && echo "B2 Key not specified" && __run="no"
if [[ "$temp_dir" == "" ]]; then [[ -z "${arg_g:-}" ]] && echo "GitHub account not specified" && __run="no"
temp_dir="$(mktemp -d)" [[ "${#}" == "0" ]] && echo "No repos specified" && __run="no"
fi fi
# Create the backup directory # Show help or run backups
mkdir -p "${temp_dir}" if [[ "${arg_h}" = 1 ]] || [[ "${__run}" == "no" ]]; then
pushd "${temp_dir}" > /dev/null # -h or --help used or missing argument(s), show usage and exit
help "Usage: ${__base} [options] repo(s)..."
echo "Backing up ${repository}" else
# Backup repo(s)
main "${@}"
tar cpzf "${repository}-${date}.git.tgz" "${repository}-${date}.git"
if [ $? -ne 0 ]; then
echo "Error compressing ${repository}"
exit 1
fi fi
exit 0
# Authorize B2 account
"${b2_cmd}" authorize-account "${b2_id}" "${b2_key}"
if [ $? -ne 0 ]; then
echo "Error authorizing Backblaze B2 account"
exit 1
fi
if [[ -f "${repository}-${date}.git.tgz" ]]; then
"${b2_cmd}" upload-file "${b2_bucket}" \
"${repository}-${date}.git.tgz" \
"${github_account}/${repository}-${date}.git.tgz"
fi
if [ $? -ne 0 ]; then
echo "Error uploading ${repository} to Backblaze B2"
exit 1
fi
#delete tar file and checked out folder
/bin/rm "${temp_dir}/${repository}-${date}.git.tgz"
/bin/rm -rf "${temp_dir}/${repository}-${date}.git"
if [ $? -ne 0 ]; then
echo "Error removing temp files"
exit 1
fi
popd > /dev/null
echo "Succesfully backed up ${repository}"