Day 10: Linux Bash Scripts - uncomplete task

Hi all,
I don’t understand what I’m missing.

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.

Here is my solution:

thor@jumphost ~$ ssh banner@stapp03
[banner@stapp03 ~]$ sudo -i
[root@stapp03 ~]# yum install zip -y
[root@stapp03 ~]# ssh-keygen -t rsa
[root@stapp03 ~]# ssh-copy-id clint@stbkp01
[root@stapp03 ~]# vi /scripts/official_backup.sh
[root@stapp03 ~]# chmod +x /scripts/official_backup.sh
[root@stapp03 ~]# ls -laht /scripts/official_backup.sh
-rwxr-xr-x 1 root root 226 Aug 25 18:14 /scripts/official_backup.sh
[root@stapp03 ~]# /scripts/official_backup.sh
  adding: var/www/html/official/ (stored 0%)
  adding: var/www/html/official/.gitkeep (stored 0%)
  adding: var/www/html/official/index.html (stored 0%)
xfusioncorp_official.zip                  100%  616     1.9MB/s   00:00
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/

Can you please help see what I’m missing? TY

HI @maggie

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!

How did you manage it, I’m still stuck here.

My repo is still work in progress but all my commands and script is here: GitHub - maggiesuero/100-Days-Of-DevOps-KodeKloud-Challenges

It took me like 4 times to do it right, doing pretty much the same all the times

1 Like

Thanks, this helped :slightly_smiling_face:

1 Like

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.

@Vidusha

I changed my script to the following. I first manually ran the following commands

ssh-keygen -t rsa
ssh-copy-id clint@stbkp01

So it will ask the one time password for the clint server. Then ran the script file. But I’m still stuck on this.

Script


#!/bin/bash

# Variables
NAME="beta"
WEBSITE_DIR="/var/www/html/${NAME}"
ARCHIVE_NAME="xfusioncorp_${NAME}.zip"
LOCAL_BACKUP_DIR="/backup"

echo "Starting backup process..."

# Install zip if needed
sudo dnf install -y zip &> /dev/null

# Create zip archive
echo "Creating zip archive..."
sudo zip -r "$LOCAL_BACKUP_DIR/$ARCHIVE_NAME" "$WEBSITE_DIR"

# Copy to backup server
echo "Copying to backup server..."
scp "$LOCAL_BACKUP_DIR/$ARCHIVE_NAME" clint@stbkp01:/backup/

echo "Backup completed!"

The requirement d is about not entering the passwords while copying the scripts, is satisfied here, right?

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!

2 Likes

Hello @Vidusha
This is correct, BIG thanks.

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.

1 Like

You’re welcome @gehadalaaeldeen! Happy to hear my solution helped. Wishing you the best of luck in your 100 days DevOps journey!

1 Like

@Vidusha
Thanks a lot, it worked, and I also removed all the sudo commands in the script as well.

Following is the worked script for me (the earlier script I used also worked after omitting the sudo and starting Vim without the root user

#!/bin/bash

# Variables
NAME="official"
WEBSITE_DIR="/var/www/html/${NAME}"
ARCHIVE_NAME="xfusioncorp_${NAME}.zip"
LOCAL_BACKUP_DIR="/backup"

ssh-keygen -t rsa
ssh-copy-id clint@stbkp01

echo "Starting backup process..."

# Install zip if needed
sudo dnf install -y zip &> /dev/null

# Create zip archive
echo "Creating zip archive..."
sudo zip -r "$LOCAL_BACKUP_DIR/$ARCHIVE_NAME" "$WEBSITE_DIR"

# Copy to backup server (NO PASSWORD because SSH keys already set up)
echo "Copying to backup server..."
scp "$LOCAL_BACKUP_DIR/$ARCHIVE_NAME" clint@stbkp01:/backup/

echo "Backup completed!"
1 Like

You’re welcome @LahiruT! I’m happy to hear my reply helped. Wishing you the best of luck!