Skip to content

Commit

Permalink
src: backup: Adding verbose mode.
Browse files Browse the repository at this point in the history
This commit adds support for the verbose parameter within `kw backup`.
The verbose parameter gives details of the commands that are executed behind the scenes.

Note: This is part of the issue: kworkflow#179

Signed-off-by: Aquila Macedo <aquilamacedo@riseup.net>
  • Loading branch information
aquilamacedo committed Apr 13, 2023
1 parent 398b2de commit 678d8cb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
4 changes: 4 additions & 0 deletions documentation/man/features/backup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ SYNOPSIS
========
| *kw* *backup* [<path>]
| *kw* *backup* (-r | \--restore) <path> [(-f | \--force)]
| *kw* *backup* (-v | \--verbose)
DESCRIPTION
===========
Expand All @@ -24,6 +25,9 @@ OPTIONS
then it will by default ask you on how to proceed. To simply replace all files
from **KW_DATA_DIR** with the backup, you may use the `\--force` option.

-v, \--verbose:
Verbose mode allows the user to see the commands executed under the hood.

EXAMPLES
========

Expand Down
1 change: 1 addition & 0 deletions src/_kw
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ _kw_backup()
fi

_arguments : \
'(-v --verbose)'{-v,--verbose}'[enable verbose mode]' \
'(-r --restore)'{-r,--restore}'[restore kw data from specified path]: :_files' \
$force \
$args
Expand Down
76 changes: 56 additions & 20 deletions src/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ decompress_path="$KW_CACHE_DIR/tmp-kw-backup"
# recieves an argument and call other functions based on that.
function backup()
{
local flag

parse_backup_options "$@"
if [[ "$?" -gt 0 ]]; then
complain "${options_values['ERROR']}"
return 22 # EINVAL
fi

[[ -n "${options_values['VERBOSE']}" ]] && flag='VERBOSE'

if [[ -n "${options_values['BACKUP_PATH']}" ]]; then
create_backup "${options_values['BACKUP_PATH']}"
create_backup "${options_values['BACKUP_PATH']}" "$flag"
return
fi

if [[ -n "${options_values['RESTORE_PATH']}" ]]; then
restore_backup "${options_values['RESTORE_PATH']}"
restore_backup "${options_values['RESTORE_PATH']}" "$flag"
return
fi
}
Expand Down Expand Up @@ -74,6 +78,7 @@ function restore_backup()
local ret
local config_file
local difference
local cmd

flag=${flag:-'SILENT'}

Expand All @@ -85,7 +90,8 @@ function restore_backup()
if [[ -n "${options_values['FORCE']}" ]]; then
decompress_path="$KW_DATA_DIR"
else
mkdir -p "$decompress_path"
cmd="mkdir -p ${decompress_path}"
cmd_manager "$flag" "$cmd"
fi
extract_tarball "$path" "$decompress_path" 'gzip' "$flag"

Expand All @@ -96,8 +102,8 @@ function restore_backup()
fi

if [[ -z "${options_values['FORCE']}" ]]; then
restore_config
restore_pomodoro
restore_config "$flag"
restore_pomodoro "$flag"
restore_statistics
fi

