112 lines
4.3 KiB
Bash
112 lines
4.3 KiB
Bash
#!/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}" <<EOF_REMOTE_COMMANDS
|
|
# This entire block of commands runs on the remote server.
|
|
|
|
# Exit immediately if any command fails.
|
|
set -e
|
|
|
|
echo "--- Starting remote execution on $(hostname) ---"
|
|
|
|
# 1. Zip the website directory.
|
|
echo "Step 1: Zipping site files..."
|
|
zip -r ${REMOTE_WEBSITE_BACKUP_FILENAME} addsourcegroup.com
|
|
|
|
# 2. Change into the website directory.
|
|
echo "Step 2: Changing directory to addsourcegroup.com..."
|
|
cd addsourcegroup.com
|
|
|
|
# 3. Export the WordPress database using WP-CLI.
|
|
# The backup file will be saved in the remote user's home directory (~).
|
|
echo "Step 3: Exporting WordPress database..."
|
|
wp db export ~/${REMOTE_DB_BACKUP_FILENAME}
|
|
|
|
echo "--- Remote execution finished successfully! ---"
|
|
EOF_REMOTE_COMMANDS
|
|
|
|
# Check the exit status of the SSH command.
|
|
# Proceed only if the remote commands were successful (exit status 0).
|
|
if [ $? -eq 0 ]; then
|
|
echo "Remote backup successful. Starting local file downloads..."
|
|
|
|
# --- Local Download Commands ---
|
|
# These commands run on your local machine after the SSH session closes.
|
|
|
|
# 4. Download the zipped site files using scp.
|
|
echo "Step 4: Downloading site files (${REMOTE_WEBSITE_BACKUP_FILENAME}) to ~/OneDrive - Add Source Group/Documents/4 - Archive/Backups/ASG Website..."
|
|
scp "${USERNAME}@${HOSTNAME}:${REMOTE_WEBSITE_BACKUP_FILENAME}" ~/OneDrive\ -\ Add\ Source\ Group/Documents/4\ -\ Archive/Backups/ASG\ Website
|
|
|
|
# 5. Download the database backup using scp.
|
|
echo "Step 5: Downloading database backup (${REMOTE_DB_BACKUP_FILENAME}) to ~/OneDrive - Add Source Group/Documents/4 - Archive/Backups/ASG Website..."
|
|
scp "${USERNAME}@${HOSTNAME}:~/${REMOTE_DB_BACKUP_FILENAME}" ~/OneDrive\ -\ Add\ Source\ Group/Documents/4\ -\ Archive/Backups/ASG\ Website
|
|
|
|
# Final check to confirm downloads were successful.
|
|
if [ $? -eq 0 ]; then
|
|
echo "Backup script and downloads completed successfully."
|
|
echo "Attempting to remove backup files from the remote host..."
|
|
|
|
# --- Remote File Cleanup ---
|
|
# Connect again to remove the files. It's safer to do this after successful download.
|
|
ssh "${USERNAME}@${HOSTNAME}" <<EOF_CLEANUP
|
|
set -e
|
|
echo "Step 6: Removing remote backup files..."
|
|
rm -f ~/${REMOTE_WEBSITE_BACKUP_FILENAME}
|
|
rm -f ~/${REMOTE_DB_BACKUP_FILENAME}
|
|
echo "Remote cleanup complete."
|
|
EOF_CLEANUP
|
|
|
|
if [ $? -eq 0 ]; then
|
|
echo "Remote files removed successfully."
|
|
else
|
|
echo "WARNING: Could not remove remote files. Please check manually." >&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 |