Skip to content

Commit

Permalink
src: backup: Adapt to kw_db
Browse files Browse the repository at this point in the history
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 <davidbtadokoro@usp.br>
  • Loading branch information
davidbtadokoro committed May 12, 2023
1 parent c4f6920 commit 0445e02
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 3 deletions.
68 changes: 65 additions & 3 deletions src/backup.sh
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
60 changes: 60 additions & 0 deletions tests/backup_test.sh
Expand Up @@ -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

0 comments on commit 0445e02

Please sign in to comment.