Advanced Linux Shell Scripting for DevOps Engineers with User management
Backup your work using automation through a bash script and crontab.

So Write a bash script createDirectories.sh that when the script is executed with three given arguments (one is directory name and second is start number of directories and third is the end number of directories ) it creates specified number of directories with a dynamic directory name.
Create a Script to backup all your work done till now.
Read About Cron and Crontab, to automate the backup Script
read about User Management
Create 2 users and just display their Usernames
Due to the length of the blog, I only addressed two tasks here. The remaining tasks will be covered in the subsequent blog.
Tasks1
There are total 90 sub-directories in the directory '2023' of this repository. What did you think, how did I create 90 directories. Manually one by one or using a script, or a command?
All 90 directories within seconds using a simple command.
mkdir day{1..90}
- You have to do the same using Shell Script i.e using either Loops or command with start day and end day variables using arguments -
So Write a bash script create directories.sh that when the script is executed with three given arguments (one is the directory name and second is the start number of directories and the third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.
Example 1: When the script is executed as
./createDirectories.sh day 1 90
then it creates 90 directories as day1 day2 day3 .... day90
Example 2: When the script is executed as
./createDirectories.sh Movie 20 50 then it creates 50 directories as Movie20 Movie21 Movie23 ...Movie50
Notes: You may need to use loops or commands (or both), based on your preference
Solution:
open the file with vim createDirectories.sh in the terminal and write the following script to make directories with their name, start number and end number.

:wq exit
then make the script executable by the following command
chmod 700 createDirectories.sh
after that run the script
./createDirectories.sh

Task 2
:- Create a Script to back up all your work done till now.
Backups are an important part of DevOps Engineers' day to Day activities.
<$>Backup script and cron job
you have to make the bash script for the backup of the file in Step 1
and in step 2 create the cron job.
step 1 :
vim backup_script.sh
Don’t give any space after the DESTINATION= , and SOURCEFOLDER= , OTHERWISE IT WILL THROUGH AN ERROR
make the script executable with following command:
chmod 700 backup_script.sh
execute it using ./script_name.sh. Before making cron job
now >>>> the whole code explanation >>>>>>>
Let's go through the provided Bash script step by step:

- This line is called the "shebang" and indicates that the script should be interpreted using the /bin/bash shell. It specifies the interpreter to be used when running the script
- This line sets the BACKUPTIME variable with the current date and time. The date command is used with the '+%Y-%m-%d_%H-%M-%S' format to get the current year, month, day, hour, minute, and second. The resulting value will be something like 2023-07-26_16-30-15.
- This line sets the DESTINATION variable with the full path to the backup file. It uses the BACKUPTIME variable to include the timestamp in the filename. The resulting filename will be something like /home/kulwinder/tane/backup-2023-07-26_16-30-15.tar.gz.
- This line sets the SOURCEFOLDER variable with the full path to the folder that you want to back up. In this case, the folder to be backed up is /home/kulwinder/cane.
- This line creates the tar archive with gzip compression. Here's a breakdown of the options used:
- -c: Create a new archive.
- -z: Compress the archive using gzip.
- -p: Preserve permissions of the files during archiving.
- -f: Specifies the filename of the archive (`$DESTINATION`).
The tar command will compress the contents of the $SOURCEFOLDER and create a tar archive with the name and path specified in the DESTINATION variable, using the timestamp in the filename.
Once the script is executed, it will create a compressed tar archive (`.tar.gz`) of the contents inside the /home/kulwinder/cane folder and store it with a timestamp in the filename inside the /home/kulwinder/tane/ directory. The resulting backup file will be named, for example, backup-2023-07-26_16-30-15.tar.gz based on the current date and time.
Remember to make the script executable (`chmod +x script_name.sh`) or
(`chmod 700 script_name.sh`) and execute it using ./script_name.sh.
step 2: Create the cron job of backup_script.sh using command.
Crontab -e
This will open the tab………… Cron tab example
Here we want that file should run at 17:08 evry day , every month ,every week so we use * to represent this.
* = every day
* = every month
* = every week
if its nano editor ctrl +x then save yes enter
And if it's vim editor then esc : +wq
In the last lines when we change the directory to the tane and do ls, we can see the backup file of all work done is created with a time stamp.
So, folks, this is all about automating your work and using the cron tab and bash script to automatically backup your work. If you think this post is helpful, please share it on LinkedIn so that others may easily grasp it. This essay primarily focuses on new learner. You can post any questions, and I'll do my best to answer them.



