From 0445e02a8542340a8dcd48406d275c7f7df1ec3c Mon Sep 17 00:00:00 2001 From: David Tadokoro Date: Fri, 12 May 2023 19:03:21 -0300 Subject: [PATCH] src: backup: Adapt to kw_db When restoring a backup of kw data, if not using the `--force` flag, the statistics and pomodoro reports, and the kernel configs data are restored. This data is almost all the personal user data of kw (besides the kw config files). With the addition of `kw_db`, the file `KW_DATA_DIR/kw.db` needs to be restored as well. Also, the legacy metadata files and directories don't have the need to be restored and (in theory) should already be migrated into `kw_db`, leaving just `KW_DATA_DIR/kw.db` and the actual kernel config files to be restored. This commit does this by not calling the functions that used to restore the data mentioned in the first paragraph and adding and calling functions that restore the `kw_db` file and actual kernel config files. Note: As said before, this commit only affects the restoration without `--force` as, in this case, the full decompressed backup is just copied to `KW_DATA_DIR`. Signed-off-by: David Tadokoro --- src/backup.sh | 68 ++++++++++++++++++++++++++++++++++++++++++-- tests/backup_test.sh | 60 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 3 deletions(-) diff --git a/src/backup.sh b/src/backup.sh index c2a64d89f..4093d21c5 100644 --- a/src/backup.sh +++ b/src/backup.sh @@ -104,9 +104,8 @@ function restore_backup() fi if [[ -z "${options_values['FORCE']}" ]]; then - restore_config "$flag" - restore_pomodoro "$flag" - restore_statistics + restore_database "$flag" + restore_config_files "$flag" fi success "Backup restored at $KW_DATA_DIR" @@ -255,6 +254,69 @@ function restore_statistics() fi } +# This function restores the kw SQLite database from `KW_DATA_DIR`. +# +# @flag: Flag to control function output +function restore_database() +{ + local flag="$1" + local cmd + local ret + + flag=${flag:-'SILENT'} + + if [[ -f "${decompress_path}/kw.db" ]]; then + cmd="cp ${decompress_path}/kw.db ${KW_DATA_DIR}/kw.db" + cmd_manager "$flag" "$cmd" + ret="$?" + if [[ "$ret" != 0 ]]; then + complain "Couldn't restore database from ${decompress_path}/kw.db to ${KW_DATA_DIR}/kw.db" + exit "$ret" + fi + fi +} + +# This function restores the kernel config files from `KW_DATA_DIR`. +# +# @flag: Flag to control function output +function restore_config_files() +{ + local flag="$1" + local config_file_paths + local config_filename + local cmd + local ret + + flag=${flag:-'SILENT'} + + # Only get config file paths that are direct children of `configs` in backup. + # We sort the paths to avoid flaky tests. + config_file_paths=$(find "${decompress_path}/configs" -maxdepth 1 -type f | sort --dictionary-order) + + if [[ -n "$config_file_paths" ]]; then + if [[ ! -d "${KW_DATA_DIR}/configs" ]]; then + cmd="mkdir --parents ${KW_DATA_DIR}/configs" + cmd_manager "$flag" "$cmd" + ret="$?" + if [[ "$ret" != 0 ]]; then + complain "Couldn't create ${KW_DATA_DIR}/configs" + exit "$ret" + fi + fi + + while IFS=$'\n' read -r config_file_path; do + config_filename="${config_file_path##*/}" # get just the file name + cmd="cp ${config_file_path} ${KW_DATA_DIR}/configs/${config_filename}" + cmd_manager "$flag" "$cmd" + ret="$?" + if [[ "$ret" != 0 ]]; then + complain "Couldn't copy ${config_file_path} to ${KW_DATA_DIR}/configs/${config_filename}" + exit "$ret" + fi + done <<< "$config_file_paths" + fi +} + # This function parses the arguments provided to 'kw backup', validates them, # and populates the options_values variable accordingly. function parse_backup_options() diff --git a/tests/backup_test.sh b/tests/backup_test.sh index 9469dd8d2..a23998cfa 100755 --- a/tests/backup_test.sh +++ b/tests/backup_test.sh @@ -220,4 +220,64 @@ function test_parse_backup_options() } } +test_restore_database() +{ + local output + local expected + + # No database file in backup + expected='' + output=$(restore_database 'TEST_MODE') + assert_equals_helper 'Without database file should not try to restore' "$LINENO" "$expected" "$output" + + # Database file in backup + touch "${decompress_path}/kw.db" + expected="cp ${decompress_path}/kw.db ${KW_DATA_DIR}/kw.db" + output=$(restore_database 'TEST_MODE') + assert_equals_helper 'Wrong command issued' "$LINENO" "$expected" "$output" +} + +test_restore_config_files() +{ + local output + local expected + + # No config files in `KW_DATA_DIR/configs` + expected='' + output=$(restore_config_files 'TEST-MODE') + assert_equals_helper 'Without config files should not try to restore' "$LINENO" "$expected" "$output" + + # One config file in `KW_DATA_DIR/configs` + touch "${decompress_path}/configs/kconfig1" + expected="cp ${decompress_path}/configs/kconfig1 ${KW_DATA_DIR}/configs/kconfig1" + output=$(restore_config_files 'TEST-MODE') + assert_equals_helper 'Without config files should not try to restore' "$LINENO" "$expected" "$output" + + # Multiple config files in `KW_DATA_DIR/configs` + touch "${decompress_path}/configs/kconfig2" + touch "${decompress_path}/configs/kconfig3" + touch "${decompress_path}/configs/kconfig4" + expected="cp ${decompress_path}/configs/kconfig1 ${KW_DATA_DIR}/configs/kconfig1"$'\n' + expected+="cp ${decompress_path}/configs/kconfig2 ${KW_DATA_DIR}/configs/kconfig2"$'\n' + expected+="cp ${decompress_path}/configs/kconfig3 ${KW_DATA_DIR}/configs/kconfig3"$'\n' + expected+="cp ${decompress_path}/configs/kconfig4 ${KW_DATA_DIR}/configs/kconfig4" + output=$(restore_config_files 'TEST-MODE') + assert_equals_helper 'Without config files should not try to restore' "$LINENO" "$expected" "$output" + + # Test with directories of depth 1 or files of depth greater than 1 + mkdir --parents "${decompress_path}/configs/configs" + touch "${decompress_path}/configs/configs/kconfig1" + mkdir --parents "${decompress_path}/configs/legacy_configs" + touch "${decompress_path}/configs/legacy_configs/kconfig1" + mkdir --parents "${decompress_path}/configs/metadata" + touch "${decompress_path}/configs/metadata/kconfig1" + mkdir --parents "${decompress_path}/configs/legacy_metadata" + touch "${decompress_path}/configs/legacy_metadata/kconfig1" + mkdir --parents "${decompress_path}/configs/other_dir" + touch "${decompress_path}/configs/other_dir/other_file" + touch "${decompress_path}/configs/other_dir/another_file" + output=$(restore_config_files 'TEST-MODE') + assert_equals_helper 'No file of depth greater than 1 or directory should be restored' "$LINENO" "$expected" "$output" +} + invoke_shunit