Question

Create a bash script to do the following on your CentOS6 VM. To back up your...

Create a bash script to do the following on your CentOS6 VM.

  1. To back up your home folder using tar command daily. Use gzip as the compression for tar (z option). – 10pts

Hint: Make sure you exclude the tar file itself from the backup.

  1. The script should name the tar file backupYYYYMMDD.tar.gz -10 pts
  2. The script should keep the last seven days of backup files and delete backup files older than seven days. -10 pts
  3. The script should check to see if there is enough disk space before commencing the backup. If not it should notify you via email that the back up failed due to insufficient disk space and end for that day. -5 pts
  4. On successful completion the script should email you a report the starting date and time, ending date time and quantity of data copied. -5 pts
  5. Set the permission on the backup file so the owner has read privileges and group and others have none. – 5 pts
  6. Schedule the script in crontab to run every day at 6:00 PM. – 5 pts
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. How does tar work?

Tar is a program for archiving on Linux and related systems. Atypically for this kind of program, tar doesn’t offer compression by default. But the program is very popular because it offers the great advantage that entire directories can be merged into one file.

Install tar

With Ubuntu, tar should already be pre-installed. If you use another Linux or Unix distribution, install the helpful program with:

sudo apt-get install tar tar-doc

The tar-doc package is optional: It contains documentation of the archiving program.

When creating tar archives, you also have the option to create wildcards with an asterisk. If you create a new archive, always indicate the options first, then the file names of the archive that you want to create, and finally the files and folders that it should contain. In the following example, create an archive (-c) from two text files, compress it with gzip (-z), and write it to the file archive.tar.gz (-f):

tar -czf archive.tar.gz example_1.txt example_2.txt

If you want to combine all text files in a directory into an archive, use a corresponding wildcard:

tar -cf text_archiv.tar *.txt

You can also combine complete directories and their subdirectories into an archive. In the following example, /directory1 including all its subdirectories and the contained files is archived, excluding the subdirectory /directory1/subdirectory_x:

tar -cf archive.tar --exclude=”/directory1/subdirectory_x” /directory_1

In the following example, you extract (-x) the compressed (-z) archive that we created in the first example into another directory (-C):

tar -xzf archive.tar.gz -C /home/directory1/archive_directory

To add another file to an archive (which has to be uncompressed), enter the following command:

tar -rf archive.tar example_extra.txt

Create an incremental backup with tar

2. With tar, you can create regular incremental backups. You can also write your own backup script. For example, you can specify that a full backup is to be created once a month and then an incremental backup is performed daily. The following script also makes sure that old backups are regularly moved into folders sorted by date. In addition to tar, you also need cron. This daemon (a program that runs in the background) allows for time-based execution of other processes, and is always included with Ubuntu. First, open another text editor and create this script:

