Hello Readers,
Here we are Day 5 of #90daysofdevops
👣 Topics for #day5
How to create multiple directories by making use of Shell scripting?
How much important is that to take Backups as a DevOps practitioner?
Sample Script to backup a directory
What is Cron and CronTab?
What is User Management?
How to create multiple directories by making use of Shell scripting?
#!/bin/bash
echo 'Creating Directories....'
mkdir TasksFolder
cd TasksFolder
for ((i=$2 ; i<$3 ; i++))
do
mkdir "$1_$i"
done
Explanation:
#!/bin/bash
: This line is known as a "shebang" which tells the system that this script should be executed using the bash shell.echo 'Creating Directories....'
: This line simply prints out the text "Creating Directories...." to the console.mkdir TasksFolder
: This line creates a new directory called "TasksFolder" in the cwd.cd TasksFolder
: This line changes the current working directory to the "TasksFolder" directory.for ((i=$2 ; i<$3 ; i++))
: This line starts a loop that will create a series of directories. The loop uses the values passed to the script as the second and third command-line arguments ($2 and $3, respectively) basically to determine how many directories to create. The variable $i is set to $2 initially, and the loop continues as long as $i is less than $3. The "((...))" syntax is used to perform arithmetic operations in the loop.mkdir "$1_$i"
: This line creates a new directory with a name that includes the first command-line argument ($1) followed by an underscore and the current value of $i../createDirectories.sh Day 1 90
This line will create directories named "Day_1", "Day_2", "Day_3",until "Day_90".
So, this script creates a directory called "TasksFolder" and then creates a series of subdirectories within it, with names that include a prefix passed as the first argument and a number between the second and third arguments.
How much important is that to take Backups as a DevOps practitioner?
Taking backups is extremely important for as a DevOps practitioner.
It is a critical part of ensuring the reliability and availability of your systems and applications.
Backups are essential for disaster recovery, allowing you to quickly restore your systems and data in the event of any failure or data loss. They can also be useful for testing and development purposes as well, enabling you to easily create and test new environments without risking the loss of main data.
As a DevOps practitioner, it is important to understand the various backup strategies available and choose the one that best meets the needs of your organization. This may include strategies such as full backups, incremental backups, and differential backups.
Sample Script to backup a directory
#!/bin/bash
back_up_file="backup$(date +%y-%m-%d-%H-%M-%S).tar.gz"
dir="/home/pavan07"
tar -czf $back_up_file $dir
echo "Backup completed : $back_up_file"
Explanation:
#!/bin/bash
: We already know this. Recall if you don't remember!!!
back_up_file="backup$(date +%y-%m-%d-%H-%M-%S).tar.gz"
: This line creates a variable called "back_up_file" and sets it to a string that includes the current date and time in the format "YY-MM-DD-HH-MM-SS". This string will be used as the filename for the backup file, which will be a compressed tar archive (.tar.gz).dir="/home/pavan07"
: This line creates a variable called "dir" and sets it to the path of the directory that should be backed up. In this case, it's set to "/home/pavan07".tar -czf $back_up_file $dir
: This line creates a compressed tar archive of the directory specified in the "dir" variable, using the filename specified in the "back_up_file" variable.The "-c" flag tells tar to create a new archive,
The "-z" flag tells it to compress the archive using gzip,
The "-f" flag specifies the filename of the archive.
The "$back_up_file" and "$dir" variables are used to fill in the filename and directory path, respectively.
./backup.sh
This line will create the backup files.
So, this script creates a compressed tar archive of a specified directory and names the archive with the current date and time. The backup file is saved in the current working directory. You can modify the directory to backup and location where to save the backup file by changing the value of the "dir" variable.
What is Cron and CronTab?
Cron is a time-based job scheduler in Unix-like operating systems. It allows you to schedule jobs (commands or scripts) to run automatically at specified intervals, such as daily, weekly, or monthly. Cron is widely used in DevOps and system administration to automate routine tasks and maintenance activities.
CronTab is the command used to create and manage the cron job schedules. It is a file that specifies the schedule and the command to be executed. The CronTab file consists of six fields that define the schedule for the command or script to be executed:
minute (0-59)
hour (0-23)
day of the month (1-31)
month (1-12)
day of the week (0-6, where 0 is Sunday)
command to be executed
Sample Anatomy
The CronTab file can be edited using the crontab
command, which allows you to add, remove, or modify cron jobs.
Here's an example of a CronTab entry that runs a script every day at 2:45am:
45 2 * * * /pavan07/script.sh
This CronTab entry would execute the /pavan07/script.sh
script at 2:45am every day. The 45
specifies the minute (45), the 2
specifies the hour (2am), and the *
specifies that the job should be run every day of the month and every month of the year.
What is User Management?
User management is an important task in Linux system administration. Here are the basic steps involved in user management in Linux:
- Creating a new user: You can create a new user in Linux using the
useradd
command.
For example, to create a new user named "Harsha", you can use the following command:
sudo useradd Harsha
- Setting a password for the user: You can set a password for the new user using the
passwd
command.
sudo passwd Harsha
This will prompt you to enter a new password for the user.
- Granting sudo access: If you want to grant the new user sudo access, you can add them to the
sudo
group using theusermod
command.
sudo usermod -aG sudo Harsha
This will add the user "Harsha" to the "sudo" group, which grants them sudo access.
- Deleting a user: To delete a user, you can use the
userdel
command. For example:
sudo userdel Harsha
This will delete the user "Harsha" from the system.
- Modifying user accounts: You can modify user accounts using the
usermod
command. For example, to change a user's home directory, you can use the following command:
sudo usermod -d /pavan07/home Harsha
This will change the home directory for the user "Harsha" to "/pavan07/home".
- Locking and unlocking user accounts: You can lock a user account to prevent them from logging in using the
usermod
command. For example:
sudo usermod -L Harsha
This will lock the user account for "Harsha". To unlock the account, you can use the following command:
sudo usermod -U Harsha
Example: Create 2 users and just display their Usernames
sudo useradd Harsha -m
sudo useradd Manu -m
Thank you for reading my Blog. I hope you have learnt something from it! If you find this blog helpful, please like, share, and follow me for more interesting posts like this in the future.
Please navigate to my GitHub Repo: GitHub Repo Link
to check my solutions for the tasks of 90daysofdevops Challenge.