Skip to content

Instantly share code, notes, and snippets.

@sumeetpareek
Created September 16, 2013 10:34
Show Gist options
  • Save sumeetpareek/6579031 to your computer and use it in GitHub Desktop.
Save sumeetpareek/6579031 to your computer and use it in GitHub Desktop.
A shell script to take dumps of DB and FILES and clean up dumps more than 5 days old.
#!/bin/bash
#=======================================================================
# Details here - https://drupal.org/node/470114
# Script developed by Leandro Scott R. Z. Jacques (with custom modifications by Sumeet) :-)
# Version: 1.0 stable
#
#Script to backup all site's databases installed under the Drupal CMS platform. This Script
#dumps the sites database tables and store them compressed in a specified directory for
#3 days and then sends them to a backup mirror server using SCP.
#
#OBS: For each new installed site it must be included the backup routine commands for it's
#backup inside the backup block of the script. The backup control block defines how many
#days the backup will be stored and for which mirror server the backup will be sent.
#=======================================================================
TIMESTAMP=`date '+%Y_%m_%d__%H:%M:%S'` #Creates a timestamp for the file name in the format "YYYY_MM_DD_HH:MM:SS"
NON_ROOT_EXIT=1 #Script errorlevel if it was not executed by root
EXIT_SUCCESS=0 #Script errorlevel if it was executed without problems
ROOT_UID=0 #Root UID to be compared with the user's UID executing the script
#Uncomment the below block to allow only root access
#if [ "$UID" -ne "$ROOT_UID" ] #If user executing script not root then abort
#then
#echo "You need to be root to execute this scri"
#exit $NON_ROOT_EXIT
#fi
#=========================================DB BACKUP BLOCK============================================
# Take a backup of the India's dev site
drush @aap.india_dev cc all
drush @aap.india_dev sql-dump --result-file=/var/www/devdumps/aamaadmiparty.org/database_dumps/aamaadmiparty.org_DB_$TIMESTAMP.sql --gzip
#====================================END OF DB BACKUP BLOCK=====================================
#===================================DB BACKUP CONTROL BLOCK====================================
cd /var/www/devdumps/aamaadmiparty.org/database_dumps #go to the directory where DB backups are stored
#Remove older than 5 days backups
find . -mtime +4 -type f -exec rm -f '{}' \;
cd /var/www/devdumps/aamaadmiparty.org/files_dumps #go to the directory where files backups are stored
tar -czf aamaadmiparty.org_FILES_$TIMESTAMP.tar.gz -C /var/www/AAP_India/sites/default/files .
#The -C option above helps create the archive relative to the path following it
#Remove older than 5 days backups
find . -mtime +4 -type f -exec rm -f '{}' \;
#Sends the newly created backups to a backup mirror server using SCP(with an authorized key without password on the remote server)
# NO REMOTE SENDING FOR NOW
#su backupuser -c "find . -type f -mmin -1 -exec scp '{}' backupuser@mirrorserver:/path/to/backupdir \;"
#=================================END OF BACKUP COTROL BLOCK==================================
exit $EXIT_SUCCESS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment