9 June 2008 - 1:39Simple Daily Backup Solution

As promised, here is the backup script I now have in place to take care of all my backup needs. I can’t take all of the credit, as the original idea came from this site. I just modified it to make it more robust and to better fit my needs.

To use, place all of the contents into a file anywhere on your system. I called mine, daily_backup.sh and placed it in the directory /root/scripts/. Once the script is in place, change the permissions to make it executable.

chmod 754 daily_backup.sh

Now modify the script changing the options and directories to suite your system and environment. To create the crontab entry, as root run the following.

crontab -e

Then add the following line, which will execute our script everynight at 3:01AM.

1 3 * * * /root/scripts/daily_backup.sh

Below is the script as it is setup on my system. Feel free to modify it to better suite your needs. As aways comments are welcome and appreciated.

#!/bin/bash
#
# Daily backup
#
 
########################################
## Backup location options
 
# Location of remote backups
BACKUPDIR="/filer/Backup";
# Set to no if not backing up to a remote mount
VERIFYMOUNT="yes";
# Local mount point of remote server
MOUNTNAME="/filer";
# Database username
DBUSER="sampleuser";
# Database password
DBPASS="samplepass";
# Day to run full backup
DOWFB="Sun";
 
########################################
## Backup data options
 
# Space separated directories to backup
DATADIRS="/var/www/html /etc";
# Space separated MySQL databases to backup
DBNAMES=( mysql mydb1 mydb2 );
 
########################################
########## End User Options ############
########################################
 
 
########################################
## Variables and checks used by script
 
LIST="/tmp/backlist_$$.txt";
set $(date)
 
if [ "$VERIFYMOUNT" == "yes" ]; then
  RESULT=`mount | grep $MOUNTNAME`;
 
  if [ ! "$RESULT" ]; then
    exit 1;
  fi
fi
 
########################################
## Create backup of data directories
 
if test "$1" = "$DOWFB" ; then
 
  #######################################
  ## Weekly full backup
 
  if [ ! -d "$BACKUPDIR/data" ]; then
    mkdir -p $BACKUPDIR/data
  fi
 
  tar cfz "$BACKUPDIR/data/data_full_$6-$2-$3.tgz" $DATADIRS
  rm -f $BACKUPDIR/data/data_diff*
 
else
 
  ########################################
  ## Daily incremental backup
 
  if [ ! -d "$BACKUPDIR/data" ]; then
    mkdir -p $BACKUPDIR/data
  fi
 
  find $DATADIRS -depth -type f ( -ctime -1 -o -mtime -1 ) -print > $LIST
  tar cfzT "$BACKUPDIR/data/data_diff_$6-$2-$3.tgz" "$LIST"
  rm -f "$LIST"
 
fi
 
########################################
## Create sql dump of databases
 
if [ ! -d "$BACKUPDIR/database" ]; then
  mkdir -p $BACKUPDIR/database
fi
 
for db in ${DBNAMES[@]}
do
  mysqldump -u $DBUSER --password=$DBPASS --opt $db > "$BACKUPDIR/database/$db-$6-$2-$3.sql"
  gzip "$BACKUPDIR/database/$db-$6-$2-$3.sql"
done
 
exit 0

No Comments | Tags: posts

Add a Comment