Wednesday, August 20, 2008

bash script to keep incremental backup

Add following entry to /etc/crontab

20 4 * * * root /bin/bash /path/to/sync.sh



#!/bin/bash
# Copyright (C) 2008 Nayanajit Mahendra Laxaman mail: nmlaxaman@gmail.com

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

### help ###
#Before using this script you have to enable password less ssh/rsync login to remote host which you want to synchronize the data.
#1. ssh user@remote_host
#2. ssh-keygen -t rsa
#3. Don not give any phrase as password, leave it blank when asked
#4. scp /home/user/.ssh/id_rsa.pub user@localhost
#5. get back to localhost and;
#6. cat id_rsa.pub >> /home/user/.ssh/authorized_keys
#now you can rsyn without entering password
###########

#backing up database
BACKUPDIR=/root/backup_system
LOG=lms_backup_`date +%m%d%Y`.log

MOODLEWEB=/var/www/moodle_ug_2008
MOODLEDATA=/var/moodledata_ug_2008
LOGS=/var/log

DBUSERNAME=root
DBPASSWORD=letmeinliant
DATABASE=moodledb_ug_2008
DUMPOPTS="--add-drop-table"
DUMPDB=moodledb_ug_2008.sql

#SYNCHOST=root@ICTer.org
SYNCHOST=root@192.248.16.114
SYNCDIR=/usr/local/backups_lms_ug/sync/
SYNCOPTS="-avz --delete --force --ignore-errors --whole-file"

cd $BACKUPDIR
#dump database
echo "Start mysqldump at `date`" >> $LOG
mysqldump -u$DBUSERNAME -p$DBPASSWORD $DUMPOPTS $DATABASE > $DUMPDB 2>>$LOG
echo "End mysqldump at `date`" >> $LOG

#syncing web and data directories
echo "Start rsync web and data directories at `date`" >> $LOG
rsync $SYNCOPTS $MOODLEWEB $MOODLEDATA $LOGS $SYNCHOST:$SYNCDIR >>$LOG 2>>$LOG
echo "End rsync web and data directories at `date`" >> $LOG

#syncing database and log
echo "Start rsync database and log at `date`" >> $LOG
rsync $SYNCOPTS $DUMPDB $LOG $SYNCHOST:$SYNCDIR >>$LOG 2>>$LOG
echo "Start rsync database and log at `date`" >> $LOG

Blogged with the Flock Browser

No comments:

Post a Comment