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