#!/bin/bash # Prepares a Drupal site to be backed-up by populating the 'backup' # directory in the site dir. Makes a listing of the contrib themes and modules # and calls the database backup script to create a dump. # After this script is run, it is a good thing to back up the site's 'sites' directory. # Script will optionally generate a gzipped .tar file containing the sites directory. # include library file . "`dirname ${0}`/drupalLibrary.sh" printUsage() { echo -e " " echo -e "Prepares a Drupal sites dir to be backed up and, optionally, generates a backup tar file." echo -e "For more info, see script header." echo -e " " echo -e "Backs up a site/all sites to the backup directory in the 'site dir'." echo -e "For more info, see script header." echo -e " " echo -e "Usage:" echo -e "$0 [-outputdir ] [-archiveone ] [-archivemany ] ( | all ) [install group root dir]" echo -e "where:" echo -e " " echo -e "-outputdir " echo -e "\tSpecifies the directory where output is placed. Defaults to directory 'backups' in site dir." echo -e "\tCannot be specified if backing up 'all' installations." echo -e " " echo -e "-archiveone " echo -e "\tCreates a single .tar.gz file containing the site directories for all backed-up site(s)." echo -e "\tThe supplied filename will be suffixed with '.tar.gz'." echo -e "\tDO NOT USE RELATIVE PATHS when specifying this filename." echo -e " " echo -e "-archivemany " echo -e "\tCreates a file named _backup.tar.gz in the specified path" echo -e "\tfor each site dumped." echo -e "\tDO NOT USE RELATIVE PATHS when specifying the path." echo -e " " outputInstallGroupUsage foo echo -e " " } ######### # Param handling if [ $# -eq 0 ]; then printUsage exit 0 fi echo " " # Handle flags pOutputDir="" while (true); do case "${1}" in -outputdir ) pOutputDir="${2}" shift; shift # Validate the output directory if [ ! -d "${pOutputDir}" ]; then echo -e "** Specified output directory '${pOutputDir}' does not exist. Aborting.\n" exit 1 fi ;; -archiveone ) pArchiveFile="${2}.tar.gz" shift; shift # Validate this filename touch "${pArchiveFile}" || { echo -e "** Specified archive file (for -archiveone) '${pArchiveFile}' invalid. Aborting.\n" exit 1 } ;; -archivemany ) pArchiveDir="${2}" shift; shift # Validate this dir (create if it doesn't exist) if [ ! -d "${pArchiveDir}" ]; then mkdir -p "${pArchiveDir}" || { echo -e "** Specified archive dir (for -archivemany) '${pArchiveDir}' invalid. Aborting.\n" exit 1 } echo -e "Created archive dir (for -archivemany) '${pArchiveDir}'." fi ;; * ) break ;; esac done # process Group Root Dir pRootDir="`getGroupRootDir "${2}"`" errorStr="`validateGroupRootDir "${pRootDir}"`" # ignore this -- vi colorization can be stupid"( if [ -n "${errorStr}" ]; then echo -e "${errorStr}\n" printUsage exit 1 fi pInstall="$1" if [ "`toLower "${pInstall}"`" == "all" ]; then pInstall="`getSitesInGroupDir "${pRootDir}"`" # Ensure not specifying all installs if specifying output dir if [ -n "${pOutputDir}" ]; then echo -e "** Cannot specify output dir for all installs. Aborting\n" exit 1 fi else # Validate the installation errorStr="`validateSite "${pInstall}" "${pRootDir}"`" if [ -n "${errorStr}" ]; then echo -e "** Error with specified install: ${errorStr}\n" exit 1 fi fi # process for site in ${pInstall}; do echo -en "Processing site '${site}'.... " # Get output dir if not specified if [ -z "${pOutputDir}" ]; then holdOutputDir="`getBackupDir "${site}" "${pRootDir}"`" else holdOutputDir="${pOutputDir}" fi # Create a readme file. cat > "${holdOutputDir}"/readme.backup.txt <> "${holdOutputDir}"/readme.backup.txt # Create contrib module and theme lists `dirname $0`/drupalReport.sh modules "${site}" "${pRootDir}" > "${holdOutputDir}"/contribModulesList.txt `dirname $0`/drupalReport.sh themes "${site}" "${pRootDir}" > "${holdOutputDir}"/contribThemesList.txt # Backup db echo -en ".... " "`dirname $0`/drupalDBBackup.sh" -quiet -outputdir "${holdOutputDir}" "${site}" "${pRootDir}" echo -e "Done." done if [ -n "${pArchiveDir}" ]; then echo -e "Creating set of archive files in directory '${pArchiveDir}'...." # Create archive files for each site for site in ${pInstall}; do archiveFile="${pArchiveDir}/${site}_backup.tar.gz" echo -e "\tprocessing archive file '${archiveFile}'" siteRootDir="`getSitesRootDir "${site}" "${pRootDir}"`" cd "${siteRootDir}" tar czf "${archiveFile}" "${site}"/* done echo -e "\t...Done" fi if [ -n "${pArchiveFile}" ]; then echo -en "Creating (single) archive file '${pArchiveFile}'.... " # Create a hold dir tmpDir="`mktemp -d`" || { echo -e "\n** Could not create temp directory for archive creation. Aborting.\n" exit 1 } # Within it, create links to all the being-backed-up sites' site directories pushd "${tmpDir}" >/dev/null 2>&1 for site in ${pInstall}; do siteDir="`getSiteDir "${site}" "${pRootDir}"`" ln -s "${siteDir}" . done # Create the archive file tar czhlf "${pArchiveFile}" * # Remove our temp dir popd >/dev/null 2>&1 rm -rf "${tmpDir}" echo -e "Done." fi # Done echo -e "\n** Done."