The question was
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 media_backup.sh which should accomplish the following tasks. (Also remember to place the script under /scripts directory on App Server 3).
- Create a zip archive named xfusioncorp_media.zip of /var/www/html/media directory.
- 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.
- Copy the created archive to Nautilus Backup Server in /backup/ location.
- 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.
To automate the website backup as described, you need to create a Bash script on App Server 3 that:
- Creates a zip archive of /var/www/html/media.
- Saves the archive as xfusioncorp_media.zip in /backup/ (local).
- Copies the archive to the Nautilus Backup Server at /backup/.
- Ensures passwordless SSH is set up so the script runs without prompting for a password.
- Ensures it’s executable and placed at /scripts/media_backup.sh.
#!/bin/bash
# Set variables for directories
SOURCE_DIR="/var/www/html"
MEDIA_DIR="media"
ARCHIVE_NAME="xfusioncorp_media.zip"
TEMP_BACKUP_DIR="/backup"
REMOTE_BACKUP_SERVER="stbkp01.stratos.xfusioncorp.com"
REMOTE_BACKUP_DIR="/backup"
# Create a zip archive of the media directory
echo "Creating zip archive of $SOURCE_DIR/$MEDIA_DIR..."
zip -r "$TEMP_BACKUP_DIR/$ARCHIVE_NAME" "$SOURCE_DIR/$MEDIA_DIR"
# Check if the zip command was successful
if [ $? -eq 0 ]; then
echo "Archive created successfully: $TEMP_BACKUP_DIR/$ARCHIVE_NAME"
else
echo "Failed to create archive. Exiting."
exit 1
fi
# Copy the archive to the Nautilus Backup Server
echo "Copying the archive to Nautilus Backup Server..."
scp "$TEMP_BACKUP_DIR/$ARCHIVE_NAME" "clint@$REMOTE_BACKUP_SERVER:$REMOTE_BACKUP_DIR/"
# Check if the scp command was successful
if [ $? -eq 0 ]; then
echo "Archive copied to $REMOTE_BACKUP_SERVER:$REMOTE_BACKUP_DIR/"
else
echo "Failed to copy the archive to backup server. Exiting."
exit 1
fi
echo "Backup process completed successfully."
I apply these commands
on server 3
-
ssh banner@stapp03
-
BigGr33n
-
mkdir -p /scripts
-
sudo vi /scripts/media_backup.sh
-
BigGr33n
-
sudo chmod +x /scripts/media_backup.sh
-
sudo mkdir -p /var/www/html/media
-
sudo chmod -R 755 /var/www/html/media
# or -
sudo chown -R tony:tony /var/www/html/media
-
zip -r /backup/xfusioncorp_media.zip /var/www/html/media
# Test thezip
Command Manually -
/scripts/media_backup.sh
-
scp /backup/xfusioncorp_media.zip [email protected]:/backup/
# Test the Remote Backup Serverscp
Works -
ssh-keygen -t rsa -b 2048
-
ssh-copy-id [email protected]
CronJob
-
crontab -e
-
0 2 * * * /scripts/media_backup.sh
-
sudo systemctl status cron
-
sudo systemctl start cron
-
grep CRON /var/log/syslog
When test manually then it will work properly, and when I try using bash script on this time, I get the errors
No worries!!
Uh oh! Looks like the task was not completed successfully. But it’s Ok. You can try again next time this task is assigned to you.
- ‘
blog_backup.sh
’ not found under ‘/scripts’ on App Server 3
You may check your work again to see what went wrong. The environment expires in 5 minutes.
If you think you did your work correctly and is marked failed, you may request for a review from your task dashboard. Or alternatively please submit outputs/screenshot(s) of your work and post to community.kodekloud.com