Lab 7c Backups

with Crons

Aims

1. Be able to use cron for scheduling regular tasks

2. To use standard UNIX tools to implement a simple backup process

Task 1: Using cron

Create a simple cron job that will create an empty file in the /tmp directory (using the touch command) at a predefined time. To understand the format of the information required in a crontab file, type: man 5 crontab (the "5" forces the command to be searched for in section 5 of the manual – section 5 contains descriptions of system file contents). Schedule your cron job to execute a few minutes later than the current time (at least 2 minutes later than the current time). Test that your cron job is working by looking for the empty file in the /tmp directory.


Note that there are two places you can put your cron job – either in /etc/crontab by using a normal text editor, or in the root user's own personal cron table. In the /etc/crontab file, you need to specify the username under which the job will be executed, but the username is not used in a user’s personal cron table. The crontab command is used to edit/view/remove a user's personal cron table:

crontab –e (edit) crontab –l (list)

crontab –r (remove)

The editor used by default is vi. If you wish to use the nano editor (or gedit in a graphical environment), before you run any of the crontab commands, at your shell prompt, enter:

export EDITOR=nano

to set the value of the EDITOR environment variable equal to the name of the editor you wish to use by default.

Try creating cron jobs in both ways – using /etc/crontab and root's personal crontab file. Remember to test that they work.


Task 1 - Steps

We are using different user to

Manual for crontab to learn more

# man 5 crontab

There are many information but we will be focusing on field information that is important to create recursive job.

Putting job using /etc/crontab to edit the file

# vim /etc/crontab

Add the following

14 16 *** stewie touch /tmp/stewie_global

:wq

Check stewie user

# su stewie

# crontab -e // to edit personal user//

Add the following in following similar to previous one

14 16 *** touch /tmp/stewie_local

Now check etc/crontab

# cat /etc/crontab

# crontab -e


Task 2: Backup and restore

Your task is to backup two directories on your system, using two different backup tools. An introduction to the commands and their options is in the lecture slides – for more detail, refer to the man pages.

First, create a backup of the /etc directory using tar, and store the backup in a file named /tmp/backup-etc.tar

Secondly, create a backup of the /opt directory using cpio, and store the backup in a file named

/tmp/backup-opt.cpio

View a table of contents of each of the archives to verify that the backups were created successfully.

Note that this is not the way backups are really implemented. Normally you backup an entire filesystem at once. We don't do that because we don't have enough spare disk space to store a copy of an entire filesystem backup. Also, normally your backup is stored on a tape device (or other media), so you would write the backup to /dev/tape or similar. We don't do that because we don't have a tape device or other removable media big enough (in theory we could do a small backup with a USB hard drive).

Now, from your tar backup, restore the file /etc/hosts into your (root's) home directory, just to test that you can do so.

It is important to always test that you can restore a file from your backups – you don't want to find out that your backup is corrupted when you really need to restore a file.


Task 2 - Steps

Let’s switch to super user $ exit

Cd to root directory

# cd /

# pwd //check current working directory//

Next we use tar -cf to back up file called temp

# tar -cf /tmp/backup-etc.tar etc

//etc is directory//

Check it

# ls /tmp

To check wha’t inside the file we can use the following command

# tar tf /tmp/back-etc.tar