NEED HELP in this bash script

Submitted 3 years, 4 months ago
Ticket #316
Views 283
Language/Framework ShellScript
Priority Medium
Status Closed

Preliminary Info: Starting with names like Tech_backup1.tar.gz tech_backup11.tar.gz tech_backup41.tar.gz under the / home / backup folder. The Tech_backup part is fixed, followed by a random number of GZ compressed files. All of them have a different creation date.

The bash / shell script you will write should include the following steps.

1. Find the last file created from these files.

2. Extract this file from the GZ. (The name of the file inside is the same as the compressed file, instead of just the tar.gz part, the extension of the file is bck. Ex: Extracted version of the tech_backup11.tar.gz file is named tech_backup11.bck)

3. Create a folder under / var / Tech / backups / if there is no folder with today's date (Format YYYY-MM-DD). (Ex: Folder layout: / var / Tech / backups / 2020/12/28)

4. Transfer the parsed file from step 2 to the folder in step 3.

5. Make sure that under the / var / Tech / backups / folder, there are only files and folders for the last 30 days. (delete old ones)

6. Compress the file under the / home / backup folder with the same name and add the date format information specified in item 3 to the end with tar.gz. (For example: Tech_backup11.bck file Tech_backup11-2020-12-28.tar.gz)

Submitted on Dec 06, 20

Looks like it is mini project 1😛, which portion you have questions? - Vengat 3 years, 4 months ago

can we connect and understand your requirement? - Vengat 3 years, 4 months ago

Muhammad , if the below answer resolved your issue , can you please go ahead and update ticket status!! - Anonymous 3 years, 4 months ago
add a comment

1 Answer

Verified

We can use below sample script for taking the backup,

dest="/home/test/Downloads/Scripts/Test_Folder"

#The line below will store multiple folders in an array (files)
files=( "/folder1" "/folder2" "/home/username/anotherfolder" )
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-TEST-$day.tgz"


for i in "${files[@]}"  # For every line in FILES
do # Do the following, starting with the first one:
    # Print start status message.
    echo "Backing up $i to $dest/$archive_file"  # Here, $i will be the first directory
    date
    echo

    # Backup the files using tar.
    tar czf $dest/$archive_file $i # Again, $i is the directory    
done # Stop here and do the loop again with the next item in FILES

# Print end status message.
echo
echo "Backup finished"

Submitted 3 years, 4 months ago

Can you update the status of the ticket?

- Vengat 3 years, 4 months ago


Latest Blogs