Help function uses command name in usage (Required handling required arguments outside b3bp)
132 lines
3.6 KiB
Bash
Executable file
132 lines
3.6 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
# Script to backup git repo(s) to Backblaze B2
|
|
|
|
# Usage
|
|
read -r -d '' __usage <<-'EOF' || true
|
|
Options:
|
|
-b --b2-bucket [arg] Backblaze B2 bucket name.
|
|
-i --b2-id [arg] Backblaze B2 account ID.
|
|
-k --b2-key [arg] Backblaze B2 application key.
|
|
-g --github-account [arg] GitHub account name.
|
|
-t --temp-dir [arg] Dir to use for temp files.
|
|
|
|
-h --help This page.
|
|
EOF
|
|
__helptext='NOTE: --temp-dir defaults to "$(mktemp -d)" if not specified.'
|
|
|
|
# Load BASH3 Boilerplate functions
|
|
source bash3boilerplate/main.sh
|
|
|
|
# Color fix for rxvt-unicode (tell b3bp we're xterm)
|
|
TERM="xterm"
|
|
|
|
# Functions
|
|
function compress_repository() {
|
|
# Move to repository dir so relative paths are used in the archive
|
|
local repo_source="${1:-}"
|
|
local archive_dest="${2:-}"
|
|
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() {
|
|
if which backblaze-b2 >/dev/null 2>&1; then
|
|
# Arch Linux uses this name
|
|
echo "backblaze-b2"
|
|
elif which b2 > /dev/null 2>&1; then
|
|
echo "b2"
|
|
else
|
|
__b3bp_log error "B2 command-line tool not found"
|
|
exit 1
|
|
fi
|
|
}
|
|
function fix_repository_name() {
|
|
# Remove '.git' from the name (if present)
|
|
local repository="${1:-}"
|
|
if [[ "${repository: -4:4}" == ".git" ]]; then
|
|
repository="${repository:0: -4}"
|
|
fi
|
|
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}"
|
|
}
|
|
|
|
# Check args
|
|
__run="yes"
|
|
if [[ "${arg_h}" == "0" ]]; then
|
|
# Show missing arg warnings only if help arg isn't specified
|
|
[[ -z "${arg_b:-}" ]] && echo "B2 bucket not specified" && __run="no"
|
|
[[ -z "${arg_i:-}" ]] && echo "B2 ID not specified" && __run="no"
|
|
[[ -z "${arg_k:-}" ]] && echo "B2 Key not specified" && __run="no"
|
|
[[ -z "${arg_g:-}" ]] && echo "GitHub account not specified" && __run="no"
|
|
[[ "${#}" == "0" ]] && echo "No repos specified" && __run="no"
|
|
fi
|
|
|
|
# Show help or run backups
|
|
if [[ "${arg_h}" = 1 ]] || [[ "${__run}" == "no" ]]; then
|
|
# -h or --help used or missing argument(s), show usage and exit
|
|
help "Usage: ${__base} [options] repo(s)..."
|
|
else
|
|
# Backup repo(s)
|
|
main "${@}"
|
|
fi
|
|
exit 0
|