DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Linux Bash Scripts

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 archiving website content files. They have a static website running on App Server 3 in Stratos Datacenter, and they need to create a bash script named blog_archive.sh which should accomplish the following tasks. (Also remember to place the script under the /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 the /archives/ directory on the App Server 3. This is a temporary storage, as archives from this location will be cleaned on a weekly basis. Therefore, the archive should also be copied to the Nautilus Storage Server so it can be retrieved later for validation purposes.

c. Copy the created archive to the Nautilus Storage Server server in the /archives/ 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.


Environment Details

Component Value
App Server stapp03
App Server User banner
App Server Password BigGr33n
Storage Server ststor01
Storage Server User natasha
Storage Server Password Bl@kW
Website Directory /var/www/html/blog
Script Location /scripts/blog_archive.sh
Archive Location /archives/xfusioncorp_blog.zip

Step 1: Connect to App Server 3

Access the server using SSH.

ssh banner@stapp03
Password: BigGr33n
Enter fullscreen mode Exit fullscreen mode

Switch to root to perform administrative tasks.

sudo su -
Password: BigGr33n
Enter fullscreen mode Exit fullscreen mode

Step 2: Install the zip Package

The zip package is required to create the archive. Install it manually outside the script as specified in the task requirements.

yum install -y zip
Enter fullscreen mode Exit fullscreen mode

Verify the installation.

rpm -qa | grep zip
Enter fullscreen mode Exit fullscreen mode

If the package is already installed, the output will show the version.


Step 3: Create Required Directories

Create the /scripts directory for the script and the /archives directory for the archive.

mkdir -p /scripts
mkdir -p /archives
Enter fullscreen mode Exit fullscreen mode

Set ownership so the banner user can write to these directories.

chown -R banner:banner /scripts
chown -R banner:banner /archives
Enter fullscreen mode Exit fullscreen mode

Step 4: Generate SSH Keys for Passwordless Copy

The script must copy the archive to the storage server without prompting for a password. This requires SSH key-based authentication.

Switch to the banner user.

su - banner
Enter fullscreen mode Exit fullscreen mode

Generate an SSH key pair.

ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

Copy the public key to the storage server.

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

Test the passwordless login.

ssh natasha@ststor01 "hostname"
Enter fullscreen mode Exit fullscreen mode

If the command returns the storage server hostname without prompting for a password, the key authentication is working.


Step 5: Create the Bash Script

Create the script file at /scripts/blog_archive.sh.

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

Add the following content.

#!/bin/bash

# Create a zip archive of the blog directory
zip -r /archives/xfusioncorp_blog.zip /var/www/html/blog

# Copy the archive to the Nautilus Storage Server
scp /archives/xfusioncorp_blog.zip natasha@ststor01:/archives/
Enter fullscreen mode Exit fullscreen mode

Save the file and exit the editor.


Step 6: Make the Script Executable

Set execute permissions on the script.

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

Verify the permissions.

ls -la /scripts/blog_archive.sh
Enter fullscreen mode Exit fullscreen mode

Step 7: Run the Script as the banner User

The task requires that the banner user can run the script. Execute it as banner.

su - banner -c "/scripts/blog_archive.sh"
Enter fullscreen mode Exit fullscreen mode

The output shows the files being added to the archive and the copy progress.

  adding: var/www/html/blog/ (stored 0%)
  adding: var/www/html/blog/index.html (stored 0%)
  adding: var/www/html/blog/.gitkeep (stored 0%)
xfusioncorp_blog.zip                                   100%  588     1.0MB/s   00:00
Enter fullscreen mode Exit fullscreen mode

Step 8: Verify the Archive

Check that the archive was created locally.

ls -la /archives/xfusioncorp_blog.zip
Enter fullscreen mode Exit fullscreen mode

Expected output:

-rw-r--r-- 1 banner banner 588 Sep 23 10:38 /archives/xfusioncorp_blog.zip
Enter fullscreen mode Exit fullscreen mode

Check that the archive was copied to the storage server.

ssh natasha@ststor01 "ls -la /archives/xfusioncorp_blog.zip"
Enter fullscreen mode Exit fullscreen mode

Expected output:

-rw-r--r-- 1 natasha natasha 588 Sep 23 10:38 /archives/xfusioncorp_blog.zip
Enter fullscreen mode Exit fullscreen mode

Verify the contents of the archive.

unzip -l /archives/xfusioncorp_blog.zip
Enter fullscreen mode Exit fullscreen mode

Complete Workflow Summary

# 1. Connect to App Server 3
ssh banner@stapp03
sudo su -

# 2. Install zip
yum install -y zip

# 3. Create directories
mkdir -p /scripts /archives
chown -R banner:banner /scripts /archives

# 4. Generate SSH keys as banner
su - banner
ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa
ssh-copy-id natasha@ststor01

# 5. Create the script
vi /scripts/blog_archive.sh
# Add: zip -r /archives/xfusioncorp_blog.zip /var/www/html/blog
# Add: scp /archives/xfusioncorp_blog.zip natasha@ststor01:/archives/

# 6. Make executable
chmod +x /scripts/blog_archive.sh

# 7. Run the script as banner
su - banner -c "/scripts/blog_archive.sh"

# 8. Verify
ls -la /archives/xfusioncorp_blog.zip
ssh natasha@ststor01 "ls -la /archives/xfusioncorp_blog.zip"
Enter fullscreen mode Exit fullscreen mode

Script Content

#!/bin/bash

# Create a zip archive of the blog directory
zip -r /archives/xfusioncorp_blog.zip /var/www/html/blog

# Copy the archive to the Nautilus Storage Server
scp /archives/xfusioncorp_blog.zip natasha@ststor01:/archives/
Enter fullscreen mode Exit fullscreen mode

Key Requirements Explained

Requirement How It Is Met
Script named blog_archive.sh under /scripts Created at /scripts/blog_archive.sh
Zip archive of /var/www/html/blog zip -r /archives/xfusioncorp_blog.zip /var/www/html/blog
Save archive to /archives/ on App Server 3 Archive saved locally
Copy to Nautilus Storage Server scp /archives/xfusioncorp_blog.zip natasha@ststor01:/archives/
No password prompt during copy SSH key-based authentication configured
Run as the banner user Script owned by banner, keys generated for banner
No sudo inside the script Script uses only zip and scp

Troubleshooting

Issue Cause Fix
zip: command not found zip package not installed yum install -y zip
Permission denied when running script Script not executable chmod +x /scripts/blog_archive.sh
Password prompt during scp SSH keys not configured for banner Regenerate keys as banner user
Host key verification failed Host key not accepted Run ssh natasha@ststor01 first to accept the key
Archive not copied SSH connectivity issue Test with ssh natasha@ststor01 "echo test"
chown: Operation not permitted Not running as root Run chown commands as root

Best Practices

  1. Always install required packages outside the script, not inside.
  2. Generate SSH keys as the user who will run the script.
  3. Test passwordless SSH before relying on it in a script.
  4. Set appropriate ownership on script and archive directories.
  5. Keep the script simple and focused on a single task.
  6. Avoid using sudo inside scripts when not necessary.
  7. Verify both the local and remote copies of the archive.

Conclusion

This guide covered the creation of a bash script that archives website content and copies it to a remote server. The script uses zip to create the archive and scp to transfer it, with SSH key-based authentication enabling passwordless operation.

The key steps were installing the zip package, creating the required directories, generating SSH keys for the banner user, writing the script, and verifying that the archive was created and copied successfully.


Quick Reference

Task Command
Install zip yum install -y zip
Create directories mkdir -p /scripts /archives
Generate SSH key ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa
Copy SSH key ssh-copy-id natasha@ststor01
Create archive zip -r /archives/xfusioncorp_blog.zip /var/www/html/blog
Copy to remote scp /archives/xfusioncorp_blog.zip natasha@ststor01:/archives/
Run script su - banner -c "/scripts/blog_archive.sh"
Verify local ls -la /archives/xfusioncorp_blog.zip
Verify remote ssh natasha@ststor01 "ls -la /archives/xfusioncorp_blog.zip"

Top comments (0)