62.3k views
1 vote
Create a new user Alice (with home directory) and

Write a shell script that backups Alice’s home directory by creating a tar file (tape archive), using the following steps:
i. Take 2 inputs with their values- your MIDAS name and current date (for example, midas=asilv).
ii. Create a variable named as filename that should be assigned the value as MIDAS-date (example output after executing the script would be like, asilv-2021.3.17-01.16.430).
iii. Using tar command, create a tape archive for Alice’s home directory (/home/Alice) and the filename created above (in step-2-ii). (Please learn about tar command in Linux for its usage)
b. Move the tape archive file/tar file (created in step 2-iii) to /var/backups/ directory using correct command in linux.
c. To optimize the disk usage, pick a compression algorithm (bz2, gzip, or xv) to compress the tar file you created in /var/backups/ in the previous step-2b.
Create a crontab file to keep the scheduled task running for 3 minutes, then check the contents in the /var/backups directory.
Cancel the crontab jobs

User Soumya Das
by
8.3k points

1 Answer

5 votes

Final answer:

To backup Alice's home directory on Linux, a shell script is used to create and compress a tar archive file before moving it to /var/backups. A cron job can be set up to run this script every 3 minutes, which can be later canceled by editing the crontab.

Step-by-step explanation:

The process of backing up a user's home directory in Linux involves creating a tar archive file and then compressing it for efficient storage. Below is a shell script that performs the backup operation following the steps you have outlined:

#!/bin/bash

# Input MIDAS name and current date
read -p "Enter your MIDAS name: " midas
read -p "Enter current date (YYYY.MM.DD): " date

# Create a filename variable
filename="${midas}-${date}"

echo "Creating backup for Alice's home directory..."

# Create a tar file of Alice's home directory
tar -cvf "/var/backups/${filename}.tar" /home/Alice

echo "Backup file created: ${filename}.tar"

# Move the backup file to the /var/backups/ directory
# mv "/var/backups/${filename}.tar" /var/backups/ # This command is redundant as we're already creating tar in the /var/backups

echo "File moved to /var/backups/"

# Compress the tar file using gzip
gzip "/var/backups/${filename}.tar"

# Confirm file compression
ls -lh "/var/backups/${filename}.tar.gz"

# Schedule the backup to run every 3 minutes using crontab (add this line to crontab)
# */3 * * * * /path/to/backup_script.sh

# After confirming file in /var/backups, disable the cron job by commenting out or deleting the line from crontab

To schedule the backup task to run every 3 minutes, you will need to add the appropriate cron job. You can do this by running 'crontab -e' and entering the line as indicated in the script comments. Afterwards, to view the contents of the /var/backups directory, you can use the 'ls' command. To cancel the crontab jobs, you will need to edit the crontab again and either comment out or delete the line for the job.

Please remember to grant the script executable permissions using 'chmod +x scriptname.sh', replace 'scriptname.sh' with the name of your script. Additionally, before performing any of the above operations, you should have the appropriate permissions or use the 'sudo' command if needed.

User Mike Malloy
by
8.3k points