Question:
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 news_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_news.zip of /var/www/html/news 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.
Solution:
# ssh to app server 3
ssh banner@stapp03
# Create new ssh keys for app server 3 (useful for passwordless authentication)
ssh-keygen -t rsa -b 4000
# Copy public key of app server 3 to Storage Server
ssh-copy-id natasha@ststor01
Number of key(s) added: 1
# Before script let's do it manually
zip -r /archives/xfusioncorp_news.zip /var/www/html/news
# Check if the zip file created
ls -l /archives/xfusioncorp_news.zip
-rw-r--r-- 1 banner banner 588 Nov 9 08:54 /archives/xfusioncorp_news.zip
# Transfer from app server 3 to storage server
scp /archives/xfusioncorp_news.zip natasha@ststor01:/archives/
xfusioncorp_news.zip 100% 623 2.9MB/s 00:00
# Now check if the archive uploaded to storage server
ssh natasha@ststor01
ls -l /archives
total 4
-rw-r--r-- 1 natasha natasha 588 Nov 9 08:55 xfusioncorp_news.zip
# exit back to Application Server 3
exit
Time to create our script to automate this.
vi /scripts/news_archive.sh
#!/bin/bash
echo "[INFO] Starting Program"
zip -r /archives/xfusioncorp_news.zip /var/www/html/news
scp /archives/xfusioncorp_news.zip natasha@ststor01:/archives/
echo "[INFO] Program Conpleted!"
Run our script:
# Mark as executable
chmod +x /scripts/news_archive.sh
# Execute the script
/scripts/news_archive.sh
[INFO] Starting Program
updating: var/www/html/news/ (stored 0%)
updating: var/www/html/news/index.html (stored 0%)
updating: var/www/html/news/.gitkeep (stored 0%)
xfusioncorp_news.zip 100% 588 144.4KB/s 00:00
[INFO] Program Conpleted!
