In production environments, automated data backup is a critical component of disaster recovery (DR) plans. Automating these procedures using Bash scripts reduces human error, guarantees schedule consistency, and speeds up recovery times.
A typical backup automation workflow involves:
/var/www/html/).tar or zip)./backup/).scp or rsync).This guide outlines the steps to configure password-less SSH between an application server and the central backup server, write a robust Bash script (ecommerce_backup.sh), and verify automated file delivery.
stapp01, stapp02, stapp03)tony, steve, banner)stbkp01)clinton)/var/www/html/ecommerce//backup//scripts/ecommerce_backup.shxfusioncorp_ecommerce.zipSSH into the assigned application server from the Jump Host:
# Example for App Server 2
ssh steve@stapp02
To allow the backup script to run automatically without prompting for password credentials during the file transfer step:
Enter through all prompt defaults):
ssh-keygen -t rsa
# Example copying to user clinton on backup server stbkp01
ssh-copy-id clinton@stbkp01
ssh clinton@stbkp01
Expected result: Immediate terminal prompt switch to [clinton@stbkp01 ~]$ without a password challenge. Type exit to return to the App Server.
Create the folders required to house the script and store the local backup files. Set appropriate ownership if necessary:
sudo mkdir -p /scripts /backup
sudo chown -R $USER:$USER /scripts /backup
Ensure the zip utility package is installed on the server:
sudo yum install -y zip
Create the backup script file using a text editor:
vi /scripts/ecommerce_backup.sh
Paste the following script content into the file. It zips the target directory and transfers it to the remote server using scp:
#!/bin/bash
# Define variables
SOURCE_DIR="/var/www/html/ecommerce"
BACKUP_DIR="/backup"
ARCHIVE_NAME="xfusioncorp_ecommerce.zip"
BACKUP_SERVER="stbkp01"
BACKUP_USER="clinton"
# 1. Compress the website files into a local zip archive
zip -r "${BACKUP_DIR}/${ARCHIVE_NAME}" "${SOURCE_DIR}"
# 2. Transfer the archive to the remote backup server
scp "${BACKUP_DIR}/${ARCHIVE_NAME}" "${BACKUP_USER}@${BACKUP_SERVER}:${BACKUP_DIR}/"
Save and exit the file (in vi, press Esc, type :wq, and press Enter).
Make the script executable so it can be run directly:
chmod +x /scripts/ecommerce_backup.sh
Manually execute the script to verify that it completes without warnings or prompts:
/scripts/ecommerce_backup.sh
Confirm that the compressed .zip archive exists in the local backup folder:
ls -lh /backup/
Expected output:
-rw-r--r-- 1 steve steve 12M Jun 27 18:20 xfusioncorp_ecommerce.zip
SSH into the backup server and check that the file has been successfully copied into the target backup folder:
ssh clinton@stbkp01 "ls -lh /backup/"
Expected output:
-rw-r--r-- 1 clinton clinton 12M Jun 27 18:20 xfusioncorp_ecommerce.zip
Log out of the Application Server:
exit