12 Feb

Alfresco restore from a cold backup

1359432616_backupIn the Alfresco wiki is described the backup and restore strategy but is not so clear how to do it in detail. In this post is shared a practical way to perform a restore of a backuped Alfresco in the way I like: a list of commands and tasks to execute… simpler to understand, to do and to test.

In this example is supposed that Alfresco is alfresco-logo.pnginstalled in ‘/opt/alfresco’ folder and the DBMS used is PostgreSQL. The restore strategy supposes that the backup is done as a “cold backup” described in this ‘Alfresco backup script‘ post.

Let’s start…

First of all proceed to stop alfresco.

service alfresco stop

Supposing the DBMS runs using the ‘postgres’ user, proceed as first task to restore the PostgreSQL database.

su - postgres
psql
DROP DATABASE alfresco;
CREATE DATABASE alfresco WITH OWNER alfresco;

Digit CTRL+d to exit from the shell.

psql -U alfresco -d alfresco
ALTER USER alfresco WITH PASSWORD 'alfresco';

Digit CTRL+d to exit from the shell. Supposing the DBMS backup is stored in the ‘alfresco_postgres.tar’ file conforming to the backup strategy described here, let’s go ahead with the restoring of the database.

pg_restore -d alfresco alfresco_postgres.tar
exit

Now it’s time to restore the ‘alf_data’ folder with documents and indexes. Also in this case is supposed that the files are stored in a ‘alfresco_data.tgz’ file conforming to the backup strategy described here.

cd /opt/alfresco
mv alf_data alf_data.old
tar zxvf alfresco_data.tgz

That’s all!
Now it’s time to start again Alfresco and use it normally.

service alfresco start
06 Feb

Alfresco backup script

1359432616_backupIn the Alfresco wiki is described the backup and restore strategy but is not so clear how is defined a script and how to do it more exactly. To be more precise the backup strategy could be a “cold backup” or a “hot backup”. In this post is shared a practical way to perform a “cold backup” in the way I like: a list of commands and tasks to execute… simpler to understand, to do and to test.

In this example is supposed that Alfresco is instalalfresco-logo.pngled in ‘/opt/alfresco’ folder and the DBMS used is PostgreSQL. The script is developed to have a 10 days backup history. If in your case is requested more or less than 10 days you can simply modify the ‘NUM_DUMP’ variable.

Let’s start…

cd /opt/alfresco
nano alfrescoBackup.sh

.

#
# Cold backup of Alfresco.
#

# Configuration.
ALF_HOME="/opt/alfresco"
NUM_DUMP=10
ALF_DATA_SUBDIR="alf_data"
DB_HOME="/opt/postgresql"
DB_TYPE="postgresql"

# Check
if [ -d "$1" ]; then

  TARGET_FOLDER="$1"
  CURRENT_FOLDER="$(pwd)"
  TIMESTAMP="$(date +%F_%H-%M)"
  CHECK=1

  # 1) Alfresco stop.
  service alfresco stop

  # 2) Backup of the database of Alfresco with vendor service.
  DB_DUMP=${TIMESTAMP}_alfresco_${DB_TYPE}.tar
  if [ "$DB_TYPE" = "postgresql" ]; then
    cd $TARGET_FOLDER
    $DB_HOME/bin/pg_dump -Ft -b alfresco > $TARGET_FOLDER/$DB_DUMP
    cd $CURRENT_FOLDER
    if [ ! -f "$TARGET_FOLDER/$DB_DUMP" ]; then
      CHECK=0
    fi
  else
    echo "Unknown database type '${DB_TYPE}'"
    CHECK=0
  fi

  # 3) Backup of the Alfresco's data folder.
  if [ "$CHECK" = 1 ]; then
    ALF_DUMP=${TIMESTAMP}_alfresco_data.tgz
    cd $ALF_HOME
    tar zcf $TARGET_FOLDER/$ALF_DUMP $ALF_DATA_SUBDIR
    cd $CURRENT_FOLDER
    if [ ! -f "$TARGET_FOLDER/$ALF_DUMP" ]; then
      CHECK=0
    fi
  fi

  # 4) Merge the two backups.
  if [ "$CHECK" = 1 ]; then
    BACKUP_FILE="${TIMESTAMP}_alfresco_dump.tgz"
    cd $TARGET_FOLDER
    tar zcf $BACKUP_FILE $ALF_DUMP $DB_DUMP
    if [ ! -f "$BACKUP_FILE" ]; then
      CHECK=0
    else
      rm $ALF_DUMP
      rm $DB_DUMP
    fi
    cd $CURRENT_FOLDER
  fi

  # 5) Alfresco start.
  service alfresco start

  # 6) Delete dump older than a certains number of days.
  if [ "$CHECK" = 1 ]; then
    find $TARGET_FOLDER/*.tgz -type f -mtime +${NUM_DUMP} -exec rm {} ;
  fi

else
  echo "usage: $0 [targetPath]"
fi

Exit with CTRL+X and confirm saving. Let’s go ahead defining permits and preparing the repository folder. Talking about the repository folder here is presented an example but in practical cases is suggested to mount an external file system used specifically for backup purpose.

chmod uga+x /opt/alfresco/alfrescoBackup.sh
mkdir /opt/alfresco/backup

Now everything is set and it’s time to activate the crontab to perform automatic backup, probably during the night time. Please, observe that the user used to run Alfresco is named ‘alfresco’ (do not run it as root) and the crontab is performed as ‘alfresco’ user with ‘-u alfresco’.

sudo crontab -u alfresco -e

.

00 01 * * * /opt/alfresco/alfrescoBackup.sh /opt/alfresco/backup

In this example the cold backup of Alfresco is performed at 01:00 am. To read more about crontab see here.