Expand All @@ -108,43 +114,58 @@ function restore_backup()
#
function restore_config()
{
local flag="$1"
local config_file
local cmd

flag=${flag:-'SILENT'}

if [[ -d "$decompress_path/"configs ]]; then
mkdir -p "$KW_DATA_DIR/configs/configs"
mkdir -p "$KW_DATA_DIR/configs/metadata"
cmd="mkdir -p ${KW_DATA_DIR}/configs/configs"
cmd_manager "$flag" "$cmd"
cmd="mkdir -p ${KW_DATA_DIR}/configs/metadata"
cmd_manager "$flag" "$cmd"

for file in "$decompress_path/"configs/configs/*; do
config_file="$(get_file_name_from_path "$file")"
if [[ -f "$KW_DATA_DIR/configs/configs/$config_file" ]] &&
! cmp -s "$KW_DATA_DIR/configs/configs/$config_file" "$file"; then
complain "It looks like that the file $config_file differs from the backup version."
diff -u --color=always "$KW_DATA_DIR/configs/configs/$config_file" "$file"
cmd="diff -u --color=always ${KW_DATA_DIR}/configs/configs/${config_file} ${file}"
cmd_manager "$flag" "$cmd"
if [[ $(ask_yN 'Do you want to replace it and its metadata?') =~ "0" ]]; then
continue
fi
fi
cp -r "$decompress_path/configs/configs/$config_file" "$KW_DATA_DIR/configs/configs"
cp -r "$decompress_path/configs/metadata/$config_file" "$KW_DATA_DIR/configs/metadata"
cmd="cp -r ${decompress_path}/configs/configs/${config_file} ${KW_DATA_DIR}/configs/configs"
cmd_manager "$flag" "$cmd"
cmd="cp -r ${decompress_path}/configs/metadata/${config_file} ${KW_DATA_DIR}/configs/metadata"
cmd_manager "$flag" "$cmd"
done
fi
}

function restore_data_from_dir()
{
local dir="$1"
local flag="$2"
local decision
local year
local month
local day
local difference
local cmd

flag=${flag:-'SILENT'}

for year_dir in "$decompress_path/$dir/"*/; do
year="$(str_remove_prefix "$year_dir" "$decompress_path/$dir/")"
mkdir -p "$KW_DATA_DIR/$dir/$year"
cmd="mkdir -p ${KW_DATA_DIR}/${dir}/${year}"
cmd_manager "$flag" "$cmd"
for month_dir in "$year_dir"*; do
month="$(str_remove_prefix "$month_dir" "$decompress_path/$dir/$year")"
mkdir -p "$KW_DATA_DIR/$dir/$year$month"
cmd="mkdir -p ${KW_DATA_DIR}/${dir}/${year}${month}"
cmd_manager "$flag" "$cmd"
for day_file in "$month_dir"/*; do
day="$(str_remove_prefix "$day_file" "$decompress_path/$dir/$year$month/")"
if [[ -f "$KW_DATA_DIR/$dir/$year$month/$day" ]] &&
Expand All @@ -160,7 +181,8 @@ function restore_data_from_dir()

case "$decision" in
1)
cp "$day_file" "$KW_DATA_DIR/$dir/$year$month/$day"
cmd="cp ${day_file} ${KW_DATA_DIR}/${dir}/${year}${month}/${day}"
cmd_manager "$flag" "$cmd"
continue
;;
2)
Expand All @@ -173,19 +195,24 @@ function restore_data_from_dir()
;;
esac
fi
cp "$day_file" "$KW_DATA_DIR/$dir/$year$month/$day"
cmd="cp ${day_file} ${KW_DATA_DIR}/${dir}/${year}${month}/${day}"
cmd_manager "$flag" "$cmd"
done
done
done
}

function restore_pomodoro()
{
local flag="$1"
local decision
local difference
local cmd

flag=${flag:-'SILENT'}

if [[ -d "$decompress_path/pomodoro" ]]; then
restore_data_from_dir 'pomodoro'
restore_data_from_dir 'pomodoro' "$flag"

if [[ -f "$decompress_path/pomodoro/tags" ]]; then
if [[ -f "$KW_DATA_DIR/pomodoro/tags" ]] &&
Expand All @@ -200,7 +227,8 @@ function restore_pomodoro()
fi
case "$decision" in
1)
cp "$decompress_path/pomodoro/tags" "$KW_DATA_DIR/pomodoro/tags"
cmd="cp ${decompress_path}/pomodoro/tags ${KW_DATA_DIR}/pomodoro/tags"
cmd_manager "$flag" "$cmd"
;;
2)
return
Expand All @@ -212,7 +240,8 @@ function restore_pomodoro()
esac
return
fi
cp "$decompress_path/pomodoro/tags" "$KW_DATA_DIR/pomodoro/tags"
cmd="cp ${decompress_path}/pomodoro/tags ${KW_DATA_DIR}/pomodoro/tags"
cmd_manager "$flag" "$cmd"
fi
fi
}
Expand All @@ -228,8 +257,8 @@ function restore_statistics()
# and populates the options_values variable accordingly.
function parse_backup_options()
{
local long_options='help,restore:,force'
local short_options='h,r:,f'
local long_options='help,restore:,force,verbose'
local short_options='h,r:,f,v'

options="$(kw_parse "$short_options" "$long_options" "$@")"

Expand All @@ -243,6 +272,7 @@ function parse_backup_options()
options_values['RESTORE_PATH']=''
options_values['BACKUP_PATH']=''
options_values['FORCE']=''
options_values['VERBOSE']=''

eval "set -- $options"

Expand All @@ -264,6 +294,10 @@ function parse_backup_options()
options_values['FORCE']=1
shift
;;
--verbose | -v)
options_values['VERBOSE']=1
shift
;;
--)
shift
;;
Expand All @@ -284,5 +318,7 @@ function backup_help()
fi
printf '%s\n' 'kw backup:' \
' backup (<path>) - Create a compressed file containing kw data' \
' backup (-r | --restore) <path> [(-f | --force)] - Extract a tar.gz file into kw data directory'
' backup (-r | --restore) <path> [(-f | --force)] - Extract a tar.gz file into kw data directory' \
' backup (-v | --verbose) - Show a detailed output'

}

0 comments on commit 678d8cb

Please sign in to comment.