linutil/core/tabs/utils/timeshift.sh

163 lines
5.1 KiB
Bash
Raw Normal View History

2024-09-18 15:15:57 +01:00
#!/bin/sh -e
. ../common-script.sh
# Function to install Timeshift
install_timeshift() {
clear
printf "%b\n" "${YELLOW}Checking if Timeshift is installed...${RC}"
if ! command_exists timeshift; then
2024-09-19 19:05:36 +01:00
case "$PACKAGER" in
2024-09-18 15:15:57 +01:00
pacman)
2024-09-19 19:05:36 +01:00
"$ESCALATION_TOOL" "${PACKAGER}" -S --noconfirm timeshift
2024-09-18 15:15:57 +01:00
;;
2024-10-13 02:42:34 +01:00
dnf|zypper|apt-get|nala)
2024-09-19 19:05:36 +01:00
"$ESCALATION_TOOL" "${PACKAGER}" install -y timeshift
2024-09-18 15:15:57 +01:00
;;
2024-10-13 02:42:34 +01:00
*)
printf "%b\n" "${RED}Unsupported pacakge manager.${RC}"
2024-09-18 15:15:57 +01:00
esac
else
printf "%b\n" "${GREEN}Timeshift is already installed.${RC}"
2024-09-18 15:15:57 +01:00
fi
}
# Function to display the menu
display_menu() {
clear
2024-09-18 18:53:47 +01:00
printf "%b\n" "${CYAN}Timeshift CLI Automation${RC}"
printf "%b\n" "${CYAN}1) List Snapshots${RC}"
printf "%b\n" "${CYAN}2) List Devices${RC}"
printf "%b\n" "${CYAN}3) Create Snapshot${RC}"
printf "%b\n" "${CYAN}4) Restore Snapshot${RC}"
printf "%b\n" "${CYAN}5) Delete Snapshot${RC}"
printf "%b\n" "${CYAN}6) Delete All Snapshots${RC}"
printf "%b\n" "${CYAN}7) Exit${RC}"
2024-09-18 15:15:57 +01:00
}
# Function to list snapshots
list_snapshots() {
2024-09-18 18:53:47 +01:00
printf "%b\n" "${CYAN}Listing snapshots...${RC}"
2024-09-19 19:05:36 +01:00
"$ESCALATION_TOOL" timeshift --list-snapshots
2024-09-18 15:15:57 +01:00
}
# Function to list devices
list_devices() {
2024-09-18 18:53:47 +01:00
printf "%b\n" "${CYAN}Listing available devices...${RC}"
2024-09-19 19:05:36 +01:00
"$ESCALATION_TOOL" timeshift --list-devices
2024-09-18 15:15:57 +01:00
}
# Function to create a new snapshot
create_snapshot() {
printf "%b" "${CYAN}Enter a comment for the snapshot (optional): ${RC}"
2024-09-18 18:53:47 +01:00
read -r COMMENT
printf "%b" "${CYAN}Enter snapshot tag (O,B,H,D,W,M) (leave empty for no tag): ${RC}"
2024-09-18 18:53:47 +01:00
read -r TAG
2024-09-18 15:15:57 +01:00
if [ -z "$COMMENT" ] && [ -z "$TAG" ]; then
printf "%b\n" "${CYAN}Creating snapshot with no comment or tag...${RC}"
2024-09-19 19:05:36 +01:00
"$ESCALATION_TOOL" timeshift --create
2024-09-18 15:15:57 +01:00
elif [ -z "$TAG" ]; then
printf "%b\n" "${CYAN}Creating snapshot with no tag...${RC}"
2024-09-19 19:05:36 +01:00
"$ESCALATION_TOOL" timeshift --create --comments "$COMMENT"
2024-09-18 15:15:57 +01:00
else
printf "%b\n" "${CYAN}Creating snapshot with tag: $TAG...${RC}"
2024-09-19 19:05:36 +01:00
"$ESCALATION_TOOL" timeshift --create --comments "$COMMENT" --tags "$TAG"
2024-09-18 15:15:57 +01:00
fi
if [ $? -eq 0 ]; then
printf "%b\n" "${GREEN}Snapshot created successfully.${RC}"
2024-09-18 15:15:57 +01:00
else
printf "%b\n" "${RED}Snapshot creation failed.${RC}"
2024-09-18 15:15:57 +01:00
fi
}
# Function to restore a snapshot
restore_snapshot() {
list_snapshots
printf "%b" "${CYAN}Enter the snapshot name you want to restore: ${RC}"
2024-09-18 18:53:47 +01:00
read -r SNAPSHOT
printf "%b" "${CYAN}Enter the target device (e.g., /dev/sda1): ${RC}"
2024-09-18 18:53:47 +01:00
read -r TARGET_DEVICE
printf "%b" "${CYAN}Do you want to skip GRUB reinstall? (y/N): ${RC}"
2024-09-18 18:53:47 +01:00
read -r SKIP_GRUB
2024-09-18 15:15:57 +01:00
if [ "$SKIP_GRUB" = "y" ] || [ "$SKIP_GRUB" = "Y" ]; then
2024-09-19 19:05:36 +01:00
"$ESCALATION_TOOL" timeshift --restore --snapshot "$SNAPSHOT" --target-device "$TARGET_DEVICE" --skip-grub --yes
2024-09-18 15:15:57 +01:00
else
printf "%b\n" "${CYAN}Enter GRUB device (e.g., /dev/sda): ${RC}"
2024-09-18 18:53:47 +01:00
read -r GRUB_DEVICE
2024-09-19 19:05:36 +01:00
"$ESCALATION_TOOL" timeshift --restore --snapshot "$SNAPSHOT" --target-device "$TARGET_DEVICE" --grub-device "$GRUB_DEVICE" --yes
2024-09-18 15:15:57 +01:00
fi
if [ $? -eq 0 ]; then
2024-09-18 18:53:47 +01:00
printf "%b\n" "${GREEN}Snapshot restored successfully.${RC}"
2024-09-18 15:15:57 +01:00
else
2024-09-18 18:53:47 +01:00
printf "%b\n" "${RED}Snapshot restore failed.${RC}"
2024-09-18 15:15:57 +01:00
fi
}
# Function to delete a snapshot
delete_snapshot() {
list_snapshots
printf "%b" "${CYAN}Enter the snapshot name you want to delete: ${RC}"
2024-09-18 18:53:47 +01:00
read -r SNAPSHOT
2024-09-18 15:15:57 +01:00
2024-09-18 18:53:47 +01:00
printf "%b\n" "${YELLOW}Deleting snapshot $SNAPSHOT...${RC}"
2024-09-19 19:05:36 +01:00
"$ESCALATION_TOOL" timeshift --delete --snapshot "$SNAPSHOT" --yes
2024-09-18 15:15:57 +01:00
if [ $? -eq 0 ]; then
2024-09-18 18:53:47 +01:00
printf "%b\n" "${GREEN}Snapshot deleted successfully.${RC}"
2024-09-18 15:15:57 +01:00
else
2024-09-18 18:53:47 +01:00
printf "%b\n" "${RED}Snapshot deletion failed.${RC}"
2024-09-18 15:15:57 +01:00
fi
}
# Function to delete all snapshots
delete_all_snapshots() {
2024-09-18 18:53:47 +01:00
printf "%b\n" "${RED}WARNING: This will delete all snapshots!${RC}"
printf "%b" "${CYAN}Are you sure? (y/N): ${RC}"
2024-09-18 18:53:47 +01:00
read -r CONFIRMATION
2024-09-18 15:15:57 +01:00
if [ "$CONFIRMATION" = "y" ] || [ "$CONFIRMATION" = "Y" ]; then
printf "%b\n" "${CYAN}Deleting all snapshots...${RC}"
2024-09-19 19:05:36 +01:00
"$ESCALATION_TOOL" timeshift --delete-all --yes
2024-09-18 15:15:57 +01:00
if [ $? -eq 0 ]; then
printf "%b\n" "${GREEN}All snapshots deleted successfully.${RC}"
else
printf "%b\n" "${RED}Failed to delete snapshots.${RC}"
fi
else
printf "%b\n" "${RED}Operation cancelled.${RC}"
fi
}
main_menu() {
while true; do
display_menu
printf "%b\n" "${CYAN}Select an option (1-7): ${RC}"
read -r OPTION
case $OPTION in
1) list_snapshots ;;
2) list_devices ;;
3) create_snapshot ;;
4) restore_snapshot ;;
5) delete_snapshot ;;
6) delete_all_snapshots ;;
7) printf "%b\n" "${GREEN}Exiting...${RC}"; exit 0 ;;
*) printf "%b\n" "${RED}Invalid option. Please try again.${RC}" ;;
esac
printf "%b\n" "${CYAN}Press Enter to continue...${RC}"
read -r dummy
done
2024-09-18 15:15:57 +01:00
}
checkEnv
checkEscalationTool
install_timeshift
main_menu