linutil/tabs/utils/timeshift.sh

163 lines
4.8 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
case ${PACKAGER} in
pacman)
$ESCALATION_TOOL "${PACKAGER}" -S --noconfirm timeshift
;;
*)
$ESCALATION_TOOL "${PACKAGER}" install -y timeshift
;;
esac
else
echo "Timeshift is already installed."
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}\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-"
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}"
printf "%b\n" "${CYAN}\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-"
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-18 15:15:57 +01:00
$ESCALATION_TOOL timeshift --list-snapshots
}
# Function to list devices
list_devices() {
2024-09-18 18:53:47 +01:00
printf "%b\n" "${CYAN}Listing available devices...${RC}"
2024-09-18 15:15:57 +01:00
$ESCALATION_TOOL timeshift --list-devices
}
# Function to create a new snapshot
create_snapshot() {
2024-09-18 18:53:47 +01:00
printf "%b" "${CYAN}Enter a comment for the snapshot (optional): ${RC}"
read -r COMMENT
printf "%b" "${CYAN}Enter snapshot tag (O,B,H,D,W,M) (leave empty for no tag): ${RC}"
read -r TAG
2024-09-18 15:15:57 +01:00
if [ -z "$COMMENT" ] && [ -z "$TAG" ]; then
echo "Creating snapshot with no comment or tag..."
$ESCALATION_TOOL timeshift --create
elif [ -z "$TAG" ]; then
echo "Creating snapshot with no tag..."
$ESCALATION_TOOL timeshift --create --comments "$COMMENT"
else
echo "Creating snapshot with tag: $TAG..."
$ESCALATION_TOOL timeshift --create --comments "$COMMENT" --tags "$TAG"
fi
if [ $? -eq 0 ]; then
echo "Snapshot created successfully."
else
echo "Snapshot creation failed."
fi
}
# Function to restore a snapshot
restore_snapshot() {
list_snapshots
2024-09-18 18:53:47 +01:00
printf "%b" "${CYAN}Enter the snapshot name you want to restore: ${RC}"
read -r SNAPSHOT
printf "%b" "${CYAN}Enter the target device (e.g., /dev/sda1): ${RC}"
read -r TARGET_DEVICE
printf "%b" "${CYAN}Do you want to skip GRUB reinstall? (yes/no): ${RC}"
read -r SKIP_GRUB
2024-09-18 15:15:57 +01:00
if [ "$SKIP_GRUB" = "yes" ]; then
$ESCALATION_TOOL timeshift --restore --snapshot "$SNAPSHOT" --target-device "$TARGET_DEVICE" --skip-grub --yes
else
2024-09-18 18:53:47 +01:00
printf "%b" "${CYAN}Enter GRUB device (e.g., /dev/sda): ${RC}"
read -r GRUB_DEVICE
2024-09-18 15:15:57 +01:00
$ESCALATION_TOOL timeshift --restore --snapshot "$SNAPSHOT" --target-device "$TARGET_DEVICE" --grub-device "$GRUB_DEVICE" --yes
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
2024-09-18 18:53:47 +01:00
printf "%b" "${CYAN}Enter the snapshot name you want to delete: ${RC}"
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-18 15:15:57 +01:00
$ESCALATION_TOOL timeshift --delete --snapshot "$SNAPSHOT" --yes
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? (yes/no): ${RC}"
read -r CONFIRMATION
2024-09-18 15:15:57 +01:00
if [ "$CONFIRMATION" = "yes" ]; then
echo "Deleting all snapshots..."
$ESCALATION_TOOL timeshift --delete-all --yes
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
2024-09-18 18:53:47 +01:00
printf "%b" "${CYAN}Select an option (1-7): ${RC}"
read -r OPTION
2024-09-18 15:15:57 +01:00
case $OPTION in
1) list_snapshots ;;
2) list_devices ;;
3) create_snapshot ;;
4) restore_snapshot ;;
5) delete_snapshot ;;
6) delete_all_snapshots ;;
2024-09-18 18:53:47 +01:00
7) printf "%b\n" "${GREEN}Exiting...${RC}"; exit 0 ;;
*) printf "%b\n" "${RED}Invalid option. Please try again.${RC}" ;;
2024-09-18 15:15:57 +01:00
esac
2024-09-18 18:53:47 +01:00
printf "%b" "${CYAN}Press Enter to continue...${RC}"
read -r dummy
2024-09-18 15:15:57 +01:00
done
}
checkEnv
checkEscalationTool
install_timeshift
main_menu