This is a one-click auxiliary script that integrates backup and restore functions, supporting the backup of critical system data, configuration files, and software lists, as well as corresponding restore operations. The script includes interactive prompts, eliminating the need to manually input complex commands; it can be run directly.
Script Name: sys_backup_restore.sh
#!/bin/bash# System backup and restore one-click auxiliary script# Supports: backup of software lists, configuration files, critical data; restore software, configuration, kernel rollback guide# Configure backup path (customizable)BACKUP_ROOT="$HOME/Documents/system_backup"TIMESTAMP=$(date +%Y%m%d_%H%M%S) # Timestamp to avoid backup overwriteBACKUP_DIR="$BACKUP_ROOT/backup_$TIMESTAMP"# Color definitions (for better readability)RED="\033[31m"GREEN="\033[32m"YELLOW="\033[33m"BLUE="\033[34m"NC="\033[0m" # Reset color# Initialize backup directoryinit_backup_dir() { mkdir -p "$BACKUP_DIR" mkdir -p "$BACKUP_DIR/packages" mkdir -p "$BACKUP_DIR/config" mkdir -p "$BACKUP_DIR/documents" mkdir -p "$BACKUP_DIR/system"}# 1. Perform full backupperform_backup() { echo -e "\n${BLUE}=== Starting system backup ===${NC}" init_backup_dir # Backup installed software list echo -e "\n${YELLOW}→ Backing up package list...${NC}" dpkg --get-selections > "$BACKUP_DIR/packages/installed_packages.txt" apt-mark showmanual > "$BACKUP_DIR/packages/manual_installed.txt" # Manually installed software (excluding dependencies) # Backup user configuration files echo -e "${YELLOW}→ Backing up user configuration files...${NC}" cp -r "$HOME/.config" "$BACKUP_DIR/config/" cp -r "$HOME/.bashrc" "$BACKUP_DIR/config/" # Terminal configuration cp -r "$HOME/.local/share/applications" "$BACKUP_DIR/config/" # Application shortcuts # Backup critical documents echo -e "${YELLOW}→ Backing up personal documents...${NC}" [ -d "$HOME/Documents" ] && cp -r "$HOME/Documents" "$BACKUP_DIR/documents/" [ -d "$HOME/Downloads" ] && cp -r "$HOME/Downloads" "$BACKUP_DIR/documents/" # Backup critical system configurations (software sources, network, etc.) echo -e "${YELLOW}→ Backing up system configurations...${NC}" [ -d "/etc/apt/sources.list.d" ] && sudo cp -r "/etc/apt/sources.list.d" "$BACKUP_DIR/system/" [ -f "/etc/apt/sources.list" ] && sudo cp "/etc/apt/sources.list" "$BACKUP_DIR/system/" [ -d "/etc/netplan" ] && sudo cp -r "/etc/netplan" "$BACKUP_DIR/system/" # Network configuration # Generate backup checksum (for integrity verification) echo -e "${YELLOW}→ Generating backup checksum...${NC}" find "$BACKUP_DIR" -type f -print0 | xargs -0 md5sum > "$BACKUP_DIR/backup_checksum.md5" echo -e "\n${GREEN}=== Backup completed! Backup path: $BACKUP_DIR ${NC}" echo -e "${GREEN}Note: It is recommended to copy this directory to a USB drive or cloud storage for backup${NC}"}# 2. Perform restore operationperform_restore() { echo -e "\n${BLUE}=== Starting restore operation ===${NC}" # Select backup directory echo -e "\n${YELLOW}Please select the backup directory to restore (enter full path):${NC}" read -r backup_path if [ ! -d "$backup_path" ] || [ ! -f "$backup_path/backup_checksum.md5" ]; then echo -e "${RED}Error: Invalid backup directory (missing checksum file)${NC}" return 1 fi # Verify backup integrity echo -e "\n${YELLOW}→ Verifying backup integrity...${NC}" if ! md5sum -c "$backup_path/backup_checksum.md5" >/dev/null 2>&1; then echo -e "${RED}Warning: Backup files are incomplete or corrupted, do you want to continue restoring? [y/N]${NC}" read -r confirm if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then echo -e "${YELLOW}Restore canceled${NC}" return 1 fi fi # Restore packages echo -e "\n${YELLOW}→ Restoring packages (may take a while)...${NC}" sudo apt install -y dselect sudo dpkg --set-selections < "$backup_path/packages/installed_packages.txt" sudo dselect install # Restore user configuration echo -e "${YELLOW}→ Restoring user configuration files...${NC}" [ -d "$backup_path/config/.config" ] && cp -r "$backup_path/config/.config" "$HOME/" [ -f "$backup_path/config/.bashrc" ] && cp "$backup_path/config/.bashrc" "$HOME/" [ -d "$backup_path/config/applications" ] && cp -r "$backup_path/config/applications" "$HOME/.local/share/" # Restore system configurations (software sources, network) echo -e "${YELLOW}→ Restoring system configurations...${NC}" [ -d "$backup_path/system/sources.list.d" ] && sudo cp -r "$backup_path/system/sources.list.d" "/etc/apt/" [ -f "$backup_path/system/sources.list" ] && sudo cp "$backup_path/system/sources.list" "/etc/apt/" [ -d "$backup_path/system/netplan" ] && sudo cp -r "$backup_path/system/netplan" "/etc/" echo -e "\n${GREEN}=== Restore completed! It is recommended to restart the system for the configurations to take effect ${NC}"}# 3. Kernel rollback guidekernel_rollback_guide() { echo -e "\n${BLUE}=== Kernel rollback operation guide ===${NC}" echo -e "${YELLOW}1. Restart the computer and hold the Shift key during boot (UEFI systems may need to press Esc)${NC}" echo -e "${YELLOW}2. After entering the GRUB menu, select 'Advanced options for Linux Mint'${NC}" echo -e "${YELLOW}3. Choose an older kernel version (the option without recovery mode)${NC}" echo -e "${YELLOW}4. Press Enter to boot, the system will run with the old kernel${NC}" echo -e "\n${YELLOW}If you need to completely uninstall the problematic kernel, you can execute after booting:${NC}" echo -e " dpkg --list | grep linux-image # View installed kernels" echo -e " sudo apt purge linux-image-<version> linux-headers-<version> # Replace with the problematic kernel version"}# Main menu show_menu() { clear echo -e "${BLUE}==================== System Backup and Restore Tool ====================${NC}" echo -e "1. ${GREEN}Perform full backup${NC} (software list, configuration files, documents, system configurations)" echo -e "2. ${GREEN}Restore from backup${NC} (restore software, configuration to backup state)" echo -e "3. ${YELLOW}Kernel rollback guide${NC} (use when unable to boot after kernel update)" echo -e "4. ${RED}Exit${NC}" echo -e "${BLUE}==========================================================${NC}" echo -n "Please select an operation [1-4]:" read -r choice}# Main logicwhile true; do show_menu case $choice in 1) perform_backup read -p "Press Enter to return to menu..." ;; 2) perform_restore read -p "Press Enter to return to menu..." ;; 3) kernel_rollback_guide read -p "Press Enter to return to menu..." ;; 4) echo -e "\n${YELLOW}Exiting tool, goodbye!${NC}" exit 0 ;; *) echo -e "${RED}Invalid choice, please enter 1-4${NC}" read -p "Press Enter to return to menu..." ;; esacdone
Script Function Description
-
Full Backup: Automatically backs up the following content to
<span>~/Documents/system_backup/backup_timestamp</span>directory:
- List of installed software (including manually installed software for precise restoration)
- User configurations (
<span>.config</span>, terminal configurations, application shortcuts) - Personal documents (
<span>Documents</span>,<span>Downloads</span>directories) - Critical system configurations (software sources, network configurations)
- Generate checksum file to ensure backup integrity
Restore from Backup:
- Supports selecting any backup directory for restoration
- Automatically verifies backup integrity (can optionally skip corrupted backups)
- Restores packages, user configurations, system configurations (administrator password required)
Kernel Rollback Guide: Provides detailed step-by-step instructions on how to switch to an old kernel via GRUB and commands to uninstall problematic kernels
Usage Instructions
-
Save the Script: Copy the above code into a text file and name it
<span>sys_backup_restore.sh</span> -
Grant Execute Permission:
chmod +x sys_backup_restore.sh
3.Run the Script:
./sys_backup_restore.sh
4. Operation Selection: Enter the number (1-4) as prompted by the menu to complete backup / restore
Precautions
- During the backup process, you may need to enter the administrator password (for backing up system-level configurations)
- The first run will automatically create the backup directory, no manual creation is required
- The restore operation will overwrite the current system’s configuration and software state, please ensure to select the correct backup directory
- It is recommended to regularly copy the
<span>~/Documents/system_backup</span>directory to a USB drive or cloud storage to prevent loss of backups due to system damage
This script is compatible with Linux Mint 22.2 Xfce and other Debian/Ubuntu-based systems, and can be used directly without modification.