I couldn't finish the topic Linux Bash Scripts

I couldn’t complete this task ( I tried several times) due to the following issue mentioned in the attached image. What is the issue here? Thank you very much

Here is my script

# 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"

Could you please share the task link? At first glance, I don’t see any issues with the script.

Will this be helpful? [https://engineer.kodekloud.com/task?id=680670695500bdf7ab9a7b28](Day 10: Linux Bash Scripts)

Hi @LahiruT

I don’t see any problem with your script, but it seems you missed this requirement:
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.

Please try again and make sure this requirement is met.

Hi @raymond.baoly
Before running the script, I ran the following two commands(Name of the script and the user will vary depending on the requirement)

sudo chmod +x /scripts/beta_backup.sh
sudo chown banner:banner /scripts/beta_backup.sh

Hi @LahiruT

You need to run these commands to meet the d. requirement:

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

1 Like

Hi @raymond.baoly, thanks a lot for the reply. I updated my script.
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, I 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?

Hi @LahiruT

Please add the ssh-copy-id step to your script as well. The task requires that the script should not ask for a password, which means the script must handle it automatically instead of the user doing it manually.

Hi @raymond.baoly, thanks for the support. I updated the script and removed the sudo commands inside the script, and also when creating the script file, I got rid of creating that as root (sudo vi script.sh)

The final script was

#!/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!"

Hi @LahiruT,

So, you’re able to take care of this task, right?