Day 10: Linux basch Scripts

Can someone tell me why is wrong with my script. I got error message:

  • archive does not contain correct data on Backup server.
    TASK:
    a. Create a zip archive named xfusioncorp_official.zip of /var/www/html/official directory.

b. Save the archive in /backup/ on App Server 2. 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.

SCRIPT:
#!/bin/bash

Variables

SRC_DIR=“/var/www/html/official”
ARCHIVE_NAME=“xfusioncorp_official.zip”
LOCAL_BACKUP_DIR=“/backup”
REMOTE_BACKUP_DIR=“/backup”
REMOTE_USER=“backup_user”
REMOTE_HOST=“backup.nautilus”

Ensure source directory exists

if [ ! -d “$SRC_DIR” ]; then
echo “Source directory $SRC_DIR does not exist.”
exit 1
fi

Ensure local backup directory exists

mkdir -p “$LOCAL_BACKUP_DIR”

Create the zip archive

cd “$SRC_DIR”
zip -r “$LOCAL_BACKUP_DIR/$ARCHIVE_NAME” . > /dev/null

Check if zip was created successfully

if [ $? -ne 0 ]; then
echo “Failed to create archive.”
exit 2
fi

echo “Archive created at $LOCAL_BACKUP_DIR/$ARCHIVE_NAME”

Copy to Nautilus Backup Server

scp “$LOCAL_BACKUP_DIR/$ARCHIVE_NAME” “$REMOTE_USER@$REMOTE_HOST:$REMOTE_BACKUP_DIR”

Check if SCP succeeded

if [ $? -eq 0 ]; then
echo “Backup copied to Nautilus Backup Server successfully.”
else
echo “Failed to copy backup to Nautilus Backup Server.”
exit 3
fi

Hi @tzeko

Can you please share the link to the lab?

https://engineer.kodekloud.com/task?id=680670695500bdf7ab9a7b28&status=redo

@Santosh_KodeKloud

I Generated SSH key:
ssh-keygen -t rsa -b 2048 -N “” -f ~/.ssh/id_rsa

Then, I copied the key to the backup server:
ssh-copy-id [email protected]

After that, I made the script executable:
chmod +x /scripts/official_backup.sh

I ran the script. The ZIP file was created on App Server 2 and successfully copied to the Backup Server. I checked — the files were where they should be.

However, when I submitted the task, I received the following message:

  • archive does not contain correct data on Backup Server

I think your $REMOTE_USER is the issue; you are using backup_user to log in to the Backup server. Whereas, the user for Backup server is clint.

Your scp command needs to be something like:

scp -r /backup/xfusioncorp_official.zip clint@stabkp01:/backup/

The credentials for all the servers is provided here.

@Santosh_KodeKloud
I copied the script from my own lab, so the values you see are incorrect. Actually, the variables from my script are:

SRC_DIR=“/var/www/html/official”
ARCHIVE_NAME=“xfusioncorp_official.zip”
LOCAL_BACKUP_DIR=“/backup”
REMOTE_BACKUP_DIR=“/backup”
REMOTE_USER=“clint”
REMOTE_HOST=“stbkp01.stratos.xfusioncorp.com

As I mentioned before, when I ran the script, the file xfusioncorp_official.zip was successfully copied to the stbkp01 server at the path /backup/xfusioncorp_official.zip.

I double-checked everything before clicking submit.

I just reproduced the lab, and it worked fine with me.
This can be done with a simpler script, as in the screenshot.

2 Likes

I completed the task using your script. However, if you compare both scripts, they perform the same operations — the only differences are that my version uses variables, checks if directories exist, and includes some additional validations. When I initially ran my script, the files were located on the stbckp01 server, but the task was still marked as incorrect.

Could this be a bug in the system? In any case, we can consider the issue resolved now. @Santosh_KodeKloud thanks for your help!

It could be that when the task fails for the first time and you try it again, the complete variable names are bound to be changed (If copied) according to the new requirement. A mismatch in the vars may be causing the issue.

1 Like