Difference between revisions of "Backup with rsync"

From OpenKM Documentation
Jump to: navigation, search
m (Remote backup with rsync (incremental + MySQL))
(Remote backup with rsync (incremental with "backup-dir" + MySQL))
 
(7 intermediate revisions by the same user not shown)
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.
 +
 +
{{Advice|See also [[Backup restoring]].}}
  
 
== Remote backup with rsync ==
 
== Remote backup with rsync ==
Line 52: Line 54:
 
</source>
 
</source>
  
== Remote backup with rsync (incremental + MySQL) ==
+
== Remote backup with rsync (incremental with "link-dest" + MySQL) ==
 
<source lang="bash">
 
<source lang="bash">
 
#!/bin/bash
 
#!/bin/bash
Line 87: Line 89:
  
 
# Calculate snapshot
 
# Calculate snapshot
LAST_SNAPSHOT=`ssh $BACKUP_SRV "cd $BACKUP_DIR && stat --format='%n' *"`
+
LAST_SNAPSHOT=`ssh $BACKUP_SRV "cd $BACKUP_DIR && stat --format='%n' * | tail -1"`
 
NEW_SNAPSHOT=`date +%Y-%m-%d`
 
NEW_SNAPSHOT=`date +%Y-%m-%d`
echo -e "### RSYNC - BEGIN : $(date +"%x %X") ###\n"
+
echo -e "\n### RSYNC - BEGIN : $(date +"%x %X") ###\n"
 
echo "* Last Snapshot: $LAST_SNAPSHOT"
 
echo "* Last Snapshot: $LAST_SNAPSHOT"
 
echo "* New Snapshot: $NEW_SNAPSHOT"
 
echo "* New Snapshot: $NEW_SNAPSHOT"
Line 98: Line 100:
 
   rsync $RSYNC_OPTS --link-dest="$BACKUP_DIR/$LAST_SNAPSHOT" $OPENKM_HOME/ "$BACKUP_SRV:$BACKUP_DIR/$NEW_SNAPSHOT"
 
   rsync $RSYNC_OPTS --link-dest="$BACKUP_DIR/$LAST_SNAPSHOT" $OPENKM_HOME/ "$BACKUP_SRV:$BACKUP_DIR/$NEW_SNAPSHOT"
 
fi
 
fi
 +
 +
# Start Tomcat
 +
/etc/init.d/tomcat start
 +
echo -e "\n### END: $(date +"%x %X") ###"
 +
</source>
 +
 +
== Remote backup with rsync (incremental with "backup-dir" + MySQL) ==
 +
<source lang="bash">
 +
#!/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"
 +
RSYNC_OPTS="-ahR --partial --stats --delete --exclude=*~ --exclude=tomcat-7.0.27/temp/* --exclude=tomcat-7.0.27/work/* --exclude=tomcat-7.0.27/logs/* --delete-excluded"
 +
BACKUP_SRV="backup@server"
 +
BACKUP_DIR="/home/backup/$HOST"
 +
## 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
 +
 +
# Backup de MySQL
 +
if [ -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
 +
fi
 +
 +
# Calculate snapshot
 +
NEW_SNAPSHOT=`date +%Y-%m-%d`
 +
echo -e "\n### RSYNC - BEGIN : $(date +"%x %X") ###\n"
 +
echo "* New Snapshot: $NEW_SNAPSHOT"
 +
rsync $RSYNC_OPTS --backup --backup-dir="$BACKUP_DIR/INCREMENTAL/$NEW_SNAPSHOT" $OPENKM_HOME/ "$BACKUP_SRV:$BACKUP_DIR/LAST_BACKUP"
  
 
# Start Tomcat
 
# Start Tomcat

Latest revision as of 09:38, 14 November 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.


Nota idea.png See also Backup restoring.

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") ###"

Remote backup with rsync (incremental with "link-dest" + 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"
RSYNC_OPTS="-ahR --partial --stats --delete --exclude=*~ --exclude=tomcat-7.0.27/temp/* --exclude=tomcat-7.0.27/work/* --exclude=tomcat-7.0.27/logs/* --delete-excluded"
BACKUP_SRV="backup@server"
BACKUP_DIR="/home/backup/$HOST"
## 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

# Backup de MySQL
if [ -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
fi

# Create missing backup folder
ssh $BACKUP_SRV "mkdir -p $BACKUP_DIR"

# Calculate snapshot
LAST_SNAPSHOT=`ssh $BACKUP_SRV "cd $BACKUP_DIR && stat --format='%n' * | tail -1"`
NEW_SNAPSHOT=`date +%Y-%m-%d`
echo -e "\n### RSYNC - BEGIN : $(date +"%x %X") ###\n"
echo "* Last Snapshot: $LAST_SNAPSHOT"
echo "* New Snapshot: $NEW_SNAPSHOT"

if [ ! $LAST_SNAPSHOT ]; then
  rsync $RSYNC_OPTS $OPENKM_HOME/ "$BACKUP_SRV:$BACKUP_DIR/$NEW_SNAPSHOT"
else 
  rsync $RSYNC_OPTS --link-dest="$BACKUP_DIR/$LAST_SNAPSHOT" $OPENKM_HOME/ "$BACKUP_SRV:$BACKUP_DIR/$NEW_SNAPSHOT"
fi

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

Remote backup with rsync (incremental with "backup-dir" + 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"
RSYNC_OPTS="-ahR --partial --stats --delete --exclude=*~ --exclude=tomcat-7.0.27/temp/* --exclude=tomcat-7.0.27/work/* --exclude=tomcat-7.0.27/logs/* --delete-excluded"
BACKUP_SRV="backup@server"
BACKUP_DIR="/home/backup/$HOST"
## 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

# Backup de MySQL
if [ -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
fi

# Calculate snapshot
NEW_SNAPSHOT=`date +%Y-%m-%d`
echo -e "\n### RSYNC - BEGIN : $(date +"%x %X") ###\n"
echo "* New Snapshot: $NEW_SNAPSHOT"
rsync $RSYNC_OPTS --backup --backup-dir="$BACKUP_DIR/INCREMENTAL/$NEW_SNAPSHOT" $OPENKM_HOME/ "$BACKUP_SRV:$BACKUP_DIR/LAST_BACKUP"

# Start Tomcat
/etc/init.d/tomcat 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
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 "=================================";

rsync 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:


Nota clasica.png Backup performance may degrade using this method.

  • 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/