#/bin/bash # This script can be used to backup multiple MySQL-based MediaWiki instances. # 10 Dec 2012 by Justin Mercier # Declare our wikis in the 'wiki' array. In lieu of multidimensional arrays we simply use a flat comma-delimited format. # Elements are comma-delimited to specify: # - the HTML directory name # - the name of the MySQL database # - the name of the MySQL wiki user (can be found in LocalSettings.php) # - the password for the wiki MySQL user wiki[0]=NDPwiki,NDPwiki,wikiuser,password wiki[1]=DMIAwiki,dmia_wikidb,wikiuser,password wiki[2]=RTMwiki,rtm_wikidb,wikiuser,password #wiki[3]=SomeOtherwiki,Someother_wikidb,SomeOtherwikiuser,SomeOtherpassword # Set the path to the PHP binary. Uses the default installed php unleess the below is commented out and set manually. PHP=`which php` #PHP=/usr/bin/php # The HTML Document root where the Wiki is installed to. On Red Hat this is /var/www/html so change for Debian/Ubuntu/Solaris etc and defined in httpd.conf. HTMLROOT=/var/www/html # The local dump directory DUMPDIR=/usr/local/backups/wiki ### FUNCTIONS VERBOSE=ON DEBUG=ON # die function to echo a message and exit function die () { echo "[FATAL] - $@ ...exiting." exit 1 } # warn function for non-catastrophic errors function warn () { echo "[WARN] - $@." } # info function for conditional verbose output function info () { echo "[INFO] - $@" } # info_n and info_tag function for conditional verbose output without newline function info_n () { if [ $VERBOSE ] && [ "$VERBOSE" = "ON" ]; then echo -e -n "[INFO] - $@" fi } function info_tag () { if [ $VERBOSE ] && [ "$VERBOSE" = "ON" ]; then echo -e "\t\t$@" fi } ### END OF FUNCTIONS # Print a summary info "PHP: $PHP" info "Backup directory: $DUMPDIR" info "Wikis to backup: ${#wiki[@]}" echo if [ -d $DUMPDIR ]; then mkdir -p $DUMPDIR || die "Directory $DUMPDIR does not exist and could not be created" fi # now we iterate through our Wikis and back them up for f in ${wiki[@]}; do read wikiName wikiDBname wikiDBuser wikiDBpass <<<$(echo $f | awk -F, '{ print $1, $2, $3, $4 }') [[ $wikiName && $wikiDBname && $wikiDBuser && $wikiDBpass ]] || (warn "The information for $wikiName ($f) is not complete and will be skipped." & continue) info Processing $wikiName if [ -d $HTMLROOT/$wikiName ]; then pushd $HTMLROOT >/dev/null info_n "\tHTML" tar cfh - $wikiName | gzip -c > "$DUMPDIR/$wikiName"_html.tgz || exit 1 popd >/dev/null info_tag "...complete." else warn "Wiki $wikiname does not exist in $HTMLROOT and will be skipped." continue fi info_n "\tXML" pushd /var/www/html/$wikiName/maintenance >/dev/null $PHP dumpBackup.php --current --quiet | gzip -c > "$DUMPDIR/$wikiName"_xml.tgz || exit 1 popd >/dev/null info_tag "...complete." info_n "\tMySQL" mysqldump -u $wikiDBuser --password=$wikiDBpass $wikiDBname --single-transaction | gzip -c > "$DUMPDIR/$wikiName"_sql.gz || exit 1 info_tag "...complete." info "Backup of $wikiName complete." echo done echo info "Backup of all specified wikis complete!"