Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management
#devops
All 90 directories within seconds using a simple command, creating 90 directories in one go as below:
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 createDirectories.sh that when the script is executed with three given arguments (one is directory name and second is 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.
#!/bin/bash directname=$1 startnum=$2 endnum=$3 for (( i =startnum; i <= endnum; i++)) do mkdir "$directname$i" echo "$directname$i" done
wq! To save the file and provide chmod 700 permission to file ,Execute the ./createDirectories.sh
output:
Create a Script to backup all your work done till now
#!/bin/bash #!/bin/bash echo "Do you have to take backup" read input echo "As you provided the path for backup " read backupath echo "$backupath, will take backup" path=$1 tar -czvf backup.tar.gz ${path}
wq! To save the file and provide chmod 700 permission to file ,Execute the ./backup.sh /home/ubuntu
Cron and Crontab, to automate the backup Script:
Cron is the system's main scheduler for running jobs or tasks unattended. a command called crontab allows the user to submit,
edit or delete entries to cron. A crontab file is a user file that holds the scheduling information.
The Cron daemon is a background process that runs continuously on the system and checks the crontabs of all users to see if any commands need to be executed at a particular time. Cron uses a configuration file called "crontab" to manage the schedule of commands.
Create the backup script: First, create the backup script that you want to automate. For example, let's create a script called "backup. sh" that backs up a specific directory:
#!/bin/bash src= /home/ubuntu/my-scripts tgt= /home/ubuntu/backups tar -czvf $tgt/$1.tar.gz $src echo "backup complete" file_name=$(date | xargs | awk '{print $3 "-" "$2" "-" "$6"}') echo $filname.tar.gz
Schedule the backup script with Cron: To schedule this script to run automatically using "cron", follow these steps:
Open the crontab editor by running the command "crontab -e" in your terminal.
If prompted to select an editor, choose your preferred text editor.
Add a new line to the crontab file with the following syntax:
The string 0 2 * is a cron expression that specifies when to run the command. In this case, it means "run the command at 2:00 AM every day".
0: The minute when the command should run (in this case, "0" means "at the start of the hour").
2: The hour when the command should run (in this case, "2" means "2:00 AM"). : The day of the month when the command should run (in this case, "" means "every day"). : The month when the command should run (in this case, "" means "every month").
: The day of the week when the command should run (in this case, "" means "every day of the week"). The command that follows the cron expression is /home/ubuntu/backup.sh /home/ubuntu /home/ubuntu/backup_directory. This means that the script located at /home/ubuntu/backup.sh will be executed, with /home/ubuntu as the source directory to be backed up and /home/ubuntu/backup_directory as the destination directory where the backup will be stored.
So the overall meaning of this cron job is to run the backup. sh script at 2:00 AM every day, using the specified source and destination directories.
crontab -e Use the crontab -e command to open your user account's crontab file
For checking use: crontab -l
Create 2 users and just display their Usernames
- To add a user to the Linux system:
useradd devops
- setting user password:
passwd devops
If we want to add a user by making a directory use the -m flag
sudo useradd devops -m
-d dir
Set the user’s home directory to be dir.
-s shell
Set the user’s login shell to be shell.
-u uid
Set the user’s ID to be uid. Unless you know what you’re doing, omit this option and accept the default.
-g group
Set the user’s initial (default) group to group, which can either be a numeric group ID or a group name, and which must already exist.
-G group1,group2,…
Make the user a member of the additional, existing groups group1, group2, and so on.
-m
Copy all files from your system skeleton directory, /etc/skel, into the newly created home directory. The skeleton directory traditionally contains minimal (skeletal) versions of initialization files, like ~/.bash_profile, to get new users started If you prefer to copy from a different directory, add the -k option (-k your_preferred_directory).
To check whether the user added or not if the user added where it should be present:
sudo cat /etc/passwd
Thank you for reading this Blog. Hope you learned something new today! If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future
I would like to connect with you at linkedin.com/in/madhuri-patil-278b19118