Jahia DX > Script to backup...

1 (1 Good)
0 (0 Bad)

Script to backup your sites within a Jahia installation from bash

by  rvt »  2011/09/01 16:21

 

Guys,

 

Today I made a little boo boo,

during a upgrade of Jahia I forgot to backup my website and

lost a days worth of work. 

 

Here is a little script you can use to backup your sites within a Jahia installation from a bash script. 

You could use cron to even make regular backups (I know I am going todo that!).

 

Admitedly, I am not a bash and sed guru, so there is properly some improvement possible there.

I also need to test if it really works with multiple sites.

 

The script essentially logs in as root into your Jahia installation and finds all websites.

Then it will download each website into a backups directory and tags them with a date and time.

 

Comments and suggestions are highly appreciated!

 

Ries

 

 

Sep 4 update: Line 28 Changed regexp a bit.

Sep 22 update: Added loading username/password from a external source and added test for ZIP integrety.

Nov 29 update: Added option to delete old backups in number of days and added a additional check if the script really had access

 

Nov 19 update : Removed automatic searching of jahia sites and now needs to be given in a list

 

 

 

#!/bin/bash

CONFIGFILE="$HOME/.jahiaBackup"

# Password of user that has root rights
jahiaPassword="<>"

# USername of user that has root rights
jahiaUsername="<>"

# URL to access jahia, without the last slash
jahiaUrl="http://127.0.0.1:18080"

# Backup directory, relative or ansolute
backupDir="./backups"

# How many days of backups to keep, if set to 0 we will never delete anything
# Entery one means keep todays and yesterday files
backupDaysToKeep=2

backupSites=(sitekeyA sitekeyB)

# ================= Don't edit below ===============
# Fetch passwords
if [ -r "$CONFIGFILE" ]; then
   source $HOME/.jahiaBackup
else
   echo "Configuration file not found, will use configuration within this file"
fi

pid=$$

echo '----------------------------------------------------------------------------'
echo " Logging in into ${jahiaUrl}"

# Login to the website and go to the page that lists all sites
curl -c /tmp/jahiacookies${pid}.txt "$jahiaUrl/cms/login" > /dev/null 2>&1
curl -d "doLogin=true&username=$jahiaUsername&password=$jahiaPassword" -c /tmp/jahiacookies${pid}.txt -b /tmp/jahiacookies${pid}.txt "$jahiaUrl/cms/login" > /dev/null 2>&1
curl -d "" -c /tmp/jahiacookies${pid}.txt -b /tmp/jahiacookies${pid}.txt "$jahiaUrl/administration/?do=sites&sub=list" > /dev/null 2>&1
curl -o /tmp/jahiasiteList_${pid}.html -d "" -c /tmp/jahiacookies${pid}.txt -b /tmp/jahiacookies${pid}.txt "$jahiaUrl/administration/?do=sites&sub=list" > /dev/null 2>&1

if grep -q sitebox /tmp/jahiasiteList_${pid}.html 
then
   echo ' Making backups'
   if [ ! -d ${backupDir} ]; then
       mkdir ${backupDir}
   fi

   for site in ${backupSites[*]}; do
   echo '----------------------------------------------------------------------------'
       thisDate=`date +%Y%m%d_%H%M`
       echo "backing up : ${site} into ${backupDir}/site_${site}_${thisDate}.zip"
       curl -o "${backupDir}/site_${site}_${thisDate}.zip" -d "do=sites&sub=multipledelete&exportformat=site&sitebox=${site}&live=false" -c /tmp/jahiacookies${pid}.txt -b /tmp/jahiacookies${pid}.txt "$jahiaUrl/cms/export/default/site_${site}_${thisDate}.zip"
#        echo "## Testing integrity of zip site_${site}_${thisDate}.zip"
#        zip -T "${backupDir}/site_${site}_${thisDate}.zip"
#        if [ $? != "0" ] ; then
#            echo "---------------------------------------------"
#            echo "  WARNING INTEGRETY CHECK OF ZIP VAILED"
#            echo "  ${backupDir}/site_${site}_${thisDate}.zip"
#            echo "  PLEASE RE-RUN BACKUP PROCEDURE"
#            echo "---------------------------------------------"
#        fi
   done

   if [ "0" -lt "${backupDaysToKeep}" ]; then
   echo '----------------------------------------------------------------------------'
   echo "  Deleting files that are older then $((1+backupDaysToKeep)) day(s)";
   echo '----------------------------------------------------------------------------'
       find ${backupDir} -iname "site_*.zip" -mtime +$((1+backupDaysToKeep)) -delete
   else
   echo "No files will be deleted"
   fi
   rm -f /tmp/jahiacookies_${pid}.txt
   rm -f /tmp/jahiasiteList_${pid}.html
   exit 0
else
   echo "---------------------------------------------"
   echo "  WARNING Login failed, please check username and password"
   echo "---------------------------------------------"
    rm -f /tmp/jahiacookies_${pid}
    rm -f /tmp/jahiasiteList_${pid}.html 
   exit 1
