In 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 instal
led 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.