From a5f73b3cf784faae997a748ef4b8f17ef2dc7835 Mon Sep 17 00:00:00 2001 From: Ali Date: Sat, 28 Jun 2025 12:51:55 +0100 Subject: [PATCH] backup function fix --- setup_harden_debian_ubuntu.sh | 318 +++++++++++++++++----------------- 1 file changed, 161 insertions(+), 157 deletions(-) diff --git a/setup_harden_debian_ubuntu.sh b/setup_harden_debian_ubuntu.sh index d612620..5fd55a1 100644 --- a/setup_harden_debian_ubuntu.sh +++ b/setup_harden_debian_ubuntu.sh @@ -778,6 +778,160 @@ configure_firewall() { log "Firewall configuration completed." } +configure_fail2ban() { + print_section "Fail2Ban Configuration" + + # Set the SSH port for Fail2Ban to monitor. + local SSH_PORTS_TO_MONITOR="$SSH_PORT" + local NEW_FAIL2BAN_CONFIG + + NEW_FAIL2BAN_CONFIG=$(mktemp) + tee "$NEW_FAIL2BAN_CONFIG" > /dev/null </dev/null 2>&1; then + print_info "Docker already installed." + return 0 + fi + print_info "Removing old container runtimes..." + apt-get remove -y -qq docker docker-engine docker.io containerd runc 2>/dev/null || true + print_info "Adding Docker's official GPG key and repository..." + install -m 0755 -d /etc/apt/keyrings + curl -fsSL https://download.docker.com/linux/${ID}/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg + chmod a+r /etc/apt/keyrings/docker.gpg + echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/${ID} $(. /etc/os-release && echo "$VERSION_CODENAME") stable" > /etc/apt/sources.list.d/docker.list + print_info "Installing Docker packages..." + if ! apt-get update -qq || ! apt-get install -y -qq docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin; then + print_error "Failed to install Docker packages." + exit 1 + fi + print_info "Adding '$USERNAME' to docker group..." + getent group docker >/dev/null || groupadd docker + if ! groups "$USERNAME" | grep -qw docker; then + usermod -aG docker "$USERNAME" + print_success "User '$USERNAME' added to docker group." + else + print_info "User '$USERNAME' is already in docker group." + fi + print_info "Configuring Docker daemon..." + local NEW_DOCKER_CONFIG + NEW_DOCKER_CONFIG=$(mktemp) + tee "$NEW_DOCKER_CONFIG" > /dev/null <&1 | tee -a "$LOG_FILE" | grep -q "Hello from Docker"; then + print_success "Docker sanity check passed." + else + print_error "Docker hello-world test failed. Please verify installation." + exit 1 + fi + print_warning "NOTE: '$USERNAME' must log out and back in to use Docker without sudo." + log "Docker installation completed." +} + +install_tailscale() { + if ! confirm "Install Tailscale VPN (Optional)?"; then + print_info "Skipping Tailscale installation." + return 0 + fi + print_section "Tailscale VPN Installation" + if command -v tailscale >/dev/null 2>&1; then + print_info "Tailscale already installed." + return 0 + fi + print_info "Installing Tailscale..." + curl -fsSL https://tailscale.com/install.sh -o /tmp/tailscale_install.sh + chmod +x /tmp/tailscale_install.sh + # Simple sanity check on the downloaded script + if ! grep -q "tailscale" /tmp/tailscale_install.sh; then + print_error "Downloaded Tailscale install script appears invalid." + rm -f /tmp/tailscale_install.sh + exit 1 + fi + if ! /tmp/tailscale_install.sh; then + print_error "Failed to install Tailscale." + rm -f /tmp/tailscale_install.sh + exit 1 + fi + rm -f /tmp/tailscale_install.sh + print_warning "ACTION REQUIRED: Run 'sudo tailscale up' after script finishes." + print_success "Tailscale installation complete." + log "Tailscale installation completed." +} + setup_backup() { print_section "Backup Configuration (rsync over SSH)" @@ -787,8 +941,12 @@ setup_backup() { fi # Validate USERNAME - if [[ -z "${USERNAME:-}" || ! id "$USERNAME" >/dev/null 2>&1 ]]; then - print_error "Invalid or unset USERNAME. Please run user setup first." + if [[ -z "$USERNAME" ]]; then + print_error "USERNAME is not set. Please run user setup first." + exit 1 + fi + if ! id "$USERNAME" >/dev/null 2>&1; then + print_error "Invalid USERNAME '$USERNAME'. User does not exist. Please run user setup first." exit 1 fi @@ -824,7 +982,7 @@ setup_backup() { } fi if [[ -f "$EXCLUDE_FILE" ]]; then - print_info "Found existing exclude file مهم at $EXCLUDE_FILE. It will be replaced." + print_info "Found existing exclude file at $EXCLUDE_FILE. It will be replaced." rm -f "$EXCLUDE_FILE" || { print_error "Failed to remove stale exclude file." exit 1 @@ -1168,160 +1326,6 @@ EOF log "Backup configuration completed." } -configure_fail2ban() { - print_section "Fail2Ban Configuration" - - # Set the SSH port for Fail2Ban to monitor. - local SSH_PORTS_TO_MONITOR="$SSH_PORT" - local NEW_FAIL2BAN_CONFIG - - NEW_FAIL2BAN_CONFIG=$(mktemp) - tee "$NEW_FAIL2BAN_CONFIG" > /dev/null </dev/null 2>&1; then - print_info "Docker already installed." - return 0 - fi - print_info "Removing old container runtimes..." - apt-get remove -y -qq docker docker-engine docker.io containerd runc 2>/dev/null || true - print_info "Adding Docker's official GPG key and repository..." - install -m 0755 -d /etc/apt/keyrings - curl -fsSL https://download.docker.com/linux/${ID}/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg - chmod a+r /etc/apt/keyrings/docker.gpg - echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/${ID} $(. /etc/os-release && echo "$VERSION_CODENAME") stable" > /etc/apt/sources.list.d/docker.list - print_info "Installing Docker packages..." - if ! apt-get update -qq || ! apt-get install -y -qq docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin; then - print_error "Failed to install Docker packages." - exit 1 - fi - print_info "Adding '$USERNAME' to docker group..." - getent group docker >/dev/null || groupadd docker - if ! groups "$USERNAME" | grep -qw docker; then - usermod -aG docker "$USERNAME" - print_success "User '$USERNAME' added to docker group." - else - print_info "User '$USERNAME' is already in docker group." - fi - print_info "Configuring Docker daemon..." - local NEW_DOCKER_CONFIG - NEW_DOCKER_CONFIG=$(mktemp) - tee "$NEW_DOCKER_CONFIG" > /dev/null <&1 | tee -a "$LOG_FILE" | grep -q "Hello from Docker"; then - print_success "Docker sanity check passed." - else - print_error "Docker hello-world test failed. Please verify installation." - exit 1 - fi - print_warning "NOTE: '$USERNAME' must log out and back in to use Docker without sudo." - log "Docker installation completed." -} - -install_tailscale() { - if ! confirm "Install Tailscale VPN (Optional)?"; then - print_info "Skipping Tailscale installation." - return 0 - fi - print_section "Tailscale VPN Installation" - if command -v tailscale >/dev/null 2>&1; then - print_info "Tailscale already installed." - return 0 - fi - print_info "Installing Tailscale..." - curl -fsSL https://tailscale.com/install.sh -o /tmp/tailscale_install.sh - chmod +x /tmp/tailscale_install.sh - # Simple sanity check on the downloaded script - if ! grep -q "tailscale" /tmp/tailscale_install.sh; then - print_error "Downloaded Tailscale install script appears invalid." - rm -f /tmp/tailscale_install.sh - exit 1 - fi - if ! /tmp/tailscale_install.sh; then - print_error "Failed to install Tailscale." - rm -f /tmp/tailscale_install.sh - exit 1 - fi - rm -f /tmp/tailscale_install.sh - print_warning "ACTION REQUIRED: Run 'sudo tailscale up' after script finishes." - print_success "Tailscale installation complete." - log "Tailscale installation completed." -} - configure_swap() { if [[ $IS_CONTAINER == true ]]; then print_info "Swap configuration skipped in container."