fi

  • Script to backup your sites within a Jahia installation from bash
    2016/03/08 16:39

    rvt <p> &nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo"> Guys,</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px"> &nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo"> Today I made a little boo boo,</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo"> during a upgrade of Jahia I forgot to backup my website and</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo"> lost a days worth of work.&nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px"> &nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo"> Here is a little script you can use to backup your sites within a Jahia installation from a bash script.&nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo"> You could use cron to even make regular backups (I know I am going todo that!).</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px"> &nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo"> Admitedly, I am not a bash and sed guru, so there is properly some improvement possible there.</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo"> I also need to test <span style="color: #b933a1">if</span> it really works with multiple sites.</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px"> &nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo"> The script essentially logs <span style="color: #b933a1">in</span> as root into your Jahia installation and finds all websites.</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo"> Then it will download each website into a backups directory and tags them with a date and time.</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px"> &nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo"> Comments and suggestions are highly appreciated!</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px"> &nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo"> Ries</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px"> &nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px"> &nbsp;</p> <p> <sub>Sep 4 update: Line 28 Changed regexp a bit.</sub></p> <div> <p> <sub>Sep 22 update: Added loading username/password from a external source and added test for ZIP integrety.</sub></p> <div> <sub>Nov 29 update: Added option to delete old backups in number of days and added a additional check if the script really had access</sub></div> </div> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px"> &nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px"> Nov 19 update : Removed automatic searching of jahia sites and now needs to be given in a list</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008212"> &nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008212"> &nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008212"> &nbsp;</p> <p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008212"> #!/bin/bash<br /> <br /> CONFIGFILE=&quot;$HOME/.jahiaBackup&quot;<br /> <br /> # Password of user that has root rights<br /> jahiaPassword=&quot;&lt;&gt;&quot;<br /> <br /> # USername of user that has root rights<br /> jahiaUsername=&quot;&lt;&gt;&quot;<br /> <br /> # URL to access jahia, without the last slash<br /> jahiaUrl=&quot;<a href="http://127.0.0.1:18080/">http://127.0.0.1:18080</a>&quot;<br /> <br /> # Backup directory, relative or ansolute<br /> backupDir=&quot;./backups&quot;<br /> <br /> # How many days of backups to keep, if set to 0 we will never delete anything<br /> # Entery one means keep todays and yesterday files<br /> backupDaysToKeep=2<br /> <br /> backupSites=(sitekeyA sitekeyB)<br /> <br /> # ================= Don&#39;t edit below ===============<br /> # Fetch passwords<br /> if [ -r &quot;$CONFIGFILE&quot; ]; then<br /> &nbsp;&nbsp;&nbsp;source $HOME/.jahiaBackup<br /> else<br /> &nbsp;&nbsp;&nbsp;echo &quot;Configuration file not found, will use configuration within this file&quot;<br /> fi<br /> <br /> pid=$$<br /> <br /> echo &#39;----------------------------------------------------------------------------&#39;<br /> echo &quot; Logging in into ${jahiaUrl}&quot;<br /> <br /> # Login to the website and go to the page that lists all sites<br /> curl -c /tmp/jahiacookies${pid}.txt &quot;$jahiaUrl/cms/login&quot; &gt; /dev/null 2&gt;&amp;1<br /> curl -d &quot;doLogin=true&amp;username=$jahiaUsername&amp;password=$jahiaPassword&quot; -c /tmp/jahiacookies${pid}.txt -b /tmp/jahiacookies${pid}.txt &quot;$jahiaUrl/cms/login&quot; &gt; /dev/null 2&gt;&amp;1<br /> curl -d &quot;&quot; -c /tmp/jahiacookies${pid}.txt -b /tmp/jahiacookies${pid}.txt &quot;$jahiaUrl/administration/?do=sites&amp;sub=list&quot; &gt; /dev/null 2&gt;&amp;1<br /> curl -o /tmp/jahiasiteList_${pid}.html -d &quot;&quot; -c /tmp/jahiacookies${pid}.txt -b /tmp/jahiacookies${pid}.txt &quot;$jahiaUrl/administration/?do=sites&amp;sub=list&quot; &gt; /dev/null 2&gt;&amp;1<br /> <br /> if grep -q sitebox /tmp/jahiasiteList_${pid}.html&nbsp;<br /> then<br /> &nbsp;&nbsp;&nbsp;echo &#39; Making backups&#39;<br /> &nbsp;&nbsp;&nbsp;if [ ! -d ${backupDir} ]; then<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mkdir ${backupDir}<br /> &nbsp;&nbsp;&nbsp;fi<br /> <br /> &nbsp;&nbsp;&nbsp;for site in ${backupSites[*]}; do<br /> &nbsp;&nbsp;&nbsp;echo &#39;----------------------------------------------------------------------------&#39;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thisDate=`date +%Y%m%d_%H%M`<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;backing up : ${site} into ${backupDir}/site_${site}_${thisDate}.zip&quot;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl -o &quot;${backupDir}/site_${site}_${thisDate}.zip&quot; -d &quot;do=sites&amp;sub=multipledelete&amp;exportformat=site&amp;sitebox=${site}&amp;live=false&quot; -c /tmp/jahiacookies${pid}.txt -b /tmp/jahiacookies${pid}.txt &quot;$jahiaUrl/cms/export/default/site_${site}_${thisDate}.zip&quot;<br /> # &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;## Testing integrity of zip site_${site}_${thisDate}.zip&quot;<br /> # &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zip -T &quot;${backupDir}/site_${site}_${thisDate}.zip&quot;<br /> # &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if [ $? != &quot;0&quot; ] ; then<br /> # &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;---------------------------------------------&quot;<br /> # &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot; &nbsp;WARNING INTEGRETY CHECK OF ZIP VAILED&quot;<br /> # &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot; &nbsp;${backupDir}/site_${site}_${thisDate}.zip&quot;<br /> # &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot; &nbsp;PLEASE RE-RUN BACKUP PROCEDURE&quot;<br /> # &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;---------------------------------------------&quot;<br /> # &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi<br /> &nbsp;&nbsp;&nbsp;done<br /> <br /> &nbsp;&nbsp;&nbsp;if [ &quot;0&quot; -lt &quot;${backupDaysToKeep}&quot; ]; then<br /> &nbsp;&nbsp;&nbsp;echo &#39;----------------------------------------------------------------------------&#39;<br /> &nbsp;&nbsp;&nbsp;echo &quot; &nbsp;Deleting files that are older then $((1+backupDaysToKeep)) day(s)&quot;;<br /> &nbsp;&nbsp;&nbsp;echo &#39;----------------------------------------------------------------------------&#39;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find ${backupDir} -iname &quot;site_*.zip&quot; -mtime +$((1+backupDaysToKeep)) -delete<br /> &nbsp;&nbsp;&nbsp;else<br /> &nbsp;&nbsp;&nbsp;echo &quot;No files will be deleted&quot;<br /> &nbsp;&nbsp;&nbsp;fi<br /> &nbsp;&nbsp;&nbsp;rm -f /tmp/jahiacookies_${pid}.txt<br /> &nbsp;&nbsp;&nbsp;rm -f /tmp/jahiasiteList_${pid}.html<br /> &nbsp;&nbsp;&nbsp;exit 0<br /> else<br /> &nbsp;&nbsp;&nbsp;echo &quot;---------------------------------------------&quot;<br /> &nbsp;&nbsp;&nbsp;echo &quot; &nbsp;WARNING Login failed, please check username and password&quot;<br /> &nbsp;&nbsp;&nbsp;echo &quot;---------------------------------------------&quot;<br /> &nbsp; &nbsp; rm -f /tmp/jahiacookies_${pid}<br /> &nbsp; &nbsp; rm -f /tmp/jahiasiteList_${pid}.html&nbsp;<br /> &nbsp;&nbsp;&nbsp;exit 1<br /> fi</p>

  • Number of messages  185
    Registration date Sep 1, 2011
    0 (0 Good)
    0 (0 Bad)

    Re: Script to backup your sites within a Jahia installation from bash

    by  faissah »  2011/09/07 15:26

    Hi Ries,

    Great job and thank you so much for sharing this with the community. This script can actually be usefull for many people. I will try it and keep you in touch if i have any issue with it.

    Thanks again,

    Fabrice

    Fabrice  (faissah)

    Number of messages  264
    Registration date
    0 (0 Good)
    0 (0 Bad)

    Re: Re: Script to backup your sites within a Jahia installation from bash

    by  rvt »  2011/09/07 17:59

    Fabrice,

    I am more then happy to share it...

    If you know a better/more reliable way to get the site keys rather then using a regexp through the HTML, then I can modify the code accordingly....
    Reason for creating was simple, during a update of Jahia I forgot to backup my current site, using this script it's just a matter of executing it, wait a couple of minutes and DONE! :)

    Ries

  • Re: Re: Script to backup your sites within a Jahia installation from bash
    2011/09/07 17:59

    rvt <p> Fabrice,</p> <p> I am more then happy to share it...</p> <p> If you know a better/more reliable way to get the site keys rather then using a regexp through the HTML, then I can modify the code accordingly....<br /> Reason for creating was simple, during a update of Jahia I forgot to backup my current site, using this script it&#39;s just a matter of executing it, wait a couple of minutes and DONE! :)</p> <p> Ries</p>

  • Number of messages  185
    Registration date Sep 7, 2011
    Contact
    Share
    Feedback

    Get in touch

    Whether you are a current user or if you are just evaluating Jahia, we are here to help.

    Contact us

    Share this page