Backup
It is extremely important to take backups of your data. Data loss can't be predicted and it is needless to say that any loss of data is a loss of time and money.
What is a backup?
Backups are simply archiving your data strategically so that in the case of loss, you can recover it from those archives.
Types of backup
- Full - Backup the complete data set
- Incremental - Backup only changes since the last backup
- Differential - Backup changes since the last backup cumulatively
Periodic backup
- Daily - Hold for the short term
- Weekly - Hold for the medium term
- Monthly - Hold for the long term
Backup using a shell script
Using a shell script to take a backup is one of the most simple ways. Usually, an array of directories or files for which the backup has to be taken are added to the script and then when the script is executed all those directories and files are archived in a tar file. The tar file/utility has also got capabilities to compress thus reducing the size of the resulted archive. Using tar utility, you can also restore data from an archive. Usually, backups are stored in a location other than the place of creation. Sometimes, an NFS remotely mounted is used to store archived data.
This script rotates through 7 backups - one for each day.
Explanaiton
$backup_files: this is a shell variable containing directories you would like to backup.
$day: a variable referring the day of the week (Monday, Tuesday, Wednesday, etc). This helps to create an archive file for each day of the week. You may use date utilities to accomplish this too.
$hostname: this variable refers to the short hostname of the system. You may use this to keep daily backups of multiple systems in the same directory.
$archive_file: the full archive filename.
$dest_dir: destination of the archive file. Before you execute the shell script, this directory has to be created and if you are targeting an NFS, then it must be mounted too.
status messages: if you want optional messages to be printed to the console using the echo utility, you may store those, messages in this variable.
tar czf $dest_dir/$archive_file $backup_files: the tar command used to create the archive file.
c: creates an archive.
z: filter the archive through the gzip utility compressing the archive.
f: output to an archive file. Otherwise, the tar output will be sent to STDOUT.
ls -lh $dest_dir: optional statement prints a -l long listing in -h human readable format of the destination directory. This is useful for a quick file size check of the archive file. This check should not replace testing the archive file.
Executing the script
Save the script you have created in a .sh file, say my_backup.sh and execute the file from prompt as follows
Automating with cron
Cron is used to schedule the execution of scripts. We will look at it in more detail later.
To enter the cron job editor
No comments:
Post a Comment