Lab Information
The production support team of xFusionCorp Industries is working on developing some bash scripts to automate different day to day tasks. One is to create a bash script for taking websites backup. They have a static website running on App Server 3 in Stratos Datacenter, and they need to create a bash script named blog_backup.sh which should accomplish the following tasks. (Also remember to place the script under /scripts directory on App Server 3).
a. Create a zip archive named xfusioncorp_blog.zip of /var/www/html/blog directory.
b. Save the archive in /backup/ on App Server 3. This is a temporary storage, as backups from this location will be cleaned on a weekly basis. Therefore, we also need to save this backup archive on Nautilus Storage Server.
c. Copy the created archive to Nautilus Storage Server server in /backup/ location.
d. Please make sure script won't ask for password while copying the archive file. Additionally, the respective server user (for example, tony in case of App Server 1) must be able to run it.
e. Do not use sudo inside the script.
Note:
The zip package must be installed on given App Server before executing the script. This package is essential for creating the zip archive of the website files. Install it manually outside the script.
Lab Solutions
π§ Part 1: Lab Step-by-Step Guidelines
1οΈβ£ Login to App Server 3
ssh banner@stapp03
# Password: BigGr33n
sudo -i
2οΈβ£ Install zip package
yum install -y zip
3οΈβ£ Create required directories
mkdir -p /scripts
mkdir -p /backup
chown banner:banner /scripts /backup
4οΈβ£ Switch back to normal user
exit
5οΈβ£ Create script
vi /scripts/blog_backup.sh
6οΈβ£ Add script content
#!/bin/bash
zip -r /backup/xfusioncorp_blog.zip /var/www/html/blog
scp /backup/xfusioncorp_blog.zip natasha@ststor01:/backup/
7οΈβ£ Make script executable
chmod +x /scripts/blog_backup.sh
8οΈβ£ Setup passwordless SSH (IMPORTANT)
ssh-keygen -t rsa
# (Press Enter for all prompts)
ssh-copy-id natasha@ststor01
# Password: Bl@kW
9οΈβ£ Test passwordless connection
ssh natasha@ststor01
(Should not ask password)
exit
π Run the script
/scripts/blog_backup.sh
1οΈβ£1οΈβ£ Verify backup
On app server:
ls /backup
On storage server:
ssh natasha@ststor01
ls /backup
π§ Part 2: Simple Step-by-Step Explanation (Beginner Friendly)
What you are doing
You are automating a backup process:
Website folder β zip β save locally β copy to another server
Step explanation
zip command
zip -r
β compresses the blog folder into one file
backup location
/backup/
β temporary storage on same server
scp command
scp file user@server:/path
β copies file to another server
passwordless SSH
ssh-keygen + ssh-copy-id
β allows script to run automatically without password
no sudo rule
β script runs as normal user (banner), so permissions must already be correct
Final flow
/var/www/html/blog
β
zip file created
β
saved in /backup
β
copied to storage server
Key concept to remember
Automation = command + no manual input
Thatβs why:
No password prompts
Top comments (0)