Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An automated drush backup script with folder pruning using ZSH Globbing
#! /bin/zsh
## An automated Drupal backup script using Drush and ZSH globbing.
## Ideally place this script in usr/local/bin
## Set this to run via a cronjob.
## Easy crontab setup: http://crontab-generator.org
## Inspiration from: by https://www.drupal.org/node/470114
## by Danny Englander | dannyenglander.com
# Define System paths
PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin
# If using MAMP, Define MAMP paths, alter as needed for the specfic PHP version being used.
PATH=/Applications/MAMP/Library/bin/:$PATH
PATH=/Applications/MAMP/bin/php/php5.6.10/bin:$PATH
# Define the path to composer / drush
PATH="$HOME/.composer/vendor/bin:$PATH"
#=======================================================================
EXIT_SUCCESS=0 #Script errorlevel if it was executed without problems
#=========================================BACKUP BLOCK============================================
# Drupal 8 site
drush -r /Users/myuser/Sites/myd8site --uri=http://d8.local cr
drush -r /Users/myuser/Sites/myd8site --uri=http://d8.local sql-dump --result-file --gzip
# Drupal 7 site
drush -r /Users/myuser/Sites/myd7site --uri=http://d7.local cc all
drush -r /Users/myuser/Sites/myd7site --uri=http://d7.local sql-dump --result-file --gzip
# ADD MORE SITES AS NEEDED BELOW IN THIS BLOCK.
#====================================END OF BACKUP BLOCK=====================================
#===================================BACKUP CONTROL BLOCK====================================
## Loop to recursively check backup sub-directories and keep 14 newest in each
for d in /path/to/drush-backups/*; do
( cd $d && rm -rf ./*(Om[1,-15]))
done
#=================================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