Difference between revisions of "Backup scripts"

From OpenKM Documentation
Jump to: navigation, search
(Remote backup)
Line 37: Line 37:
 
# Copy to backup server
 
# Copy to backup server
 
ssh backup@server "cd $HOST; rm -rf backup.3; mv backup.2 backup.3; mv backup.1 backup.2; mv backup.0 backup.1"
 
ssh backup@server "cd $HOST; rm -rf backup.3; mv backup.2 backup.3; mv backup.1 backup.2; mv backup.0 backup.1"
rsync -apzhR --stats --delete --exclude=*~ --delete-excluded --link-dest="/home/backup/$HOST/backup.1" $FILES backup@server:/home/backup/
+
rsync -apzhR --stats --delete --exclude=*~ --delete-excluded --link-dest="/home/backup/$HOST/backup.1" $FILES backup@server:/home/backup/ $HOST/backup.0
$HOST/backup.0
 
  
 
# Start JBoss
 
# Start JBoss
Line 51: Line 50:
 
## BEGIN CONFIG ##
 
## BEGIN CONFIG ##
 
HOST=$(uname -n)
 
HOST=$(uname -n)
MYSQL_PASS="*****"
+
FILES="/home/openkm"
DATABASE_EXP="/home/openkm/db"
 
BACKUP_DIR="/media/usbbackup"
 
 
## END CONFIG ##
 
## END CONFIG ##
 
if [ $(id -u) != 0 ]; then echo "You should run this script as root"; exit; fi
 
 
# Mount disk
 
mount $BACKUP_DIR
 
 
 
echo -e "### BEGIN: $(date +"%x %X") ###\n"
 
echo -e "### BEGIN: $(date +"%x %X") ###\n"
rm -rf $DATABASE_EXP
 
mkdir -p $DATABASE_EXP
 
  
 
# Stop JBoss
 
# Stop JBoss
 
/etc/init.d/jboss stop
 
/etc/init.d/jboss stop
 +
while [ "$(ps -ef | grep java | grep jboss | wc -l)" -gt "0" ]; do
 +
  sleep 5; echo ".";
 +
done
  
# Clean logs
 
echo "Clean JBoss temporal files."
 
JBOSS_HOME="/home/openkm/jboss-4.2.3.GA"
 
#rm -rf $JBOSS_HOME/server/default/log
 
#rm -rf $JBOSS_HOME/server/default/tmp
 
#rm -rf $JBOSS_HOME/server/default/work
 
 
# Backup de MySQL
 
if [ -n "$MYSQL_PASS" ]; then
 
  MYSQL_DBS=$(mysqlshow -h localhost -u root -p$MYSQL_PASS | awk '(NR > 2) && (/[a-zA-Z0-9]+[ ]+[|]/) && ($2 != "mysql") && ($2 != "test") && ($2 != "information_schema") { print $2 }');
 
 
  for DB in $MYSQL_DBS ; do
 
    echo "* Backuping MySQL data from $DB...";
 
    mysqldump -h localhost -u root -p$MYSQL_PASS $DB > $DATABASE_EXP/mysql_$DB.sql
 
  done
 
  echo "-------------------------------------";
 
fi
 
 
 
# Rotate and copy files
 
# Rotate and copy files
rdiff-backup --remove-older-than 30B $BACKUP_DIR/$HOST
+
rdiff-backup --remove-older-than 30B backup@server:/home/backup/$HOST
 
rdiff-backup -v 3 --print-statistics \
 
rdiff-backup -v 3 --print-statistics \
 
   --exclude /etc/webmin/sysstats --exclude /media \
 
   --exclude /etc/webmin/sysstats --exclude /media \
 
   --include /etc --include /root --include /home --include /var/www \
 
   --include /etc --include /root --include /home --include /var/www \
   --exclude '**' / $BACKUP_DIR/$HOST
+
   --exclude '**' / backup@server:/home/backup/$HOST
  
 
# Start JBoss
 
# Start JBoss
 
/etc/init.d/jboss start
 
/etc/init.d/jboss start
 
echo -e "\n### END: $(date +"%x %X") ###"
 
echo -e "\n### END: $(date +"%x %X") ###"
 
# Status
 
echo "=================================";
 
du -hs $BACKUP_DIR
 
echo "*********************************";
 
