|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# SystemGuard Installer Script |
| 4 | +# ---------------------------- |
| 5 | +# This script installs, uninstalls, backs up, and restores SystemGuard by managing its installation, cleanup, and configuration. |
| 6 | + |
| 7 | +# Variables |
| 8 | +DOWNLOAD_DIR="/tmp" |
| 9 | +EXTRACT_DIR="/home/$USER/.systemguard" |
| 10 | +LOG_DIR="$HOME/logs" |
| 11 | +LOG_FILE="$LOG_DIR/systemguard-installer.log" |
| 12 | +BACKUP_DIR="/home/$USER/.systemguard_backup" |
| 13 | +EXECUTABLE="/usr/local/bin/systemguard-installer" |
| 14 | + |
| 15 | +# Create necessary directories |
| 16 | +mkdir -p "$LOG_DIR" |
| 17 | +mkdir -p "$BACKUP_DIR" |
| 18 | + |
| 19 | +# Logging function with timestamp |
| 20 | +log() { |
| 21 | + local message="$1" |
| 22 | + echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" | tee -a "$LOG_FILE" |
| 23 | +} |
| 24 | + |
| 25 | +# Backup function for existing configurations |
| 26 | +backup_configs() { |
| 27 | + log "Backing up existing configurations..." |
| 28 | + if [ -d "$EXTRACT_DIR" ]; then |
| 29 | + mkdir -p "$BACKUP_DIR" |
| 30 | + cp -r "$EXTRACT_DIR" "$BACKUP_DIR/$(date '+%Y%m%d_%H%M%S')" |
| 31 | + log "Backup completed: $BACKUP_DIR" |
| 32 | + else |
| 33 | + log "No existing installation found to back up." |
| 34 | + fi |
| 35 | +} |
| 36 | + |
| 37 | +# Restore function to restore from a backup |
| 38 | +restore() { |
| 39 | + log "Starting restore process..." |
| 40 | + |
| 41 | + # List available backups |
| 42 | + if [ -d "$BACKUP_DIR" ]; then |
| 43 | + echo "Available backups:" |
| 44 | + select BACKUP in "$BACKUP_DIR"/*; do |
| 45 | + if [ -n "$BACKUP" ]; then |
| 46 | + echo "You selected: $BACKUP" |
| 47 | + break |
| 48 | + else |
| 49 | + echo "Invalid selection. Please try again." |
| 50 | + fi |
| 51 | + done |
| 52 | + |
| 53 | + # Confirm restoration |
| 54 | + echo "Are you sure you want to restore this backup? This will overwrite the current installation. (y/n)" |
| 55 | + read CONFIRM |
| 56 | + if [ "$CONFIRM" != "y" ]; then |
| 57 | + log "Restore aborted by user." |
| 58 | + exit 0 |
| 59 | + fi |
| 60 | + |
| 61 | + # Remove existing installation |
| 62 | + if [ -d "$EXTRACT_DIR" ]; then |
| 63 | + rm -rf "$EXTRACT_DIR" |
| 64 | + log "Old installation removed." |
| 65 | + fi |
| 66 | + |
| 67 | + # Restore selected backup |
| 68 | + cp -r "$BACKUP" "$EXTRACT_DIR" |
| 69 | + log "Restore completed from backup: $BACKUP" |
| 70 | + else |
| 71 | + log "No backups found to restore." |
| 72 | + echo "No backups found in $BACKUP_DIR." |
| 73 | + fi |
| 74 | +} |
| 75 | + |
| 76 | +# Install function |
| 77 | +install() { |
| 78 | + log "Starting installation of SystemGuard..." |
| 79 | + echo "Enter the version of SystemGuard to install (e.g., v1.0.0 or 'latest' for the latest version):" |
| 80 | + read VERSION |
| 81 | + echo "Warning: This script will remove any existing installation of SystemGuard. Continue? (y/n)" |
| 82 | + read CONFIRM |
| 83 | + |
| 84 | + if [ "$CONFIRM" != "y" ]; then |
| 85 | + log "Installation aborted by user." |
| 86 | + exit 0 |
| 87 | + fi |
| 88 | + |
| 89 | + # Fetch latest version if specified |
| 90 | + if [ "$VERSION" == "latest" ]; then |
| 91 | + log "Fetching the latest version of SystemGuard from GitHub..." |
| 92 | + VERSION=$(curl -s https://api.github.com/repos/codeperfectplus/SystemGuard/releases/latest | grep -Po '"tag_name": "\K.*?(?=")') |
| 93 | + if [ -z "$VERSION" ]; then |
| 94 | + log "Error: Unable to fetch the latest version. Please try again or specify a version manually." |
| 95 | + exit 1 |
| 96 | + fi |
| 97 | + log "Latest version found: $VERSION" |
| 98 | + fi |
| 99 | + |
| 100 | + # Define URL after determining the version |
| 101 | + ZIP_URL="https://github.com/codeperfectplus/SystemGuard/archive/refs/tags/$VERSION.zip" |
| 102 | + log "Installing SystemGuard version $VERSION..." |
| 103 | + |
| 104 | + # Download the SystemGuard zip file |
| 105 | + wget -q $ZIP_URL -O $DOWNLOAD_DIR/systemguard.zip |
| 106 | + if [ $? -ne 0 ]; then |
| 107 | + log "Error: Failed to download SystemGuard version $VERSION. Please check the version number and try again." |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | + log "Download completed." |
| 111 | + |
| 112 | + # Backup existing configurations |
| 113 | + backup_configs |
| 114 | + |
| 115 | + # Remove any existing installation of SystemGuard |
| 116 | + log "Removing previous installation of SystemGuard, if any..." |
| 117 | + if [ -d "$EXTRACT_DIR" ]; then |
| 118 | + rm -rf "$EXTRACT_DIR" |
| 119 | + log "Old installation removed." |
| 120 | + fi |
| 121 | + |
| 122 | + # Clean up previous cron jobs related to SystemGuard |
| 123 | + log "Cleaning up previous cron jobs related to SystemGuard..." |
| 124 | + CRON_PATTERN=".systemguard/SystemGuard-.*/dashboard.sh" |
| 125 | + if crontab -l | grep -q "$CRON_PATTERN"; then |
| 126 | + crontab -l | grep -v "$CRON_PATTERN" | crontab - |
| 127 | + log "Old cron jobs removed." |
| 128 | + else |
| 129 | + log "No previous cron jobs found." |
| 130 | + fi |
| 131 | + |
| 132 | + # Create the extraction directory |
| 133 | + log "Setting up installation directory..." |
| 134 | + mkdir -p $EXTRACT_DIR |
| 135 | + |
| 136 | + # Extract the downloaded zip file |
| 137 | + log "Extracting SystemGuard package..." |
| 138 | + unzip -q $DOWNLOAD_DIR/systemguard.zip -d $EXTRACT_DIR |
| 139 | + rm $DOWNLOAD_DIR/systemguard.zip |
| 140 | + log "Extraction completed." |
| 141 | + |
| 142 | + # Navigate to the setup directory and execute setup script |
| 143 | + log "Navigating to the SystemGuard setup directory..." |
| 144 | + cd $EXTRACT_DIR/SystemGuard-*/src/scripts |
| 145 | + if [ ! -f "cronjob.sh" ]; then |
| 146 | + log "Error: cronjob.sh not found in the extracted directory. Please verify the contents." |
| 147 | + exit 1 |
| 148 | + fi |
| 149 | + |
| 150 | + # Make cronjob.sh executable and run it |
| 151 | + log "Preparing cronjob script..." |
| 152 | + chmod +x cronjob.sh |
| 153 | + log "Executing the cronjob setup..." |
| 154 | + ./cronjob.sh |
| 155 | + |
| 156 | + # Install the executable |
| 157 | + log "Installing executable to /usr/local/bin/systemguard-installer..." |
| 158 | + cp "$(basename "$0")" "$EXECUTABLE" |
| 159 | + log "SystemGuard version $VERSION installed successfully!" |
| 160 | +} |
| 161 | + |
| 162 | +# Uninstall function |
| 163 | +uninstall() { |
| 164 | + log "Uninstalling SystemGuard..." |
| 165 | + |
| 166 | + # Remove cron jobs related to SystemGuard |
| 167 | + log "Cleaning up cron jobs related to SystemGuard..." |
| 168 | + CRON_PATTERN=".systemguard/SystemGuard-.*/dashboard.sh" |
| 169 | + if crontab -l | grep -q "$CRON_PATTERN"; then |
| 170 | + crontab -l | grep -v "$CRON_PATTERN" | crontab - |
| 171 | + log "Cron jobs removed." |
| 172 | + else |
| 173 | + log "No cron jobs found." |
| 174 | + fi |
| 175 | + |
| 176 | + # Remove the SystemGuard installation directory |
| 177 | + if [ -d "$EXTRACT_DIR" ]; then |
| 178 | + rm -rf "$EXTRACT_DIR" |
| 179 | + log "SystemGuard has been removed from your system." |
| 180 | + else |
| 181 | + log "SystemGuard is not installed on this system." |
| 182 | + fi |
| 183 | + |
| 184 | + # Remove the executable |
| 185 | + if [ -f "$EXECUTABLE" ]; then |
| 186 | + rm "$EXECUTABLE" |
| 187 | + log "Executable $EXECUTABLE removed." |
| 188 | + else |
| 189 | + log "No executable found to remove." |
| 190 | + fi |
| 191 | +} |
| 192 | + |
| 193 | +# Display help |
| 194 | +show_help() { |
| 195 | + echo "SystemGuard Installer" |
| 196 | + echo "Usage: ./installer.sh [options]" |
| 197 | + echo "Options:" |
| 198 | + echo " --install Install SystemGuard" |
| 199 | + echo " --uninstall Uninstall SystemGuard" |
| 200 | + echo " --restore Restore SystemGuard from a backup" |
| 201 | + echo " --help Display this help message" |
| 202 | +} |
| 203 | + |
| 204 | +# Parse command-line options |
| 205 | +for arg in "$@"; do |
| 206 | + case $arg in |
| 207 | + --install) ACTION="install" ;; |
| 208 | + --uninstall) ACTION="uninstall" ;; |
| 209 | + --restore) ACTION="restore" ;; |
| 210 | + --help) show_help; exit 0 ;; |
| 211 | + *) echo "Unknown option: $arg"; show_help; exit 1 ;; |
| 212 | + esac |
| 213 | +done |
| 214 | + |
| 215 | +# Execute based on the action specified |
| 216 | +case $ACTION in |
| 217 | + install) install ;; |
| 218 | + uninstall) uninstall ;; |
| 219 | + restore) restore ;; |
| 220 | + *) echo "No action specified. Use --help for usage information." ;; |
| 221 | +esac |
0 commit comments