scale-catalog/functions/backup.sh

128 lines
5.1 KiB
Bash
Raw Normal View History

2022-07-25 02:29:58 +00:00
#!/bin/bash
backup(){
2022-07-28 19:31:48 +00:00
echo_backup+=("\n🄱 🄰 🄲 🄺 🅄 🄿 🅂")
2022-07-26 23:53:08 +00:00
echo_backup+=("Number of backups was set to $number_of_backups")
2022-07-25 02:29:58 +00:00
date=$(date '+%Y_%m_%d_%H_%M_%S')
2022-07-26 06:39:25 +00:00
[[ "$verbose" == "true" ]] && cli -c 'app kubernetes backup_chart_releases backup_name=''"'HeavyScript_"$date"'"' &> /dev/null && echo_backup+=(HeavyScript_"$date")
[[ -z "$verbose" ]] && echo_backup+=("\nNew Backup Name:") && cli -c 'app kubernetes backup_chart_releases backup_name=''"'HeavyScript_"$date"'"' | tail -n 1 &> /dev/null && echo_backup+=(HeavyScript_"$date")
2022-07-25 02:29:58 +00:00
mapfile -t list_backups < <(cli -c 'app kubernetes list_backups' | grep "HeavyScript_" | sort -t '_' -Vr -k2,7 | awk -F '|' '{print $2}'| tr -d " \t\r")
2022-07-27 02:20:16 +00:00
if [[ ${#list_backups[@]} -gt "$number_of_backups" ]]; then
2022-07-26 23:43:06 +00:00
echo_backup+=("\nDeleted the oldest backup(s) for exceeding limit:")
2022-07-25 02:29:58 +00:00
overflow=$(( ${#list_backups[@]} - "$number_of_backups" ))
mapfile -t list_overflow < <(cli -c 'app kubernetes list_backups' | grep "HeavyScript_" | sort -t '_' -V -k2,7 | awk -F '|' '{print $2}'| tr -d " \t\r" | head -n "$overflow")
for i in "${list_overflow[@]}"
do
2022-07-26 06:32:37 +00:00
cli -c 'app kubernetes delete_backup backup_name=''"'"$i"'"' &> /dev/null || echo_backup+=("Failed to delete $i")
echo_backup+=("$i")
2022-07-25 02:29:58 +00:00
done
fi
2022-07-26 06:32:37 +00:00
#Dump the echo_array, ensures all output is in a neat order.
for i in "${echo_backup[@]}"
do
echo -e "$i"
done
2022-07-25 02:29:58 +00:00
}
export -f backup
deleteBackup(){
2022-07-29 05:11:09 +00:00
while true
do
2022-07-29 05:22:06 +00:00
clear -x && echo "pulling all restore points.."
2022-07-29 05:32:45 +00:00
list_backups=$(cli -c 'app kubernetes list_backups' | sort -t '_' -Vr -k2,7 | tr -d " \t\r" | awk -F '|' '{print $2}' | nl -s ") " | column -t)
2022-07-29 05:22:06 +00:00
clear -x
if [[ -z "$list_backups" ]]; then
echo "No restore points available"
exit
else
title
echo -e "Choose a restore point to delete\nThese may be out of order if they are not HeavyScript backups"
fi
echo "$list_backups"
read -rt 120 -p "Please type a number: " selection
2022-07-29 05:33:44 +00:00
restore_point=$(echo "$list_backups" | grep ^"$selection)" | awk '{print $2}')
2022-07-29 05:22:06 +00:00
#Check for valid selection. If none, kill script
if [[ -z "$selection" ]]; then
echo "Your selection cannot be empty"
2022-07-29 05:32:01 +00:00
sleep 3
2022-07-29 05:22:06 +00:00
continue
elif [[ -z "$restore_point" ]]; then
echo "Invalid Selection: $selection, was not an option"
2022-07-29 05:32:01 +00:00
sleep 3
2022-07-29 05:22:06 +00:00
continue
fi
while true
do
2022-07-29 05:32:01 +00:00
clear -x
2022-07-29 05:22:06 +00:00
echo -e "\nWARNING:\nYou CANNOT go back after deleting your restore point"
echo -e "\n\nYou have chosen:\n$restore_point\n\nWould you like to continue?"
2022-07-29 05:32:01 +00:00
echo -e "1) Yes\n2) Exit\n"
read -rt 120 -p "Please type a number: " yesno
2022-07-29 05:22:06 +00:00
case $yesno in
2022-07-29 05:32:01 +00:00
1)
2022-07-29 05:22:06 +00:00
echo -e "\nDeleting $restore_point"
cli -c 'app kubernetes delete_backup backup_name=''"'"$restore_point"'"' &>/dev/null || { echo "Failed to delete backup.."; exit; }
echo "Sucessfully deleted"
break
;;
2022-07-29 05:32:01 +00:00
2)
2022-07-29 05:22:06 +00:00
echo "Exiting"
exit
;;
*)
2022-07-29 05:32:01 +00:00
echo "That was not an option, try again"
sleep 3
continue
2022-07-29 05:22:06 +00:00
;;
esac
done
while true
do
echo "Delete more?"
2022-07-29 05:32:01 +00:00
echo "1) Yes"
echo "2) No"
2022-07-29 05:22:06 +00:00
read -rt 120 -p "Please type a number: " yesno
case $yesno in
1)
break
;;
2)
exit
;;
*)
echo "$yesno was not an option, try again"
sleep 2
continue
;;
2022-07-29 05:11:09 +00:00
2022-07-29 05:22:06 +00:00
esac
2022-07-29 05:11:09 +00:00
2022-07-29 05:22:06 +00:00
done
done
2022-07-25 02:29:58 +00:00
}
export -f deleteBackup
restore(){
clear -x && echo "pulling restore points.."
list_backups=$(cli -c 'app kubernetes list_backups' | grep "HeavyScript_" | sort -t '_' -Vr -k2,7 | tr -d " \t\r" | awk -F '|' '{print $2}' | nl | column -t)
clear -x
[[ -z "$list_backups" ]] && echo "No HeavyScript restore points available" && exit || { title; echo "Choose a restore point" ; }
echo "$list_backups" && read -t 600 -p "Please type a number: " selection && restore_point=$(echo "$list_backups" | grep ^"$selection " | awk '{print $2}')
[[ -z "$selection" ]] && echo "Your selection cannot be empty" && exit #Check for valid selection. If none, kill script
[[ -z "$restore_point" ]] && echo "Invalid Selection: $selection, was not an option" && exit #Check for valid selection. If none, kill script
echo -e "\nWARNING:\nThis is NOT guranteed to work\nThis is ONLY supposed to be used as a LAST RESORT\nConsider rolling back your applications instead if possible" || { echo "FAILED"; exit; }
echo -e "\n\nYou have chosen:\n$restore_point\n\nWould you like to continue?" && echo -e "1 Yes\n2 No" && read -t 120 -p "Please type a number: " yesno || { echo "FAILED"; exit; }
if [[ $yesno == "1" ]]; then
echo -e "\nStarting Backup, this will take a LONG time." && cli -c 'app kubernetes restore_backup backup_name=''"'"$restore_point"'"' || echo "Restore FAILED"
elif [[ $yesno == "2" ]]; then
echo "You've chosen NO, killing script. Good luck."
else
echo "Invalid Selection"
fi
}
export -f restore