From 011e2af296462de4f15ee6cf354b20dce73a0814 Mon Sep 17 00:00:00 2001 From: 2Shirt <1923621+2Shirt@users.noreply.github.com> Date: Sun, 3 Jun 2018 15:32:56 -0600 Subject: [PATCH] Finished functions and refactored argument section Help function uses command name in usage (Required handling required arguments outside b3bp) --- backup-git-repos-to-b2 | 180 ++++++++++++++++++++++++----------------- 1 file changed, 104 insertions(+), 76 deletions(-) diff --git a/backup-git-repos-to-b2 b/backup-git-repos-to-b2 index 7a201f8..56f48b7 100755 --- a/backup-git-repos-to-b2 +++ b/backup-git-repos-to-b2 @@ -4,101 +4,129 @@ # Usage read -r -d '' __usage <<-'EOF' || true -Usage: backup-git-repos-to-b2 [options] repo(s)... - Options: - -b --b2-bucket [arg] Backblaze B2 bucket name. Required. - -i --b2-id [arg] Backblaze B2 account ID. Required. - -k --b2-key [arg] Backblaze B2 application key. Required. - -g --github-account [arg] GitHub account name. Required. + -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.' -# Source BASH3 Boilerplate +# Load BASH3 Boilerplate functions source bash3boilerplate/main.sh # Color fix for rxvt-unicode (tell b3bp we're xterm) TERM="xterm" # Functions -function clone_github_repo() { - repo="$(fix_repo_name "${1:-}")" - git clone --mirror --verbose \ - "https://github.com/${github_account}/${repo}.git" \ - "${repo}-${date}.git" +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 + 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) - repository="${1:-}" - if [[ "${repository: -4:4}" == ".git" ]]; then - repository="${repository:0: -4}" - fi - echo "${repository}" + # 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}" } -b2_bucket="${arg_b}" -b2_cmd="$(find_b2_cmd)" -b2_id="${arg_i}" -b2_key="${arg_k}" -date="$(date '+%F_%H%M%z')" -github_account="${arg_g}" -temp_dir="${arg_t:-}" -if [[ "$temp_dir" == "" ]]; then - temp_dir="$(mktemp -d)" +# 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 -# Create the backup directory -mkdir -p "${temp_dir}" -pushd "${temp_dir}" > /dev/null - -echo "Backing up ${repository}" - - -tar cpzf "${repository}-${date}.git.tgz" "${repository}-${date}.git" -if [ $? -ne 0 ]; then - echo "Error compressing ${repository}" - exit 1 +# 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 - -# 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}" +exit 0