Create a series of directories with the help shell script using run time arguments,Create Backup, Make all the above task automate by cron/crontab
All the above tasks I have already explained in my previous blogs but today we will learn these tasks in detail.
Let's start our first task:
Create a series of directories with the help of a shell script using run-time arguments.
Question: Why do we need to create these kinds of directories:
The answer is, suppose we got a request to create 100 directories in a particular location. It will be a very time taking task and maybe we made some mistakes right? so reduce the time and get the accuracy we used shell script.
Now let's jump to our terminal and create a shell script for the same.
$ sudo nano runtargs.sh
#!/bin/bash
# Check if the arguments are 3 or not.
if [ "$#" -ne 3 ]
then
echo "Usage: $0 dir_name start_num end_num "
exit 1
fi
# Define the arguments to variables
dir_name="$1"
start_num="$2"
end_num="$3"
#loop through by the range of directory numbers
for (( i="$start_num"; i<="$end_num"; i++ ));
do
mkdir "$dir_name$i"
done
echo "Your directories are created below"
ls
Now run the script.
$ ./runtargs.sh
Usage: ./runtargs.sh dir_name start_num end_num
Now pass the arg.
$ ./runtargs.sh file 1 10
Your directories are created below
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
Now let's move to our next task:
Create a Backup of my above work
We have a directory called "scripting" and want to take a backup of this directory and want to move it to another path for example I will use the target path of my backup "/backup" from "/prod/scripting" and I want to use the backup file name as today's date
Now let's jump to the terminal.
# create a bkp.sh file
sudo nano bkp.sh
Now write down below code
#!/bin/bash #these tags are script starting tag
src_dir=/prod/scripting #souce of the directory
tgt_dir=/backup #target/destination of backup file
# now get current time stamp by below commands
current_timestamp=$(date "+%Y"-"+%m"-"+%d"-"+%H"-"+%M"-"+%S")
#defining backup file name and taking backup
backup_file=$tgt_dir/$current_timestamp.tgz
echo "Taking backup on $current_timestamp"
echo "$backup_file"
#Now create a tar file to compress it.
tar czf $backup_file --absolute-names $src_dir
echo "Backup Completed"
Here is the output.
/backup# la
2023-+04-+01-+15-+09-+10.tgz
Now let's do our last task making these tasks automate by a shell script.
Automate these tasks
Before automating the task let's discuss what is cron and crontab.
CRON: Cron is a time-based job scheduler in Unix-like operating systems, which can automatically execute commands or scripts at specified times or intervals. Cron is commonly used for scheduling repetitive tasks, such as backups, system maintenance, and automated tasks.
CRONTAB: A crontab file is a simple text file that contains a list of commands meant to be run at specified times. The crontab
command is used to create, modify, and manage the crontab files for individual users. Each line of the crontab file specifies a command or script to run, along with a schedule for when it should be run. The syntax for each line is as follows:
* * * * * command to be executed every minute
- - - - -
| | | | |
| | | | +----- day of the week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of the month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
Now let's automate both above tasks and jump to the terminal and crontab.
$ crontab -e
# paste below code at the of the file.
* * * * * /prod/scripting/bkp.sh
* * * * * /prod/scripting/runtargs.sh file 11 13
Here is the backup
/backup# ls 2023-+04-+01-+15-+09-+10.tgz
Here are the new directories.
file11 file12 file13
I hope it helps!!
Happy Learning
Saif Raza Khan