refactor: backup functions

Signed-off-by: Dominik Willner th33xitus@gmail.com
This commit is contained in:
th33xitus
2022-04-23 14:45:38 +02:00
parent cae3b069d7
commit 5fa1dad2ca
2 changed files with 48 additions and 41 deletions

View File

@@ -17,14 +17,14 @@ function get_date(){
} }
function check_for_backup_dir(){ function check_for_backup_dir(){
if [ ! -d "${BACKUP_DIR}" ]; then [ -d "${BACKUP_DIR}" ] && return
status_msg "Create KIAUH backup directory ..." status_msg "Create KIAUH backup directory ..."
mkdir -p "${BACKUP_DIR}" && ok_msg "Directory created!" mkdir -p "${BACKUP_DIR}" && ok_msg "Directory created!"
fi
} }
function backup_before_update(){ function backup_before_update(){
read_kiauh_ini read_kiauh_ini
# shellcheck disable=SC2154
local state="${backup_before_update}" local state="${backup_before_update}"
[ "${state}" = "false" ] && return [ "${state}" = "false" ] && return
backup_"${1}" backup_"${1}"
@@ -52,7 +52,8 @@ function backup_klipper_config_dir(){
status_msg "Create backup of the Klipper config directory ..." status_msg "Create backup of the Klipper config directory ..."
config_folder_name="$(echo "${KLIPPER_CONFIG}" | rev | cut -d"/" -f1 | rev)" config_folder_name="$(echo "${KLIPPER_CONFIG}" | rev | cut -d"/" -f1 | rev)"
mkdir -p "${BACKUP_DIR}/${config_folder_name}/${current_date}" mkdir -p "${BACKUP_DIR}/${config_folder_name}/${current_date}"
cp -r "${KLIPPER_CONFIG}" "${_}" && ok_msg "Backup complete!" cp -r "${KLIPPER_CONFIG}" "${_}"
print_confirm "Configuration directory backup complete!"
else else
ok_msg "No config directory found! Skipping backup ..." ok_msg "No config directory found! Skipping backup ..."
fi fi
@@ -60,21 +61,24 @@ function backup_klipper_config_dir(){
function backup_moonraker_database(){ function backup_moonraker_database(){
check_for_backup_dir check_for_backup_dir
local current_date local current_date databases target_dir
if ls -d "${HOME}"/.moonraker_database* 2>/dev/null 1>&2; then
databases=$(find "${HOME}" -maxdepth 1 -type d -regextype posix-extended -regex "${HOME}/.moonraker_database(_[^0])?[0-9]*" | sort)
if [ -n "${databases}" ]; then
current_date=$(get_date) current_date=$(get_date)
target_dir="${BACKUP_DIR}/moonraker_database_backup/${current_date}"
status_msg "Timestamp: ${current_date}" status_msg "Timestamp: ${current_date}"
mkdir -p "${BACKUP_DIR}/mr_db_backup/${current_date}" mkdir -p "${target_dir}"
for database in $(ls -d ${HOME}/.moonraker_database*) for database in ${databases}; do
do
status_msg "Create backup of ${database} ..." status_msg "Create backup of ${database} ..."
cp -r "${database}" "${BACKUP_DIR}/mr_db_backup/${current_date}" cp -r "${database}" "${target_dir}"
ok_msg "Done!" ok_msg "Done!"
done done
ok_msg "Backup complete!\n" print_confirm "Moonraker database backup complete!"
else else
ok_msg "No Moonraker database found! Skipping backup ..." print_error "No Moonraker database found! Skipping backup ..."
fi fi
return
} }
function backup_klipper(){ function backup_klipper(){
@@ -85,7 +89,8 @@ function backup_klipper(){
current_date=$(get_date) current_date=$(get_date)
status_msg "Timestamp: ${current_date}" status_msg "Timestamp: ${current_date}"
mkdir -p "${BACKUP_DIR}/klipper-backups/${current_date}" mkdir -p "${BACKUP_DIR}/klipper-backups/${current_date}"
cp -r "${KLIPPER_DIR}" "${_}" && cp -r "${KLIPPY_ENV}" "${_}" && ok_msg "Backup complete!" cp -r "${KLIPPER_DIR}" "${_}" && cp -r "${KLIPPY_ENV}" "${_}"
print_confirm "Klipper backup complete!"
else else
print_error "Can't backup klipper and/or klipper-env directory! Not found!" print_error "Can't backup klipper and/or klipper-env directory! Not found!"
fi fi
@@ -99,7 +104,8 @@ function backup_mainsail(){
current_date=$(get_date) current_date=$(get_date)
status_msg "Timestamp: ${current_date}" status_msg "Timestamp: ${current_date}"
mkdir -p "${BACKUP_DIR}/mainsail-backups/${current_date}" mkdir -p "${BACKUP_DIR}/mainsail-backups/${current_date}"
cp -r "${MAINSAIL_DIR}" "${_}" && ok_msg "Backup complete!" cp -r "${MAINSAIL_DIR}" "${_}"
print_confirm "Mainsail backup complete!"
else else
print_error "Can't backup mainsail directory! Not found!" print_error "Can't backup mainsail directory! Not found!"
fi fi
@@ -113,7 +119,8 @@ function backup_fluidd(){
current_date=$(get_date) current_date=$(get_date)
status_msg "Timestamp: ${current_date}" status_msg "Timestamp: ${current_date}"
mkdir -p "${BACKUP_DIR}/fluidd-backups/${current_date}" mkdir -p "${BACKUP_DIR}/fluidd-backups/${current_date}"
cp -r "${FLUIDD_DIR}" "${_}" && ok_msg "Backup complete!" cp -r "${FLUIDD_DIR}" "${_}"
print_confirm "Fluidd backup complete!"
else else
print_error "Can't backup fluidd directory! Not found!" print_error "Can't backup fluidd directory! Not found!"
fi fi
@@ -127,7 +134,8 @@ function backup_moonraker(){
current_date=$(get_date) current_date=$(get_date)
status_msg "Timestamp: ${current_date}" status_msg "Timestamp: ${current_date}"
mkdir -p "${BACKUP_DIR}/moonraker-backups/${current_date}" mkdir -p "${BACKUP_DIR}/moonraker-backups/${current_date}"
cp -r "${MOONRAKER_DIR}" "${_}" && cp -r "${MOONRAKER_ENV}" "${_}" && ok_msg "Backup complete!" cp -r "${MOONRAKER_DIR}" "${_}" && cp -r "${MOONRAKER_ENV}" "${_}"
print_confirm "Moonraker backup complete!"
else else
print_error "Can't backup moonraker and/or moonraker-env directory! Not found!" print_error "Can't backup moonraker and/or moonraker-env directory! Not found!"
fi fi
@@ -142,7 +150,7 @@ function backup_octoprint(){
status_msg "Timestamp: ${current_date}" status_msg "Timestamp: ${current_date}"
mkdir -p "${BACKUP_DIR}/octoprint-backups/${current_date}" mkdir -p "${BACKUP_DIR}/octoprint-backups/${current_date}"
cp -r "${OCTOPRINT_DIR}" "${_}" && cp -r "${OCTOPRINT_CFG_DIR}" "${_}" cp -r "${OCTOPRINT_DIR}" "${_}" && cp -r "${OCTOPRINT_CFG_DIR}" "${_}"
ok_msg "Backup complete!" print_confirm " OctoPrint backup complete!"
else else
print_error "Can't backup OctoPrint and/or .octoprint directory!\n Not found!" print_error "Can't backup OctoPrint and/or .octoprint directory!\n Not found!"
fi fi
@@ -157,13 +165,13 @@ function backup_klipperscreen(){
status_msg "Timestamp: ${current_date}" status_msg "Timestamp: ${current_date}"
mkdir -p "${BACKUP_DIR}/klipperscreen-backups/${current_date}" mkdir -p "${BACKUP_DIR}/klipperscreen-backups/${current_date}"
cp -r "${KLIPPERSCREEN_DIR}" "${_}" cp -r "${KLIPPERSCREEN_DIR}" "${_}"
ok_msg "Backup complete!" print_confirm "KlipperScreen backup complete!"
else else
print_error "Can't backup KlipperScreen directory!\n Not found!" print_error "Can't backup KlipperScreen directory!\n Not found!"
fi fi
} }
function backup_MoonrakerTelegramBot(){ function backup_telegram_bot(){
local current_date local current_date
if [ -d "${MOONRAKER_TELEGRAM_BOT_DIR}" ] ; then if [ -d "${MOONRAKER_TELEGRAM_BOT_DIR}" ] ; then
status_msg "Creating MoonrakerTelegramBot backup ..." status_msg "Creating MoonrakerTelegramBot backup ..."
@@ -172,7 +180,7 @@ function backup_MoonrakerTelegramBot(){
status_msg "Timestamp: ${current_date}" status_msg "Timestamp: ${current_date}"
mkdir -p "${BACKUP_DIR}/MoonrakerTelegramBot-backups/${current_date}" mkdir -p "${BACKUP_DIR}/MoonrakerTelegramBot-backups/${current_date}"
cp -r "${MOONRAKER_TELEGRAM_BOT_DIR}" "${_}" cp -r "${MOONRAKER_TELEGRAM_BOT_DIR}" "${_}"
ok_msg "Backup complete!" print_confirm "MoonrakerTelegramBot backup complete!"
else else
print_error "Can't backup MoonrakerTelegramBot directory!\n Not found!" print_error "Can't backup MoonrakerTelegramBot directory!\n Not found!"
fi fi

View File

@@ -13,20 +13,19 @@ set -e
function backup_ui(){ function backup_ui(){
top_border top_border
echo -e "| $(title_msg "~~~~~~~~~~~~~~ [ Backup Menu ] ~~~~~~~~~~~~~~") | " echo -e "| $(title_msg "~~~~~~~~~~~~~~ [ Backup Menu ] ~~~~~~~~~~~~~~") |"
hr hr
echo -e "| ${yellow}Backup location: ~/kiauh-backups${white} | " echo -e "| ${yellow}INFO: Backups are located in '~/kiauh-backups'${white} |"
hr hr
echo -e "| Configuration folder: | Klipper Webinterface: | " echo -e "| Klipper & API: | Touchscreen GUI: |"
echo -e "| 0) [Klipper configs] | 4) [Mainsail] | " echo -e "| 1) [Klipper] | 7) [KlipperScreen] |"
echo -e "| | 5) [Fluidd] | " echo -e "| 2) [Moonraker] | |"
echo -e "| Firmware: | | " echo -e "| 3) [Configuration Folder] | 3rd Party Webinterface: |"
echo -e "| 1) [Klipper] | HDMI Screen: | " echo -e "| 4) [Moonraker Database] | 8) [OctoPrint] |"
echo -e "| | 6) [KlipperScreen] | " echo -e "| | |"
echo -e "| Klipper API: | | " echo -e "| Klipper Webinterface: | Other: |"
echo -e "| 2) [Moonraker] | Other: | " echo -e "| 5) [Mainsail] | 9) [Telegram Bot] |"
echo -e "| 3) [Moonraker DB] | 7) [OctoPrint] | " echo -e "| 6) [Fluidd] | |"
echo -e "| | 8) [MoonrakerTelegramBot] | "
back_footer back_footer
} }
@@ -35,24 +34,24 @@ function backup_menu(){
while true; do while true; do
read -p "${cyan}####### Perform action:${white} " action read -p "${cyan}####### Perform action:${white} " action
case "${action}" in case "${action}" in
0)
do_action "backup_klipper_config_dir" "backup_ui";;
1) 1)
do_action "backup_klipper" "backup_ui";; do_action "backup_klipper" "backup_ui";;
2) 2)
do_action "backup_moonraker" "backup_ui";; do_action "backup_moonraker" "backup_ui";;
3) 3)
do_action "backup_moonraker_database" "backup_ui";; do_action "backup_klipper_config_dir" "backup_ui";;
4) 4)
do_action "backup_mainsail" "backup_ui";; do_action "backup_moonraker_database" "backup_ui";;
5) 5)
do_action "backup_fluidd" "backup_ui";; do_action "backup_mainsail" "backup_ui";;
6) 6)
do_action "backup_klipperscreen" "backup_ui";; do_action "backup_fluidd" "backup_ui";;
7) 7)
do_action "backup_octoprint" "backup_ui";; do_action "backup_klipperscreen" "backup_ui";;
8) 8)
do_action "backup_MoonrakerTelegramBot" "backup_ui";; do_action "backup_octoprint" "backup_ui";;
9)
do_action "backup_telegram_bot" "backup_ui";;
B|b) B|b)
clear; main_menu; break;; clear; main_menu; break;;
*) *)