Example backup script using cmscli

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Example backup script using cmscli

Post by calguy1000 »

The following trivial example script is something I threw together to allow me to create a one-command snapshot of a cmsms install using cmscli.

This script currently assumes that 'cmscli' is in the CMSMS root directory (but that's easily changed). It clears the cache, does the db-dump and generates a .tar.gz file.

It demonstrates the usefulness of cmscli as it does not require looking at or remembering the cmsms database credentials.

Your mileage may vary... but you can use this to create your own simple snapshot script.

Code: Select all

#!/bin/bash
_outfile='cmsms_backup.tar.gz'
if [ ! -d tmp -o ! -f lib/version.php ]; then
    echo "This is not a CMSMS root directory"
    exit 1
fi
if [ ! -w . ]; then 
    echo "No write permission to "`pwd`
    exit 1
fi
if [ -f $_outfile -a ! -w $_outfile ]; then
   echo "$_outfile exists but is not writable"
   exit 1
fi

./cmscli db-dump -F tmp/dump.sql && ./cmscli cache-clear && tar zcf $_outfile . && echo "Done.  Backup is in $_outfile"
Potential improvements include:
- taking the site down, and backup before and after doing the backup
- relying on cmscli being in the current path.
- configuring an output file via an option (getopt).
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Example backup script using cmscli

Post by calguy1000 »

Here's a better example that I am now putting in my /usr/local/bin directory on my VPS.

- It is capable of finding cmscli in the path, or via an environment variable. but it prefers one in the local directory (incase it is a newer version).
- It generates the .tar.gz file in the parent directory (as an exercise you could change this to use an argument)
- It excludes the tmp/cache, tmp/templates_c, and upload directories, which is suitable for a quick snapshot but is not a full backup.

Code: Select all

#!/bin/bash
_outfile='../cmsms_backup.tar.gz'

_CMSCLI=`which cmscli`
# check environment variable
if [ ! -z $CMSCLI_PATH ]; then _CMSCLI=$CMSCLI_PATH; fi
# prefer local file
if [ -f bin/cmscli ]; then _CMSCLI='bin/cmscli'; fi
if [ -f ./cmscli ]; then _CMSCLI='./cmscli'; fi

if [ ! -d tmp -o ! -f lib/version.php ]; then
    echo "This is not a CMSMS root directory"
    exit 1
fi
if [ ! -w . ]; then
    echo "No write permission to "`pwd`
    exit 1
fi
if [ -f $_outfile -a ! -w $_outfile ]; then
   echo "$_outfile exists but is not writable"
   exit 1
fi

./cmscli site-down 2>&1 && \
echo "Site is down" && \
$_CMSCLI db-dump -F tmp/dump.sql && \
echo "Database dump (cmsms tables only) created in tmp/dump.sql" && \
$_CMSCLI cache-clear && \
echo "CMSMS cache cleared" && \
tar zcf $_outfile --exclude=tmp/cache/ --exclude=tmp/templates_c/ --exclude=uploads/ . && \
$_CMSCLI cache-clear && \
$_CMSCLI site-up && \
echo "Site is back up" && \
echo "Done.  Backup is in $_outfile"

I may change it to generate a checksum.dat file in the tmp directory too... for later reference. but this is good enough for me right now.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
tristan
Dev Team Member
Dev Team Member
Posts: 375
Joined: Tue May 02, 2006 10:58 am
Location: The Netherlands

Re: Example backup script using cmscli

Post by tristan »

Very cool! Would it be possible to use cmscli to restore these backups as well in case all hope is lost after a failed update for example?
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Example backup script using cmscli

Post by calguy1000 »

cmscli has a 'db-import' and 'db-reset' commands to help with this.

Though they need a few tweaks... the idea is that you can (with just a few commands, or a script you write) automate this.

The order for a restore for the SAME SITE would be
1. extract the archive (we need the config.php)
2. Import the database (cmscli db-import)
3. clear the cache (cmscli cache-clear)

for a site-move
1. extract the archive
2. edit the config.php
3. import the database (cmscli db-import)
4. clear the cache (cmscli cache-clear)
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Post Reply

Return to “Modules/Add-Ons”