DEV Community

Cover image for 19.Linux Bash Scripts
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

19.Linux Bash Scripts

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
Enter fullscreen mode Exit fullscreen mode

2️⃣ Install zip package

yum install -y zip
Enter fullscreen mode Exit fullscreen mode

3️⃣ Create required directories

mkdir -p /scripts
mkdir -p /backup
chown banner:banner /scripts /backup
Enter fullscreen mode Exit fullscreen mode

4️⃣ Switch back to normal user

exit

5️⃣ Create script

vi /scripts/blog_backup.sh
Enter fullscreen mode Exit fullscreen mode

6️⃣ Add script content

#!/bin/bash

zip -r /backup/xfusioncorp_blog.zip /var/www/html/blog
scp /backup/xfusioncorp_blog.zip natasha@ststor01:/backup/
Enter fullscreen mode Exit fullscreen mode

7️⃣ Make script executable

chmod +x /scripts/blog_backup.sh
Enter fullscreen mode Exit fullscreen mode

8️⃣ Setup passwordless SSH (IMPORTANT)

ssh-keygen -t rsa

# (Press Enter for all prompts)

ssh-copy-id natasha@ststor01
# Password: Bl@kW
Enter fullscreen mode Exit fullscreen mode

9️⃣ Test passwordless connection

ssh natasha@ststor01

(Should not ask password)

exit
Enter fullscreen mode Exit fullscreen mode

πŸ”Ÿ Run the script

/scripts/blog_backup.sh
Enter fullscreen mode Exit fullscreen mode

1️⃣1️⃣ Verify backup

On app server:

ls /backup

On storage server:

ssh natasha@ststor01
ls /backup
Enter fullscreen mode Exit fullscreen mode

🧠 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

No sudo inside script

Resources & Next Steps
πŸ“¦ Full Code Repository: KodeKloud Learning Labs
πŸ“– More Deep Dives: Whispering Cloud Insights - Read other technical articles
πŸ’¬ Join Discussion: DEV Community - Share your thoughts and questions
πŸ’Ό Let's Connect: LinkedIn - I'd love to connect with you

Credits
β€’ All labs are from: KodeKloud
β€’ I sincerely appreciate your provision of these valuable resources.

Top comments (0)