Difference between revisions of "Backup with rsync"

From OpenKM Documentation
Jump to: navigation, search
m (Local backup with rsync and rotation)
(Local backup with rsync and MySQL)
Line 59: Line 59:
 
HOST=$(uname -n)
 
HOST=$(uname -n)
 
MYSQL_PASS=""
 
MYSQL_PASS=""
 +
OPENKM_DB="okmdb"
 
OPENKM_HOME="/home/openkm"
 
OPENKM_HOME="/home/openkm"
JBOSS_HOME="$OPENKM_HOME/jboss-4.2.3.GA"
+
TOMCAT_HOME="$OPENKM_HOME/tomcat-7.0.27"
 
DATABASE_EXP="$OPENKM_HOME/db"
 
DATABASE_EXP="$OPENKM_HOME/db"
 
BACKUP_DIR="/mnt/backup"
 
BACKUP_DIR="/mnt/backup"
Line 71: Line 72:
 
mkdir -p $DATABASE_EXP
 
mkdir -p $DATABASE_EXP
  
# Stop JBoss
+
# Stop Tomcat
/etc/init.d/jboss stop
+
/etc/init.d/tomcat stop
  
 
# Clean logs
 
# Clean logs
echo "Clean JBoss temporal files."
+
#echo "Clean tomcat temporal files."
# rm -rf $JBOSS_HOME/server/default/log
+
#rm -rf $TOMCAT_HOME/temp/hbm
rm -rf $JBOSS_HOME/server/default/tmp
+
#rm -rf $TOMCAT_HOME/temp/okm
rm -rf $JBOSS_HOME/server/default/work
+
#rm -rf $TOMCAT_HOME/work/Catalina
rm -rf $JBOSS_HOME/server/default/data/wsdl
 
  
 
# Backup de MySQL
 
# Backup de MySQL
if [ -n "$MYSQL_PASS" ]; then
+
f [ -n "$MYSQL_PASS" ]; then
   MYSQL_DBS=$(mysqlshow -h localhost -u root -p$MYSQL_PASS | awk '(NR > 2) && (/[a-zA-Z0-9]+[ ]+[|]/) { print $2 }');
+
   echo "* Backuping MySQL data from $OPENKM_DB...";
 
+
    mysqldump -h localhost -u root -p$MYSQL_PASS $OPENKM_DB > $DATABASE_EXP/mysql_$OPENKM_DB.sql
  for DB in $MYSQL_DBS; do
 
    if [[ $DB != "mysql" && $DB != "test" && $DB != "information_schema" && $DB != "performance_schema" ]]; then
 
      echo "* Backuping MySQL data from $DB..."
 
      mysqldump -h localhost -u root -p$MYSQL_PASS $DB > $DATABASE_EXP/mysql_$DB.sql
 
    fi
 
  done
 
 
   echo "-------------------------------------";
 
   echo "-------------------------------------";
 
fi
 
fi
Line 97: Line 91:
 
rsync -apzhR --stats --delete --exclude=*~ --delete-excluded $OPENKM_HOME $BACKUP_DIR/$HOST
 
rsync -apzhR --stats --delete --exclude=*~ --delete-excluded $OPENKM_HOME $BACKUP_DIR/$HOST
  
# Start JBoss
+
# Start Tomcat
/etc/init.d/jboss start
+
/etc/init.d/tomcat start
 
echo -e "\n### END: $(date +"%x %X") ###"
 
echo -e "\n### END: $(date +"%x %X") ###"
 
</source>
 
</source>

Revision as of 14:02, 14 November 2012

rsync is a software application and network protocol for Unix, Linux and Windows systems which synchronizes files and directories from one location to another while minimizing data transfer using delta encoding when appropriate. An important feature of rsync not found in most similar programs/protocols is that the mirroring takes place with only one transmission in each direction. rsync can copy or display directory contents and copy files, optionally using compression and recursion.

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 and MySQL

#!/bin/bash
#
## BEGIN CONFIG ##
HOST=$(uname -n)
MYSQL_PASS=""
OPENKM_DB="okmdb"
OPENKM_HOME="/home/openkm"
TOMCAT_HOME="$OPENKM_HOME/tomcat-7.0.27"
DATABASE_EXP="$OPENKM_HOME/db"
BACKUP_DIR="/mnt/backup"
## 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

# Stop Tomcat
/etc/init.d/tomcat stop

# Clean logs
#echo "Clean tomcat temporal files."
#rm -rf $TOMCAT_HOME/temp/hbm
#rm -rf $TOMCAT_HOME/temp/okm
#rm -rf $TOMCAT_HOME/work/Catalina

# Backup de MySQL
f [ -n "$MYSQL_PASS" ]; then
  echo "* Backuping MySQL data from $OPENKM_DB...";
    mysqldump -h localhost -u root -p$MYSQL_PASS $OPENKM_DB > $DATABASE_EXP/mysql_$OPENKM_DB.sql
  echo "-------------------------------------";
fi

# Create backup
rsync -apzhR --stats --delete --exclude=*~ --delete-excluded $OPENKM_HOME $BACKUP_DIR/$HOST

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

Local 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

# 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 rsync (Tomcat - PostgreSQL)

#!/bin/bash
#
## BEGIN CONFIG ##
HOST=$(uname -n)
RSYNC_OPTS="-ahPR --stats --delete --exclude=*~ --delete-excluded"
OPENKM_HOME="/home/openkm"
TOMCAT_HOME="$OPENKM_HOME/tomcat-7.0.27"
DATABASE_EXP="$OPENKM_HOME/db"
BACKUP_SRV="user@server.com"
BACKUP_DIR="/mnt/backup"
## 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

# Stop Tomcat
/etc/init.d/tomcat stop

# Clean logs
#echo "Clean Tomcat temporal files."
#rm -rf $TOMCAT_HOME/logs/*
#rm -rf $TOMCAT_HOME/temp/*
#rm -rf $TOMCAT_HOME/work/Catalina/localhost

# Backup de PostgreSQL
POSTGRESQL_DBS=$(su postgres -c "psql -l" | awk '(NR > 2) && (/[a-zA-Z0-9]+[ ]+[|]/) && ($1 !~ /template[0-1]/) && ($1 != "postgres") { print $1 }');

for DB in $POSTGRESQL_DBS ; do
  echo "* Backuping PostgreSQL data from $DB..."
  su postgres -c "pg_dump -Fc -b $DB" > $DATABASE_EXP/pg_$DB.dmp
done

# Databases optimizations
su postgres -c "vacuumdb -a -z" > /dev/null
su postgres -c "reindexdb -a -q" 2> /dev/null

# Calculate snapshot
NEW_SNAPSHOT=$(date "+%Y-%m-%dT%H:%M:%S")
LAST_SNAPSHOT=$(ssh $BACKUP_SRV ls -tr $BACKUP_DIR/$HOST | tail -1)

# Backup and rotate
if [ $LAST_SNAPSHOT ]; then
  echo "Incremental backup $NEW_SNAPSHOT based on $LAST_SNAPSHOT... "
  rsync $RSYNC_OPTS --link-dest="$BACKUP_DIR/$HOST/$LAST_SNAPSHOT" "$OPENKM_HOME" "$BACKUP_SRV:$BACKUP_DIR/$HOST/$NEW_SNAPSHOT"
else
  echo "Initial full backup $NEW_SNAPSHOT..."
  rsync $RSYNC_OPTS "$OPENKM_HOME" "$BACKUP_SRV:$BACKUP_DIR/$HOST/$NEW_SNAPSHOT"
fi

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

# Status
echo "=================================";
ssh $BACKUP_SRV ls -l "$BACKUP_DIR/$HOST"
echo "=================================";