mirror of
https://github.com/octoleo/hosts.git
synced 2024-11-21 12:25:14 +00:00
Improve backups
argument handling.
This commit is contained in:
parent
3e326dad13
commit
6e751065de
76
hosts
76
hosts
@ -768,11 +768,29 @@ Exit status:
|
||||
1 Invalid parameters or backup not found.
|
||||
HEREDOC
|
||||
backups() {
|
||||
local _subcommand="${1:-}"
|
||||
shift
|
||||
|
||||
local _hosts_dirname
|
||||
local _filename=
|
||||
local _hosts_dirname=
|
||||
_hosts_dirname="$(dirname "${HOSTS_PATH}")"
|
||||
local _skip_backup=0
|
||||
local _subcommand=
|
||||
|
||||
for __arg in "${@:-}"
|
||||
do
|
||||
case "${__arg}" in
|
||||
--skip-backup)
|
||||
_skip_backup=1
|
||||
;;
|
||||
create|compare|delete|restore|show)
|
||||
_subcommand="${__arg}"
|
||||
;;
|
||||
*)
|
||||
if [[ -z "${_filename:-}" ]]
|
||||
then
|
||||
_filename="${__arg}"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
case "${_subcommand}" in
|
||||
create)
|
||||
@ -785,24 +803,6 @@ backups() {
|
||||
printf "Backed up to %s--backup-%s\\n" "${HOSTS_PATH}" "${_timestamp}"
|
||||
;;
|
||||
compare)
|
||||
local _filename
|
||||
local _use_diff=0
|
||||
|
||||
for __arg in "${@:-}"
|
||||
do
|
||||
case "${__arg}" in
|
||||
--diff)
|
||||
_use_diff=1
|
||||
;;
|
||||
*)
|
||||
if [[ -z "${_filename:-}" ]]
|
||||
then
|
||||
_filename="${__arg}"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ -z "${_filename:-}" ]]
|
||||
then
|
||||
help backups
|
||||
@ -816,7 +816,7 @@ backups() {
|
||||
diff -u "${HOSTS_PATH}" "${_hosts_dirname}/${_filename}"
|
||||
;;
|
||||
delete)
|
||||
if [[ -z "${1:-}" ]]
|
||||
if [[ -z "${_filename:-}" ]]
|
||||
then
|
||||
help backups
|
||||
exit 1
|
||||
@ -824,49 +824,49 @@ backups() {
|
||||
|
||||
_verify_write_permissions "$@"
|
||||
|
||||
if [[ "${HOSTS_PATH}" != "${_hosts_dirname}/${1:-}" ]] &&
|
||||
[[ -e "${_hosts_dirname}/${1:-}" ]]
|
||||
if [[ "${HOSTS_PATH}" != "${_hosts_dirname}/${_filename:-}" ]] &&
|
||||
[[ -e "${_hosts_dirname}/${_filename:-}" ]]
|
||||
then
|
||||
rm "${_hosts_dirname}/${1:-}" &&
|
||||
printf "Backup deleted: %s\\n" "${_hosts_dirname}/${1:-}"
|
||||
rm "${_hosts_dirname}/${_filename:-}" &&
|
||||
printf "Backup deleted: %s\\n" "${_hosts_dirname}/${_filename:-}"
|
||||
else
|
||||
printf "Backup not found: %s\\n" "${1:-}"
|
||||
printf "Backup not found: %s\\n" "${_filename:-}"
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
restore)
|
||||
if [[ -z "${1:-}" ]]
|
||||
if [[ -z "${_filename:-}" ]]
|
||||
then
|
||||
help backups
|
||||
exit 1
|
||||
elif [[ ! -e "${_hosts_dirname}/${1}" ]]
|
||||
elif [[ ! -e "${_hosts_dirname}/${_filename}" ]]
|
||||
then
|
||||
printf "Backup not found: %s\\n" "${1:-}"
|
||||
printf "Backup not found: %s\\n" "${_filename:-}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
_verify_write_permissions "$@"
|
||||
|
||||
if [[ "${2:-}" != "--skip-backup" ]]
|
||||
if ! ((_skip_backup))
|
||||
then
|
||||
backups create
|
||||
fi
|
||||
|
||||
cat "${_hosts_dirname}/${1}" > "${HOSTS_PATH}" &&
|
||||
printf "Restored from backup: %s\\n" "${1}"
|
||||
cat "${_hosts_dirname}/${_filename}" > "${HOSTS_PATH}" &&
|
||||
printf "Restored from backup: %s\\n" "${_filename}"
|
||||
;;
|
||||
show)
|
||||
if [[ -z "${1:-}" ]]
|
||||
if [[ -z "${_filename:-}" ]]
|
||||
then
|
||||
help backups
|
||||
exit 1
|
||||
elif [[ ! -e "${_hosts_dirname}/${1}" ]]
|
||||
elif [[ ! -e "${_hosts_dirname}/${_filename}" ]]
|
||||
then
|
||||
printf "Backup not found: %s\\n" "${1:-}"
|
||||
printf "Backup not found: %s\\n" "${_filename:-}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cat "${_hosts_dirname}/${1:-}"
|
||||
cat "${_hosts_dirname}/${_filename:-}"
|
||||
;;
|
||||
*)
|
||||
local _filenames=()
|
||||
|
@ -85,7 +85,7 @@ No backups found. Create a new backup:
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups compare "${_backup_basename}" --diff
|
||||
run "${_HOSTS}" backups compare "${_backup_basename}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
printf "\${lines[1]}: '%s'\\n" "${lines[1]}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
@ -100,7 +100,7 @@ No backups found. Create a new backup:
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups compare --diff
|
||||
run "${_HOSTS}" backups compare
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
@ -115,7 +115,7 @@ No backups found. Create a new backup:
|
||||
run "${_HOSTS}" add 0.0.0.0 example.com
|
||||
}
|
||||
|
||||
run "${_HOSTS}" backups compare "invalid-backup-name" --diff
|
||||
run "${_HOSTS}" backups compare "invalid-backup-name"
|
||||
printf "\${status}: %s\\n" "${status}"
|
||||
printf "\${output}: '%s'\\n" "${output}"
|
||||
[[ ${status} -eq 1 ]]
|
||||
@ -128,6 +128,7 @@ No backups found. Create a new backup:
|
||||
{
|
||||
run "${_HOSTS}" backups create
|
||||
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
|
||||
[[ -e "${_backup_path}" ]]
|
||||
_backup_basename="$(basename "${_backup_path}")"
|
||||
}
|
||||
|
||||
@ -230,6 +231,7 @@ No backups found. Create a new backup:
|
||||
{
|
||||
run "${_HOSTS}" backups create
|
||||
_backup_path="$(echo "${output}" | sed -e 's/Backed up to \(.*\)/\1/')"
|
||||
[[ -e "${_backup_path:-}" ]]
|
||||
_backup_basename="$(basename "${_backup_path}")"
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user