#!/bin/bash # ============================================================================== # SSH Backup and Download Script # # Description: # This script connects to a remote server to perform a backup, then downloads # the backup files to the local machine. It uses SSH keys for secure, # password-less authentication. # 1. Connects via SSH to the remote server. # 2. Zips a specified directory on the remote server. # 3. Exports a WordPress database on the remote server. # 4. Downloads the zipped directory to the local ~/Downloads folder. # 5. Downloads the SQL database backup to the local ~/Downloads folder. # 6. Removes the created backup files from the remote server. # # Usage: # ./connect.sh # # ============================================================================== # --- Configuration --- # The username for the SSH connection. USERNAME="mattspeer" # The hostname or IP address of the server to connect to. HOSTNAME="addsourcegroup.com" # --- Main Execution --- echo "Connecting to ${HOSTNAME} to run backup commands..." # Define filenames that will be created on the remote server. # This needs to be done locally to ensure the filenames match for removal. DATE_STAMP=$(date +"%Y-%m-%d") REMOTE_WEBSITE_BACKUP_FILENAME="addsourcegroup_com_${DATE_STAMP}.zip" REMOTE_DB_BACKUP_FILENAME="db_backup_${DATE_STAMP}.sql" # Use a Here Document (Heredoc) to pass a block of commands to the remote server. # Using 'EOF' in quotes prevents the local shell from expanding variables, # ensuring that commands like `date` are executed on the remote server. # No password or password variable is needed due to SSH key authentication. ssh "${USERNAME}@${HOSTNAME}" <&2 fi else echo "An error occurred during the file download. Remote files were NOT removed." >&2 exit 1 fi else echo "An error occurred during the remote execution. Halting script." >&2 exit 1 fi