2020-05-05 05:06:25 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# backup all tracked files
|
|
|
|
#
|
|
|
|
# @params
|
|
|
|
# Globals
|
2020-05-07 01:56:58 +00:00
|
|
|
# ${mydir}: current dir of the script
|
|
|
|
# ${individual_file}: allow selection of files to backup
|
|
|
|
# ${selected_files}: selected files to backup
|
2020-05-09 04:01:50 +00:00
|
|
|
# ${action_command}: actions to run, cp|mv
|
2020-05-05 05:06:25 +00:00
|
|
|
# Arguments
|
2020-05-07 01:56:58 +00:00
|
|
|
# -h: show help message and exit
|
|
|
|
# -s: select individual files through fzf and backup
|
|
|
|
# -p: pass in path and backup
|
2020-05-09 04:01:50 +00:00
|
|
|
# -m: use mv to backup instead of cp
|
2020-05-05 05:06:25 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
set -f
|
|
|
|
|
|
|
|
mydir="${0%/*}"
|
|
|
|
source "${mydir}"/../helper/set_variable.sh
|
|
|
|
source "${mydir}"/../helper/git_query.sh
|
|
|
|
|
|
|
|
function usage() {
|
2020-05-07 01:08:51 +00:00
|
|
|
echo -e "Usage: dotbare fbackup [-h] ...\n"
|
2020-05-07 01:40:33 +00:00
|
|
|
echo -e "Backup files to ${DOTBARE_BACKUP}"
|
|
|
|
echo -e "This is useful when untracking files or migrating on new machines\n"
|
2020-05-05 05:06:25 +00:00
|
|
|
echo -e "optional arguments:"
|
2020-05-07 01:56:58 +00:00
|
|
|
echo -e " -h\t\tshow this help message and exit"
|
|
|
|
echo -e " -s\t\tselect individual file through fzf and backup"
|
|
|
|
echo -e " -p PATH\tsepcify path to backup"
|
2020-05-05 05:06:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 01:11:48 +00:00
|
|
|
#######################################
|
|
|
|
# backup passed in files while preserving directory info
|
|
|
|
# Arguments:
|
|
|
|
# $1: files to backup, seperate by \n
|
2020-05-09 04:01:50 +00:00
|
|
|
# $2: action command (cp|mv)
|
2020-05-07 01:11:48 +00:00
|
|
|
#######################################
|
|
|
|
function dotbare_backup() {
|
|
|
|
local selected_files="$1"
|
2020-05-09 04:01:50 +00:00
|
|
|
local action_command="${2:-cp}"
|
2020-05-07 01:11:48 +00:00
|
|
|
while IFS= read -r line; do
|
|
|
|
dir_name=$(dirname "${line}")
|
|
|
|
[[ ! -d "${DOTBARE_BACKUP}/${dir_name}" ]] && mkdir -p "${DOTBARE_BACKUP}/${dir_name}"
|
2020-05-09 04:01:50 +00:00
|
|
|
command "${action_command}" -v "${line}" "${DOTBARE_BACKUP}/${line}"
|
2020-05-07 01:11:48 +00:00
|
|
|
done <<< "${selected_files}"
|
2020-05-07 01:54:01 +00:00
|
|
|
exit 0
|
2020-05-07 01:11:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 01:40:33 +00:00
|
|
|
individual_file=""
|
2020-05-09 04:01:50 +00:00
|
|
|
action_command='cp'
|
|
|
|
while getopts ":hsp:m" opt; do
|
2020-05-07 01:40:33 +00:00
|
|
|
case "$opt" in
|
|
|
|
s)
|
|
|
|
individual_file='true'
|
2020-05-07 01:54:01 +00:00
|
|
|
;;
|
|
|
|
p)
|
|
|
|
selected_files="${OPTARG}"
|
2020-05-09 04:01:50 +00:00
|
|
|
;;
|
|
|
|
m)
|
|
|
|
action_command="mv"
|
2020-05-07 01:40:33 +00:00
|
|
|
;;
|
|
|
|
h)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Invalid option: ${OPTARG}" >&2
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2020-05-07 01:08:51 +00:00
|
|
|
|
2020-05-07 01:11:48 +00:00
|
|
|
|
2020-05-09 04:01:50 +00:00
|
|
|
[[ -n "${selected_files}" ]] && dotbare_backup "${selected_files}" "${action_command}"
|
2020-05-07 01:54:01 +00:00
|
|
|
|
|
|
|
cd "${DOTBARE_TREE}"
|
2020-05-07 01:40:33 +00:00
|
|
|
if [[ -n "${individual_file}" ]]; 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
|
|
|
|
|
2020-05-09 04:01:50 +00:00
|
|
|
dotbare_backup "${selected_files}" "${action_command}"
|