Skip to main content

Command Palette

Search for a command to run...

#90 days of DevOps challenge - Task 5

Updated
β€’5 min read
#90 days of DevOps challenge - Task 5
M

I am passionate about learning new skills . Currently quenching my thirst for knowledge by learning DevOps methodology.

A quick scan of the '2023' directory in this repository reveals a grand total of 90 subdirectories https://github.com/mvgeorge1/90DaysOfDevOps.git

Did you really think I made all 90 directories by hand? ROFL!! πŸ˜‚πŸ˜‚

Do I look like some kind of superhuman directory-making machine 🦸🦸🦸!?? No way, I used a script or a command, of course!

I was able to create all 90 directories within mere seconds, thanks to my mastery of a simple command!

mkdir day{1..90}

AN AWESOME

DevOps Engineer is HARD TO FIND,

difficult to part with &

IMPOSSIBLE to forget.

Task 1: Are you tired of creating directories one by one? Well, have no fear because, with the CreateDirectories.sh bash script, you can make a specified number of directories with a dynamic name in just one go! Simply execute the script with three arguments - the directory name, the starting number of directories, and the ending number of directories - and let the magic happen.

This is a Bash script that creates a specified number of directories with a dynamic directory name.

Here is how it works:

  1. The first line (#!/bin/bash) is known as a shebang and specifies the interpreter that should be used to execute the script.

  2. The for loop starts at the number specified in the second argument ($2) and loops until the number specified in the third argument ($3).

  3. Within the loop, the mkdir command is used to create a directory with a dynamic name ($1$i), where $1 is the first argument (the directory name) and $i is the current iteration of the loop.

  4. The loop continues until it has created the specified number of directories.

Task 2: Create a Script to backup all your work done till now.

#!/bin/bash

# Set the current date and time
date=$(date +%Y-%m-%d_%H-%M-%S)

# Set the directory where the backup will be stored
backup_dir="/home/ubuntu/Backup_dir"

# Set the directory that contains the work to be backed up
work_dir="/home/ubuntu/Directories/"

# Create the name of the backup file
backup_file="$backup_dir/workbackup$date.tar.gz"

# Create the backup
tar -czvf $backup_file $work_dir

echo "Backup complete: $backup_file"

Task 3: About Cron and Crontab, to automate the backup Script

Cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule commands or scripts to run automatically at specified intervals or times.

Crontab is a command that allows users to create, edit, and manage their cron jobs. The crontab command is used to create and modify cron job schedules, while the cron daemon is responsible for executing these schedules.

To automate the backup script using cron, you can create a new cron job by editing the crontab file with the command crontab -e. This will open the crontab file in your default text editor.

Then, you can add a new line to the file to schedule the backup script to run at a specific interval or time. For example, to run the backup script every day at 1:30 AM, you could add the following line to the crontab file:

30 1 * * * /bin/bash /path/to/backup.sh

This line specifies that the script located at /path/to/backup.sh should be run every day at 1:30 AM. The first two fields specify the minute and hour when the job should run, while the asterisks indicate that the job should run every day of the month and every month of the year.

Once you save and exit the crontab file, the cron daemon will automatically execute the backup script at the specified time interval. You can view the list of all scheduled cron jobs with the command crontab -l.

after doing crontab -e

Second example of Crontab,

This is a crontab entry that runs a backup shell script every minute.

The crontab entry is composed of six fields, separated by spaces, that represent the time and frequency at which the command should be executed. In this case:

  • The first field (/1) specifies that the command should run every minute. The asterisk () means every, and the forward slash (/) is used to specify the frequency, in this case, every minute.

  • The remaining fields (asterisks) represent the hour, day of the month, month, and day of the week, respectively. In this case, they are all set to asterisks, which means that the command will be executed every day, every month, and every day of the week.

The command that is executed is ". /home/ubuntu/backup.sh". This command sources the backup.sh shell script, which is located in the /home/ubuntu directory. The leading period (.) is a shorthand for the "source" command, which tells the shell to read and execute the contents of the specified file. In this case, the backup.sh script is executed every minute as specified by the crontab entry.

Task 4: About User Management

In Linux, user management involves creating, modifying, and deleting user accounts. The following are some common user management tasks in Linux:

  1. Creating a user account: To create a new user account in Linux, you can use the adduser command. For example, to create a new user named "john", you can run the following command:
 adduser john
  1. Modifying user account settings: You can modify various settings for a user account, such as the username, home directory, and login shell. To do this, you can use the usermod command. For example, to change the username of the "john" account to "jane", you can run the following command:
usermod -l jane john
  1. Setting a user password: To set a password for a user account, you can use the passwd command. For example, to set a password for the "jane" account, you can run the following command:
passwd jane
  1. Granting sudo privileges: You can grant a user account sudo privileges, which allows the user to run commands with elevated privileges. To do this, you can add the user to the sudo group using the usermod command. For example, to grant sudo privileges to the "jane" account, you can run the following command:
 usermod -aG sudo jane
  1. Deleting a user account: To delete a user account, you can use the userdel command. For example, to delete the "jane" account, you can run the following command:
userdel jane

These are just some examples of common user management tasks in Linux. There are many other commands and options available for managing user accounts, depending on your specific needs and requirements.

Task 5: Create 2 users and just display their Usernames

Connect with me over LinkedIn: https://www.linkedin.com/in/mariya-george-7b0821269/ for more such amazing contents on DevOps. See you soon!!