2020-05-11 01:56:27 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-05-05 05:06:25 +00:00
|
|
|
#
|
|
|
|
# backup all tracked files
|
|
|
|
#
|
|
|
|
# @params
|
|
|
|
# Globals
|
2020-05-07 01:56:58 +00:00
|
|
|
# ${mydir}: current dir of the script
|
2020-05-15 08:25:08 +00:00
|
|
|
# ${backup_type}: types of backup, individual or all
|
2020-05-07 01:56:58 +00:00
|
|
|
# ${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-06-25 01:07:13 +00:00
|
|
|
# -h|--help: show help message and exit
|
|
|
|
# -s|--select: select individual files through fzf and backup
|
2020-06-25 01:23:12 +00:00
|
|
|
# -p PATH|--path PATH: pass in path and backup
|
2020-06-25 01:07:13 +00:00
|
|
|
# -m|--move: use mv to backup instead of cp
|
2020-05-05 05:06:25 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
set -f
|
|
|
|
|
2020-05-13 07:30:54 +00:00
|
|
|
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
2020-05-05 05:06:25 +00:00
|
|
|
source "${mydir}"/../helper/set_variable.sh
|
|
|
|
source "${mydir}"/../helper/git_query.sh
|
|
|
|
|
|
|
|
function usage() {
|
2020-07-15 02:37:33 +00:00
|
|
|
echo -e "Usage: dotbare fbackup [-h] [-m] [-s] [-p PATH] ...
|
|
|
|
|
|
|
|
Backup files to ${DOTBARE_BACKUP}.
|
|
|
|
This is useful when untracking files or migrating to new machines.
|
|
|
|
|
|
|
|
Default: backup all tracked files using cp command to ${DOTBARE_BACKUP} directory.
|
|
|
|
|
|
|
|
optional arguments:
|
|
|
|
-h, --help\t\tshow this help message and exit.
|
|
|
|
-s, --select\t\tlist all tracked files and only backup the selected files.
|
2020-08-24 22:54:32 +00:00
|
|
|
-p PATH, --path PATH\tspecify path of files to backup.
|
2020-07-15 02:37:33 +00:00
|
|
|
-m, --move\t\tuse 'mv' instead of the default 'cp' command 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:
|
2020-08-24 22:54:32 +00:00
|
|
|
# $1: files to backup, separate 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-07-10 22:49:10 +00:00
|
|
|
[[ "${action_command}" == "cp" ]] \
|
2020-07-10 23:02:30 +00:00
|
|
|
&& cp -av "${line}" "${DOTBARE_BACKUP}/${line}" \
|
|
|
|
&& continue
|
|
|
|
# Purposly didn't use the -v flag because in finit, error message were
|
|
|
|
# directed to /dev/null but the cp/mv info will still be printed, causing confusion.
|
|
|
|
[[ "${action_command}" == "mv" ]] \
|
|
|
|
&& mv "${line}" "${DOTBARE_BACKUP}/${line}" \
|
|
|
|
&& echo "${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-15 08:25:08 +00:00
|
|
|
backup_type="all"
|
2020-05-09 04:01:50 +00:00
|
|
|
action_command='cp'
|
2020-06-25 00:31:37 +00:00
|
|
|
selected_files=""
|
|
|
|
|
|
|
|
while [[ "$#" -gt 0 ]]; do
|
|
|
|
case "$1" in
|
|
|
|
-s|--select)
|
|
|
|
backup_type="select"
|
|
|
|
shift
|
2020-05-07 01:54:01 +00:00
|
|
|
;;
|
2020-06-25 00:31:37 +00:00
|
|
|
-p|--path)
|
|
|
|
[[ -z "$2" ]] && echo "Invalid option: $1" >&2 && usage && exit 1
|
|
|
|
selected_files="$2"
|
|
|
|
shift
|
|
|
|
shift
|
2020-05-09 04:01:50 +00:00
|
|
|
;;
|
2020-06-25 00:31:37 +00:00
|
|
|
-m|--move)
|
2020-05-09 04:01:50 +00:00
|
|
|
action_command="mv"
|
2020-06-25 00:31:37 +00:00
|
|
|
shift
|
2020-05-07 01:40:33 +00:00
|
|
|
;;
|
2020-06-25 00:31:37 +00:00
|
|
|
-h|--help)
|
2020-05-07 01:40:33 +00:00
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
2020-06-25 00:31:37 +00:00
|
|
|
echo "Invalid option: $1" >&2
|
2020-05-07 01:40:33 +00:00
|
|
|
usage
|
2020-05-13 01:26:37 +00:00
|
|
|
exit 1
|
2020-05-07 01:40:33 +00:00
|
|
|
;;
|
|
|
|
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-06-25 00:31:37 +00:00
|
|
|
if [[ "${backup_type}" == 'select' ]]; then
|
2020-05-07 01:40:33 +00:00
|
|
|
selected_files=$(get_git_file "select files to backup" "raw")
|
|
|
|
else
|
2020-07-07 01:49:26 +00:00
|
|
|
selected_files=$(git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
|
2020-05-07 01:40:33 +00:00
|
|
|
ls-files --full-name --directory "${DOTBARE_TREE}")
|
|
|
|
fi
|
|
|
|
|
2020-05-15 07:36:18 +00:00
|
|
|
[[ -z "${selected_files}" ]] && exit 1
|
2020-05-09 04:01:50 +00:00
|
|
|
dotbare_backup "${selected_files}" "${action_command}"
|