du -hs --time $BACKUP_DIR/*
 
echo "=================================";
 
df -h | grep "$BACKUP_DIR"
 
echo "=================================";
 
 
# Umount disk
 
sync
 
umount $BACKUP_DIR
 
 
</source>
 
</source>
  

Revision as of 19:01, 22 November 2010

These backup scripts use rsync to minimize network load and creates incremental backups, preserving last four backups. For more info, read http://www.mikerubel.org/computers/rsync_snapshots/.

To install the cron job, run:

$ sudo crontab -e

And add these lines according to your personal configuration:

MAILTO=nomail@openkm.com
@weekly /root/backup.sh | tee -a /root/backup.log

Or, if you want to separate log reports by date:

MAILTO=nomail@openkm.com
@weekly /root/backup.sh | tee /root/backup.$(date +\%Y.\%m.\%d_\%H.\%M.\%S).log

Remote backup with rsync

#!/bin/bash
#
## BEGIN CONFIG ##
HOST=$(uname -n)
FILES="/home/openkm"
## END CONFIG ##
echo -e "### BEGIN: $(date +"%x %X") ###\n"

# Stop JBoss
/etc/init.d/jboss stop
while [ "$(ps -ef | grep java | grep jboss | wc -l)" -gt "0" ]; do
  sleep 5; echo ".";
done

# Copy to backup server
ssh backup@server "cd $HOST; rm -rf backup.3; mv backup.2 backup.3; mv backup.1 backup.2; mv backup.0 backup.1"
rsync -apzhR --stats --delete --exclude=*~ --delete-excluded --link-dest="/home/backup/$HOST/backup.1" $FILES backup@server:/home/backup/ $HOST/backup.0

# Start JBoss
/etc/init.d/jboss start
echo -e "\n### END: $(date +"%x %X") ###"

Remote backup with rdiff-backup

#!/bin/bash
#
## BEGIN CONFIG ##
HOST=$(uname -n)
FILES="/home/openkm"
## END CONFIG ##
echo -e "### BEGIN: $(date +"%x %X") ###\n"

# Stop JBoss
/etc/init.d/jboss stop
while [ "$(ps -ef | grep java | grep jboss | wc -l)" -gt "0" ]; do
  sleep 5; echo ".";
done

# Rotate and copy files
rdiff-backup --remove-older-than 30B backup@server:/home/backup/$HOST
rdiff-backup -v 3 --print-statistics \
  --exclude /etc/webmin/sysstats --exclude /media \
  --include /etc --include /root --include /home --include /var/www \
  --exclude '**' / backup@server:/home/backup/$HOST

# Start JBoss
/etc/init.d/jboss start
echo -e "\n### END: $(date +"%x %X") ###"

Local backup to usb disk

#!/bin/bash
#
## BEGIN CONFIG ##
MYSQL_PASS=""
JBOSS_HOME="/home/openkm/jboss-4.2.3.GA"
DATABASE_EXP="/home/openkm/db"
BACKUP_DIR="/media/Backup"
FILES="$JBOSS_HOME $DATABASE_EXP"
## END CONFIG ##

if [ $(id -u) != 0 ]; then echo "You should run this script as root"; exit; fi

# Mount disk
mount $BACKUP_DIR

echo -e "### BEGIN: $(date +"%x %X") ###\n"
rm -rf $DATABASE_EXP
mkdir -p $DATABASE_EXP

# Stop JBoss
/etc/init.d/jboss stop

while [ "$(ps -ef | grep java | grep jboss | wc -l)" -gt "0" ]; do
  sleep 5; echo ".";
done

# Clean logs
echo "Clean JBoss temporal files."
rm -rf $JBOSS_HOME/server/default/log
rm -rf $JBOSS_HOME/server/default/tmp
rm -rf $JBOSS_HOME/server/default/work

# Backup de MySQL
if [ -n "$MYSQL_PASS" ]; then
  MYSQL_DBS=$(mysqlshow -h localhost -u root -p$MYSQL_PASS | awk '(NR > 2) && (/[a-zA-Z0-9]+[ ]+[|]/) && ($2 != "mysql") && ($2 != "test") && ($2 != "information_schema") { print $2 }');
  
  for DB in $MYSQL_DBS ; do
    echo "* Backuping MySQL data from $DB...";
    mysqldump -h localhost -u root -p$MYSQL_PASS $DB > $DATABASE_EXP/mysql_$DB.sql
  done
  echo "-------------------------------------";
fi

# Rotate and copy files
cd $BACKUP_DIR; rm -rf mv backup.3 backup.4; mv backup.2 backup.3; mv backup.1 backup.2; mv backup.0 backup.1; cd
rsync -apzhR --stats --delete --exclude=*~ --exclude="$JBOSS_HOME/cache" --delete-excluded --link-dest="$BACKUP_DIR/backup.1" $FILES "$BACKUP_DIR/backup.0"

# Start JBoss
/etc/init.d/jboss start
echo -e "\n### END: $(date +"%x %X") ###"

# Status
echo "=================================";
du -hs $BACKUP_DIR
echo "*********************************";
du -hs --time $BACKUP_DIR/*
echo "=================================";
df -h | grep "$BACKUP_DIR"
echo "=================================";

# Umount disk
sync
umount $BACKUP_DIR