#!/bin/bash
BACKUP_DIR=“/targetdirectory/backup”
ROTATE_DIR=“/targetdirectory/backup/rotate”
TIMESTAMP=“timestamp.dat”
SOURCE=“$HOME/sourcedirectory ”
DATE=$(date +%Y-%m-%d-%H%M%S)
EXCLUDE=“--exclude=/mnt/*--exclude=/proc/*--exclude=/sys/*--exclude=/tmp/*”
cd /
mkdir -p ${BACKUP_DIR}
set -- ${BACKUP_DIR}/backup-??.tar.gz
lastname=${!#}
backupnr=${lastname##*backup-}
backupnr=${backupnr%%.*}
backupnr=${backupnr//\?/0}
backupnr=$[10#${backupnr}]
if [ “$[backupnr++]” -ge 30 ]; then
  mkdir -p ${ROTATE_DIR}/${DATE}
  mv ${BACKUP_DIR}/b* ${ROTATE_DIR}/${DATE}
  mv ${BACKUP_DIR}/t* ${ROTATE_DIR}/${DATE}
  backupnr=1
fi

backupnr=0${backupnr}
backupnr=${backupnr: -2}
filename=backup-${backupnr}.tar.gz
tar -cpzf ${BACKUP_DIR}/${filename} -g ${BACKUP_DIR}/${TIMESTAMP} -X $EXCLUDE ${SOURCE}

The above steps are

  • First, define the interpreter again.
  • Then, set the variables. New additions are a directory for the rotations of the backups (a type of backup archive), and a file for a timestamp.
  • In our example, we illustrate that it doesn’t always make sense to take all directories along in the backup. In this case, we’ve excluded the contents of the folders mnt, proc, sys, and tmp (but not the folders themselves, hence the “*”). The files in these directories are either temporary or created fresh with each system start.
  • To make sure that all paths are interpreted correctly, the script switches to the root directory with cd /.
  • Set up the backup directory with mkdir, in case it doesn’t exist yet.
  • All variables are now input. Since you want to number your backups sequentially, the code block determines the number of the last backup. This is done by removing the other parts of the file name in the script.
  • You only record 30 backups at a time, after which the script moves all archive files into the rotation folder. This is created first, and then all files starting with the letters b and t are moved into the new folder. The limitation of the letters is explained by the fact that there should only be files marked with those features in the folder: backup and timestamp. Finally, the script resets the backup number to 1. If your script detects that 30 backups haven’t been created yet, it simply increases the file number by 1 (++).
  • Now the script reverses what it did at the beginning: The commands ensure that the file name is complete again – with the new number.
  • Finally, the script runs the actual tar command: As opposed to the command of the simple full backup, there are further options available here. With -g the incremental backup is enabled. For this, tar reads the timestamp of each file, compares it with the data recorded so far in timestamp.dat, and can then decide which changes have been made since the last backup. Only these become part of the new archive.

3.This completes the script for the creation of an incremental backup with tar: Save the file as backup in the bin directory. You also need to export the path here and make the script executable:

PATH=$PATH:$HOME/bin
chmod u+x $HOME/bin/backup

Theoretically, you can now start your backup script with sudo backup. But the idea behind the incremental backup is that the process is automatically run every day. For this, you access cron and change the so-called Crontab. This is the table that sets how cron tasks run. It has six sections:

Minutes (0-59) Hours (0-23) Days (1-31) Months (1-12) Days of the Week (0-7) Task

In these sections, you can either enter the corresponding number value (indicated in parentheses) or an asterisk (*). The latter basically for every possible value. One special feature is the days of the week section. Here you can set that a task is performed, for example, every Monday (1) or only on weekdays (1-5). Sunday can be given using two different values: Either 0 or 7 refers to Sunday, since for some people the week begins on this day and for others it ends.

In the command line, open the editor mode of cron with:

sudo crontab –e

5.Here, enter the following line:

6 00* * * /home/bin/backup

This means that the backup will be performed at 6:00 p.m. every day (and every month, regardless of the day of the week). Save your changes, and a daily incremental backup is ready to use.

7

Restore system from a backup

Nobody would ever wish it upon anyone, but sometimes the worst happens and your system needs to be completely restored. With tar, this is also relatively easily done and requires no additional script. A single command for a full backup isn’t possible though: It’s in the nature of incremental backups that multiple files must be unpacked. In the console, enter these command lines:

BACKUP_DIR=/targetdirectory/backup
cd /
for archive in ${BACKUP_DIR}/backup-*.tar.gz; do
tar -xpzf $archive -C /
done
Add a comment
Know the answer?
Add Answer to:
Create a bash script to do the following on your CentOS6 VM. To back up your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Bash Write two scripts, 'backup.sh' and 'restore.sh' which do the following: backup.sh accepts a list...

    In Bash Write two scripts, 'backup.sh' and 'restore.sh' which do the following: backup.sh accepts a list of arguments which indicate the name o the output file and a list of files that should be backed up. Will take all of the files provided oher than the first and create a tar-ball(a tarred, gzipped file) with the name provided in the first argument. restore.sh Accepts a single filename of a tar-ball created by the script above and restores the files from...

  • Create a VBScript script (w3_firstname_lastname.vbs) that takes one parameter (folder name) to do the following 1)...

    Create a VBScript script (w3_firstname_lastname.vbs) that takes one parameter (folder name) to do the following 1) List all files names, size, date created in the given folder 2) Parameter = Root Folder name - The script should check and validate the folder name 3) Optionally, you can save the list into a file “Results.txt” using the redirection operator or by creating the file in the script. 4) Make sure to include comment block (flowerbox) in your code. 5) Sample run:-...

  • Objective : Write a C Shell script which copies all files(*.java and *.class) from your home dire...

    Objective : Write a C Shell script which copies all files(*.java and *.class) from your home directory to a new one, and Analyze the new directory information such as number of files, user permissions, and disk usage. Sample Output:                                                    << CS Directory Analysis >>      Date:   ============================================================ Current Directory: /home/tomss New Directory Created : /home/tomss/pgm01 File information Total Number of files : 22 files Directory files:   0 files Plain text files:   10 files File have read permissions: 3 files File have...

  • Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin...

    Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin at the University of Washington, Seattle, who originally wrote this assignment (for their CSE 142, in Java) This program focuses on classes and objects. Turn in two files named Birthday.cs and Date.cs. You will also need the support file Date.dll; it is contained in the starter project for this assignment. The assignment has two parts: a client program that uses Date objects, and a...

  • Design an original, professional web site following the specifications listed below. This web sit...

    Design an original, professional web site following the specifications listed below. This web site will be for a business you plan to set up for yourself or for someone else. The following is a detailed list of the requirements for your web site. READ them carefully. Instructions - Web Site Requirements for the web site: General: You will thoroughly test all your pages in more than one browser. All links MUST work. All graphics must show on the page. All...

  • TRUE/FALSE QUESTIONS:  Foundations of Information Security and Assurance 1. There is a problem anticipating and testing for...

    TRUE/FALSE QUESTIONS:  Foundations of Information Security and Assurance 1. There is a problem anticipating and testing for all potential types of non-standard inputs that might be exploited by an attacker to subvert a program. 2. Without suitable synchronization of accesses it is possible that values may be corrupted, or changes lost, due to over-lapping access, use, and replacement of shared values. 3. The biggest change of the nature in Windows XP SP2 was to change all anonymous remote procedure call (RPC)...

  • Risk management in Information Security today Everyday information security professionals are bombarded with marketing messages around...

    Risk management in Information Security today Everyday information security professionals are bombarded with marketing messages around risk and threat management, fostering an environment in which objectives seem clear: manage risk, manage threat, stop attacks, identify attackers. These objectives aren't wrong, but they are fundamentally misleading.In this session we'll examine the state of the information security industry in order to understand how the current climate fails to address the true needs of the business. We'll use those lessons as a foundation...

  • Please read the article and answer about questions. You and the Law Business and law are...

    Please read the article and answer about questions. You and the Law Business and law are inseparable. For B-Money, the two predictably merged when he was negotiat- ing a deal for his tracks. At other times, the merger is unpredictable, like when your business faces an unexpected auto accident, product recall, or government regulation change. In either type of situation, when business owners know the law, they can better protect themselves and sometimes even avoid the problems completely. This chapter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT