Difference between revisions of "Backup with rsync"

From OpenKM Documentation
Jump to: navigation, search
Line 2: Line 2:
  
 
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.
 
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.
 
rsync need SSH access to work, but you can also use it with an FTP server if you mount it locally with '''curlftpfs''':
 
* Install curlftpfs and rsync
 
  $ sudo apt-get install curlftpfs rsync
 
 
* Create mountpoint
 
  $ mkdir /path/to/mount
 
 
* Mount remote directory
 
  $ curlftpfs ftp.example.com /path/to/mount
 
 
* Create local directory
 
  $ mkdir /local/path
 
 
* Rsync mounted directory to local directory
 
  $ rsync -r -t -v --progress --bwlimit=500 /path/to/mount/ /local/path/
 
  
 
== Remote backup with rsync ==
 
== Remote backup with rsync ==
Line 209: Line 193:
 
echo "=================================";
 
echo "=================================";
 
</source>
 
</source>
 +
 +
== rsyn and FTP ==
 +
rsync need SSH access to work, but you can also use it with an FTP server if you mount it locally with '''curlftpfs''':
 +
* Install curlftpfs and rsync
 +
  $ sudo apt-get install curlftpfs rsync
 +
 +
* Create mountpoint
 +
  $ mkdir /path/to/mount
 +
 +
* Mount remote directory
 +
  $ curlftpfs ftp.example.com /path/to/mount
 +
 +
* Create local directory
 +
  $ mkdir /local/path
 +
 +
* Rsync mounted directory to local directory
 +
  $ rsync -r -t -v --progress --bwlimit=500 /path/to/mount/ /local/path/
  
 
[[Category: Installation Guide]]
 
[[Category: Installation Guide]]

Revision as of 15:11, 3 August 2013

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/logs/*
#rm -rf $TOMCAT_HOME/temp/*
#rm -rf $TOMCAT_HOME/work/Catalina/localhost

# 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_DB="okmdb"
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
echo "* Backuping PostgreSQL data from $OPENKM_DB..."
su postgres -c "pg_dump -Fc -b $OPENKM_DB" > $DATABASE_EXP/pg_$OPENKM_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 "=================================";

rsyn and FTP

rsync need SSH access to work, but you can also use it with an FTP server if you mount it locally with curlftpfs:

  • Install curlftpfs and rsync
 $ sudo apt-get install curlftpfs rsync
  • Create mountpoint
 $ mkdir /path/to/mount
  • Mount remote directory
 $ curlftpfs ftp.example.com /path/to/mount
  • Create local directory
 $ mkdir /local/path
  • Rsync mounted directory to local directory
 $ rsync -r -t -v --progress --bwlimit=500 /path/to/mount/ /local/path/