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.

51 thoughts on “Alfresco backup script

  1. Ottimo articolo, complimenti. Se può interessare a qualcuno, aggiungo che per adattare lo script all’installazione in bundle ho verificato che è sufficiente aggiungere tra i punti 1 e 2 questa parte di codice:
    # 1.1)
    /opt/alfresco/postgresql/scripts/ctl.sh start

    e poi creare creare nella directory home dell’utente che esegue lo script il file “.pgpass” con permessi 0600 e con il seguente contenuto:
    *:*:*:postgres:Password_db_Alfresco

    A me funziona alla grande.
    Ciao!

    • Thank you C.U.,
      Grazie C.U.,
      For the non-italian people, C.U. suggest to add this command between the steps 1 and 2:
      # 1.1)
      /opt/alfresco/postgresql/scripts/ctl.sh start
      and even create a “.pgpass” file with 0600 permits and this content: *:*:*:postgres:Password_db_Alfresco

    • i’ve seen you made another article for this!sorry!
      just a question:does this procedure maintain the aspects associated to the file in the repository?

      • Thank you for the feedback Nox,
        Yes, definetely. Of course all the custom models and configurations should be restored in target environment.

  2. hi i dont have lucene index but i have onlt solr folder under alf_data do i need to back up it saperately or not

    thanks

    • Hi Amit,

      Solr runs on a different application server and separated environment. For that reason uses an indipendent strategy to backup/restore indexes.

      So the answer is: yes, you have to backup the indexes separately.

      Greetings.

  3. Hi Francesco

    I had to change the file alfrescoBackup.sh according this:
    DB_HOME=”/opt/postgresql/9.0.4″ instead of
    DB_HOME=”/opt/postgresql”

    I am not sure this applies to other people..

    With this, the cript works well.

    many thanks, best regards.

    Jean-Pierre Buttet

  4. Hello, I’m Brazilian. I have sought unsuccessfully in Portuguese, a way to make backup copy of the alfresco. I do not speak English, but I’m asking for your help. how do I backup alfresco on windows seven?
    I’ve tried to lead me by the model in linux, but it never worked.
    Thank you.

    • Hi Paulo,

      Nice to have a brazilian contribution.
      I’ll try to suggest you all the best even if your english is basic (google helps from this point of view).
      I can tell you that I used this script also in WIndows environment with success. Of course the first thing to change are the paths and the “commands styles” from “/…/…” to “C:\…\…bat”.
      I hope this help your goal!

      -F

  5. Hi,
    First of all, Thanks for the crisp blog on Backup. I have a question, I’m using Mysql db. Can you please suggest on what to modify in script to backup Mysql databases.

    Thanks for your time.!

    • Hi Sankar,

      Thank you for your feedback.
      You should:
      – change the ‘DB_TYPE=”postgresql”‘ command to ‘DB_TYPE=”mysql”‘.
      – Add a new if option to
      # 2) Backup of the database of Alfresco with vendor service.
      if [ “$DB_TYPE” = “postgresql” ]; then

      if [ “$DB_TYPE” = “mysql” ]; then
      cd $TARGET_FOLDER
      // TODO: New command to dump the db in $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

      Let me know how it goes and I can add you contribution to the post.
      Thank you in advance for sharing the solution.

  6. Hi Franchesco,

    I would like to give this a try but before I do I have a question about our particular case at my office. Our consultant for Alfresco whom made our customizations installed our Alfresco system wit two instances of Tomcat. One for share and one for alfresco. How should I go about running a backup script in this type of environment? One more thing how would restore work? Hopefully you see this because I am looking forward to your response.

    • Hi Khrissy,

      The two tomcat don’t have influence on the backup strategy except for the stop/start of the services (in your case you probably need to stop/start both).
      Backup/restore strategy affects the database and the ‘alf_data’ folder.

      • Hi, I want to know how to backup lucene indexes? Did you write restore script? 🙂 Thanks anyway..

  7. Hi Francesco.
    Great tutorial,I have to work Alfresco Community 4.2.e ,And postgreSQL Database,now i need to backup the database, And I’m using Windows 7 OS. I run this script my C:\Alfresco\alfrescoBackup.sh it’s not working?How can i run this .sh not supported while now i install cygwin to run this script But i got same error. Please Help Me How to run this script On Windows 7.

    thanks & Regards
    Stalin louis

    • Hi,
      Thank you for your feedback.
      In this thread there is a similar question from Paulo Roberto.
      I confirm what I wrote him:
      I can tell you that I used this script also in WIndows environment with success. Of course the first thing to change are the paths and the “commands styles” from “/…/…” to “C:\…\…bat”.
      I hope this help your goal!

  8. Hi Francesco,
    Is it possible to insert a function for alert users online for the imminent backup procedure starting (e.g. 10 minute later)? It would be really usefull and professional. And then to add, while backup running, an info page that inform about backup, and… to trying later.
    Is it very complicated?
    Thank you

    • I have added a line to my script that uses SSMTP to email me whenever the backup is complete.

      I am sure you could set up SSMTP to email a group of people at the beginning of the script. Then use the sleep function to pause the script for a few minutes, before it starts with the backup.

      • interesting… better than nothing. Not simple for me, but I will try.
        Thank’s

  9. Sorry, another question…
    I have my “alf_data” folder located in an other partition of HDD, so I have changed costant ALF_HOME like this:

    ALF_HOME=”/media/hdDati/alfresco”

    ALF_DATA_SUBDIR=”alf_data”

    That’s correct ?

  10. Hi there. This is great, except I get the following errors:
    line 4: $’\r’: command not found
    line 11: $’\r’: command not found
    line 82: syntax error: unexpected end of file

  11. Hi, for some reasons I put in stand-by this implementation and now I have a new error. 😮
    If I run /opt/alfresco/alfrescoBackup.sh from promt to test it, prompt tell me:
    usage: /opt/alfresco/alfrescoBackup.sh [targetPath]

    What does this message?
    The script worked properly when I have created it!
    thank you in advance

      • sorry for my ignorance…
        Now it is clear by re-reading crontab changes 😐
        Can I put path directly in the code between brackets?
        And I would like copy backup .tgz in an other destination by default every time. Is it possible to insert new instructions in alfrescoBackup.sh?

  12. when I start alfrescoBackup, system respons is:
    /opt/alfresco/alfrescoBackup.sh: line 27: /opt/postgresql/9.3.4/bin/pg_dump: Permission denied

    why permission denied?
    thank you

    • sorry…
      all code is working well if it run from crontab.
      Manually I have an error about permission at line 27…

      • Hi… the backup regarding postgres have size =0. Is this connected to the error above mentioned?
        Directories permissions are the same as the installation :-o.
        Any ideas?
        Thank you very much

  13. Hi Elvis,
    Yes, the problem is related to permits.
    I suggest you to pay attention because the user executing the cron process could not be the same of the one you use from the command line.
    I hope this help you.

    • I have used command line to try out the script, but from 6 days now backup was generated automatically from crontab.
      And inside there postgres backup size is 0!
      I followed your steps literally

      • looks like your database backup is just failing. Do you have a ~/.pgpass file?

        To backup the postgres database you need a ~/.pgpass file that will send the postgres info, when the script tries to log into the postgres user:

        a. Create the .pgpass file
        i. sudo nano ~/.pgpass

        b. Add the file contents.
        i. *:*:database_name:username:password

        c. Set the file’s access rights
        i. sudo chmod 0600 ~/.pgpass

        https://wiki.postgresql.org/wiki/Pgpass

  14. no, in the /opt/postgresql/9.3.4/bin I don’t have this file.
    Thank’s for now. I will try out with your suggestions

    • You have to create the file, and it doesn’t get stored in your /bin folder. It is stored in the ~/ folder.

      • no that’s the user home folder. I wasn’t sure if it was the home folder of root, or your own user account, so I put a copy in both homes.

  15. Hi,
    I have created .pgpass file in /home/postgres/ but don’t work.
    I obtain postgres backup size = 0.
    If I run this command I get:

    /opt/alfresco/alfrescoBackup.sh: line 27: /opt/postgresql/9.3.4/bin/pg_dump: Permission denied.

    I do not know how to get out of it…

  16. Thank you Francesco,
    I too think so, but I have set pg_dump as 2777 (all rights for all), ownership=alfresco, group=root.
    pg_dump is located in: /opt/postgresql/9.3.4/bin/
    alfresco id was added to root group during installation.
    In the .tar backup file I have:
    4,xxGB for ..alfresco_data
    0kB for …alfresco_postgresql

    Boh!

    • Hi Elvis,
      Alfresco added to root group is a strange thing.
      For sure this is not correct and could cause problems.
      I suggest you to repeat the installation checking the right settings.
      I hope this will help you.

  17. Hi Francesco,
    The script is all good and beautiful. However, when i tried to back-up content store (on NFS) that as 700K records it took approximately 4Hrs. And shutting down Alfresco for 4+ Hrs i snot feasible for us as there are daily batch jobs that run every 2Hrs. Is there any quicker alternative for Alfresco community version alf_data back-up.

    Thanks,
    Praveen

    • Hi Praveen,
      I think there is something to understand better because the duration you are sharing is too long. The duration often depends on the alf_data folder dimension. Please, check the MB/GB of this repository because probably the creation of the zip file is the reason. Please, check if you are performing the zip on a network space or something unusual.
      I hope this will help you.

Leave a Reply to stalinlouis Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s