You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dotbare/scripts/fbackup

98 lines
2.7 KiB
Plaintext

4 years ago
#!/usr/bin/env bash
4 years ago
#
# backup all tracked files
#
# @params
# Globals
4 years ago
# ${mydir}: current dir of the script
4 years ago
# ${backup_type}: types of backup, individual or all
4 years ago
# ${selected_files}: selected files to backup
# ${action_command}: actions to run, cp|mv
4 years ago
# Arguments
4 years ago
# -h: show help message and exit
# -s: select individual files through fzf and backup
# -p: pass in path and backup
# -m: use mv to backup instead of cp
4 years ago
set -e
set -f
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
4 years ago
source "${mydir}"/../helper/set_variable.sh
source "${mydir}"/../helper/git_query.sh
function usage() {
echo -e "Usage: dotbare fbackup [-h] [-m] [-s] [-p PATH] ...\n"
echo -e "Backup files to ${DOTBARE_BACKUP}"
echo -e "This is useful when untracking files or migrating on new machines\n"
4 years ago
echo -e "Default: backup every tracked files using cp to ${DOTBARE_BACKUP} directory\n"
4 years ago
echo -e "optional arguments:"
echo -e " -h, --help\t\tshow this help message and exit"
echo -e " -s, --select\t\tselect individual file through fzf and backup"
echo -e " -p PATH, --path PATH\tsepcify path of files to backup"
echo -e " -m, --move\t\tuse mv command for backup instead of default cp"
4 years ago
}
#######################################
# backup passed in files while preserving directory info
# Arguments:
# $1: files to backup, seperate by \n
# $2: action command (cp|mv)
#######################################
function dotbare_backup() {
local selected_files="$1"
local action_command="${2:-cp}"
while IFS= read -r line; do
dir_name=$(dirname "${line}")
[[ ! -d "${DOTBARE_BACKUP}/${dir_name}" ]] && mkdir -p "${DOTBARE_BACKUP}/${dir_name}"
command "${action_command}" -v "${line}" "${DOTBARE_BACKUP}/${line}"
done <<< "${selected_files}"
4 years ago
exit 0
}
4 years ago
backup_type="all"
action_command='cp'
selected_files=""
while [[ "$#" -gt 0 ]]; do
case "$1" in
-s|--select)
backup_type="select"
shift
4 years ago
;;
-p|--path)
[[ -z "$2" ]] && echo "Invalid option: $1" >&2 && usage && exit 1
selected_files="$2"
shift
shift
;;
-m|--move)
action_command="mv"
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Invalid option: $1" >&2
usage
4 years ago
exit 1
;;
esac
done
4 years ago
[[ -n "${selected_files}" ]] && dotbare_backup "${selected_files}" "${action_command}"
4 years ago
cd "${DOTBARE_TREE}"
if [[ "${backup_type}" == 'select' ]]; then
selected_files=$(get_git_file "select files to backup" "raw")
else
selected_files=$(/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
ls-files --full-name --directory "${DOTBARE_TREE}")
fi
[[ -z "${selected_files}" ]] && exit 1
dotbare_backup "${selected_files}" "${action_command}"