Difference between revisions of "Backup scripts"
From OpenKM Documentation
(→Remote backup with rsync and rotation) |
(→Remote backup with rsync) |
||
Line 26: | Line 26: | ||
HOST=$(uname -n) | HOST=$(uname -n) | ||
FILES="/home/openkm" | FILES="/home/openkm" | ||
− | BACKUP_DIR="/ | + | BACKUP_DIR="/mnt/backup" |
## END CONFIG ## | ## END CONFIG ## | ||
echo -e "### BEGIN: $(date +"%x %X") ###\n" | echo -e "### BEGIN: $(date +"%x %X") ###\n" |
Revision as of 12:41, 2 December 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
Contents
Remote backup with rsync
#!/bin/bash
#
## BEGIN CONFIG ##
HOST=$(uname -n)
FILES="/home/openkm"
BACKUP_DIR="/mnt/backup"
## 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
rsync -apzhR --stats --delete --exclude=*~ --delete-excluded $FILES backup@server:$BACKUP_DIR/$HOST
# Start JBoss
/etc/init.d/jboss start
echo -e "\n### END: $(date +"%x %X") ###"
Remote backup with rsync and rotation
#!/bin/bash
#
## BEGIN CONFIG ##
HOST=$(uname -n)
FILES="/home/openkm"
BACKUP_DIR="/mnt/backup"
## END CONFIG ##
echo -e "### BEGIN: $(date +"%x %X") ###\n"
# Stop JBoss
/etc/init.d/jboss stop
# 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="$BACKUP_DIR/$HOST/backup.1" \
$FILES backup@server:$BACKUP_DIR/$HOST/backup.0
# Start JBoss
/etc/init.d/jboss start
echo -e "\n### END: $(date +"%x %X") ###"
Local backup with rsync
#!/bin/bash
#
## BEGIN CONFIG ##
HOST=$(uname -n)
FILES="/home/openkm"
BACKUP_DIR="/media/backup"
## END CONFIG ##
echo -e "### BEGIN: $(date +"%x %X") ###\n"
# Stop JBoss
/etc/init.d/jboss stop
# Calculate snapshot
LAST_SNAPSHOT=`ls -ltr $BACKUP_DIR | tail -1 | awk {'print $8'} | cut -d . -f 2`
NEW_SNAPSHOT=$((LAST_SNAPSHOT+1))
# Copy to backup server
rsync -apzhR --stats --delete --exclude=*~ --exclude="$JBOSS_HOME/cache" --delete-excluded \
--link-dest="$BACKUP_DIR/$HOST/backup.$LAST_SNAPSHOT" $FILES "$BACKUP_DIR/$HOST/backup.$NEW_SNAPSHOT"
# 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 "=================================";
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
# Backup and purge old backups
rdiff-backup --remove-older-than 30B backup@server:/home/backup/$HOST
rdiff-backup -v 3 --print-statistics --exclude /media \
--include /etc --include /root --include /home \
--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
USB disk mount point can be defined in /etc/fstab as:
/dev/sdb1 /mnt/backup ext4 defaults 0 0
#!/bin/bash
#
## BEGIN CONFIG ##
HOST=$(uname -n)
MYSQL_PASS=""
JBOSS_HOME="/home/openkm/jboss-4.2.3.GA"
DATABASE_EXP="/home/openkm/db"
BACKUP_DIR="/mnt/backup"
FILES="$JBOSS_HOME $DATABASE_EXP" # Only for rsync
## END CONFIG ##
if [ $(id -u) != 0 ]; then echo "You should run this script as root"; exit; fi
echo -e "### BEGIN: $(date +"%x %X") ###\n"
rm -rf $DATABASE_EXP
mkdir -p $DATABASE_EXP
# Mount disk
if mount | grep "$BACKUP_DIR type" > /dev/null; then
echo "$BACKUP_DIR already mounted";
else
mount $BACKUP_DIR;
fi
# Stop JBoss
/etc/init.d/jboss stop
# 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
# Calculate snapshot
#LAST_SNAPSHOT=`ls -ldtr $BACKUP_DIR/$HOST/backup.* | tail -1 | awk {'print $8'} | cut -d . -f 2`
#NEW_SNAPSHOT=$((LAST_SNAPSHOT+1))
# Copy to backup server
#rsync -apzhR --stats --delete --exclude=*~ --exclude="$JBOSS_HOME/cache" --delete-excluded \
#--link-dest="$BACKUP_DIR/$HOST/backup.$LAST_SNAPSHOT" $FILES "$BACKUP_DIR/$HOST/backup.$NEW_SNAPSHOT"
# Backup and purge old backups
rdiff-backup --remove-older-than 30B $BACKUP_DIR/$HOST
rdiff-backup -v 3 --print-statistics --exclude /media --exclude /mnt \
--include $JBOSS_HOME --include $DATABASE_EXP \
--exclude '**' / $BACKUP_DIR/$HOST
# 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
Be sure to have the last JBoss startup script, otherwise use this piece of code to ensure that JBoss is stopped before backuping the files:
while [ "$(ps -ef | grep java | grep jboss | wc -l)" -gt "0" ]; do
sleep 5; echo ".";
done