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 official_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_official.zip of /var/www/html/official directory.
b. Save the archive in /backup/ on App Server 3. This is a temporary storage, as backups from this location will be clean on weekly basis. Therefore, we also need to save this backup archive on Nautilus Backup Server.
c. Copy the created archive to Nautilus Backup 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.
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. You can install it either manually or through the bash script as needed.
thor@jumphost ~$ ssh clint@stbkp01
[clint@stbkp01 ~]$ ls -l /backup/xfusioncorp_official.zip
-rw-r--r-- 1 clint clint 616 Aug 25 18:15 /backup/xfusioncorp_official.zip
This is the /scripts/official_backup.sh:
#!/bin/bash
# Create a zip archive of the official directory
zip -r /backup/xfusioncorp_official.zip /var/www/html/official
# Copy the archive to the backup server
scp /backup/xfusioncorp_official.zip clint@stbkp01:/backup/
Your script and setup looks good and should have resulted in a pass.
The only thing I see could cause a failure is the -r flag while executing the scp command within the script. (That could be how the grader might want to see the script written.)
Thank you @Santosh_KodeKloud
I tried it again, I would said that I did pretty much the same but this time it worked.
I still used the zip -r /backup/xfusioncorp_official.zip /var/www/html/official since I couldn’t think of an alternative.
TY again. Cheers!
I’m still stuck in this place, can anyone point me out where the issue is? I tried 5-6 times using different approaches but always saying the zip file is not in the backup server.
#!/bin/bash
# Variables
WEBSITE_DIR="/var/www/html/news"
ARCHIVE_NAME="xfusioncorp_news.zip"
LOCAL_BACKUP_DIR="/backup"
REMOTE_USER="clint"
REMOTE_HOST="stbkp01"
REMOTE_BACKUP_DIR="/backup"
echo "Starting backup process..."
# Check if zip package is installed, install if not
rpm -q zip &> /dev/null
if [ $? -eq 0 ]; then
echo "Zip package is already installed"
else
echo "Zip package is not installed. Installing..."
sudo dnf install -y zip
if [ $? -eq 0 ]; then
echo "Zip package installed successfully"
else
echo "Failed to install zip package. Exiting."
exit 1
fi
fi
# Create zip archive of the website directory
echo "Creating zip archive of $WEBSITE_DIR..."
cd /var/www/html/news
sudo zip -r "$ARCHIVE_NAME" .
if [ $? -eq 0 ]; then
echo "Archive $ARCHIVE_NAME created successfully"
else
echo "Failed to create archive. Exiting."
exit 1
fi
# Save archive to local backup directory
echo "Copying archive to local backup directory..."
cp "/var/www/html/news/$ARCHIVE_NAME" "$LOCAL_BACKUP_DIR/"
if [ $? -eq 0 ]; then
echo "Archive saved to $LOCAL_BACKUP_DIR successfully"
else
echo "Failed to save archive locally. Exiting."
exit 1
fi
# Copy archive to Nautilus Backup Server
echo "Copying archive to Nautilus Backup Server..."
scp "/backup/$ARCHIVE_NAME" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_BACKUP_DIR/"
if [ $? -eq 0 ]; then
echo "Archive copied to Nautilus Backup Server successfully"
else
echo "Failed to copy archive to remote server. Exiting."
exit 1
fi
# Clean up - remove archive from /var/www/html/
#rm "/var/www/html/$ARCHIVE_NAME"
echo "Backup process completed successfully!"
echo "Archive saved locally: $LOCAL_BACKUP_DIR/$ARCHIVE_NAME"
echo "Archive saved remotely: $REMOTE_HOST:$REMOTE_BACKUP_DIR/$ARCHIVE_NAME"```
Hi Lahiru! Did you manually entered the backup server’s password after you executed the bash script? If so, I suggest you to try using public and private key pairs to log into backup server. Also do not switch to root user when doing this task.
Yes. You have already satisfied the requirement d by manually running those two commands. You can check it by running ssh clint@stbkp01 command manually. You will see that you can log into the backup server without providing the password. Do you face any further issues in this task?
@Vidusha
Yes, even though I follow those steps, the task cannot be completed. All the files are in the right places. Same error appearing ..zip not found/backup
I have also encountered this issue. In my case the reason is that I did the task by switching to the root user. I think it may be the reason in your case also. Section d of the task states that the respective server user (for example, tony in case of App Server 1) must be able to run it. So, you have to do this task without switching to the root user or by using sudo. I also assume you have created the script using sudo. When you create a file using sudo touch filename, the owner of the file will be the root user. You can see it by running ls -l command. So, try doing the task without sudo. Use sudo only if it says permission denied when changing the file permissions. Wishing you the best of luck!
I used to be stuck in this challenge for two days, and all I was doing that using root instead of the user account.
After using sudo and server account, everything worked fine.