Day 10 of 100 Days of Devops
Linux Bash Script
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 2 in Stratos Datacenter, and they need to create a bash script named news_backup.sh which should accomplish the following tasks. (Also remember to place the script under /scripts directory on App Server 2).
a. Create a zip archive named xfusioncorp_news.zip of /var/www/html/news 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.
e. Do not use sudo inside the script.
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. Install it manually outside the script.
Mind mapping
Before we do this we need to check our requiremnts and list what commands we need to use
1 - Install zip package on server (yes we have to do first)
2 - create zip from the path and stored in designated path /backup/ (zip command to use)
3 - then copy the archive file to backup server using ssh without asking password (Require SCP with sshkey so that they wont ask password)
4 - make that automation into script
5 - give proper permission to script for execution
Prepare prequisties
1
2
3
4
5
6
yum install zip -y
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa -N ""
ssh-id-copy username@hostnameofbackupserver
Then write the following script for automation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash
SOURCE=/var/www/html/news
DEST=/backup
BACKUP_FILE_NAME=xfusioncorp_news.zip
BACKUP_SERVER_ADDR=stbkp01
BACKUP_SERVER_USR=clint
echo "Creating backup archive: $DEST/$BACKUP_FILE_NAME"
if [ ! -d "$SOURCE" ]; then
echo "ERR:Source directory $SOURCE does not exist";
exit 1;
fi
zip -r "$DEST/$BACKUP_FILE_NAME" "$SOURCE" || {
echo "ERR: Failed to create zip archive."
exit 1
}
echo "Verifying backup file"
if [ ! -d "$DEST" ]; then
echo "ERR:Source directory $DEST/$BACKUP_FILE_NAME does not exist";
sudo mkdir -p "$DEST"
sudo chown tony:tony "$DEST"
fi
echo "Backup file exists"
scp "$DEST/$BACKUP_FILE_NAME" "$BACKUP_SERVER_USR@$BACKUP_SERVER_ADDR:$DEST" || {
echo "Error: Failed to copy archive to backup server."
exit 1
}
echo "Backup completed successfully!"
exit 0
Verification
Login to backupserver
1
2
[clint@stbkp01 backup]$ ls -ld
You should be able to see the file.
Thanks all for today , Thx Bye.