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
- 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.
#!/bin/sh #################################### # # Backup to NFS mount script. # #################################### # What to backup. backup_files="/home /var/spool/mail /etc /root /boot /opt" # Where to backup to. dest_dir="/mnt/backup" # Create archive filename. day=$(date +%A) hostname=$(hostname -s) archive_file="$hostname-$day.tgz" # Print start status message. echo "Backing up $backup_files to $dest_dir/$archive_file" date echo # Backup the files using tar. tar czf $dest_dir/$archive_file $backup_files # Print end status message. echo echo "Backup finished" date # Long listing of files in $dest to check file sizes. ls -lh $dest_dir
$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 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
sudo bash my_backup.sh
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
To run the backup script every day of every month of every year, at midnight
# m h dom mon dow command 0 0 * * * bash /usr/local/bin/backup.sh
Use tar to test the integrity of an archive, or to extract its contents.
To list the contents of the archive
tar -tzvf /mnt/backup/host-Monday.tgz
To extract a file from the archive
tar -xzvf /mnt/backup/host-Monday.tgz - C /tmp etc/hosts
To extract a file from the archive
tar -xzvf /mnt/backup/host-Monday.tg
Specialised backup utilities(e.g. Bacula)
Instead of taking backup with shell script or corn, you may use a specialized backup utility like Bacula. Now we will see how you can install, configure and execute Bacula to take backup and restore.
Before we install Bacula, it would be better to understand different components/services of Bacula.
Bacula Director: This may be referred as workhorse component of Bacula. it is a service which is used to control backup, restore, verify, and archive operations.
Bacula Console: This is an application using which communication with the Bacula Director is established. It has got three versions - Text based command line version, Gnome based GTK+ Graphical User Interface (GUI) interface and wxWidgets GUI interface.
Bacula File: It is also called as Bacula Client program. Using this application, file attributes and data when requested by the Director is provided. This application is installed on machines to be backed up. This application is Operating System specific.
Bacula Storage: The Bacula Storage services consist of the software programs that perform the storage and recovery of the file attributes and data to the physical backup media or volumes.
Bacula Catalog: The Bacula catalog service is responsible for maintaining the file indexes and volume databases for all files backed up, enabling quick location and restoration of archived files. The Catalog supports three different databases MySQL, PostgreSQL, and SQLite.
Bacula Monitor: The Bacula monitor service allows the monitoring of the Director, File daemons, and Storage daemons. Currently, the Monitor is only available as a GTK+ GUI application.
Start your Bacula installation with the following command
sudo apt-get install bacula
New Exercises:R Programming