Skip to content

Instantly share code, notes, and snippets.

@SalvadorP
Last active August 29, 2015 14:25
Show Gist options
  • Save SalvadorP/d548358363b324bc154f to your computer and use it in GitHub Desktop.
Save SalvadorP/d548358363b324bc154f to your computer and use it in GitHub Desktop.
Bash script to backup entire sites, copies all the contents of each site directory, sends an email with info about the results, admits one parameter in case you want to backup only one site
#!/bin/bash
SITES=(site1 site2 site3)
DATE=$(date +"%Y-%m-%d")
DIR='/directory/where/sites/are'
BACKUPDIR='/directory/where/to_store/backup'
if [ ! -d "$BACKUPDIR" ]; then
mkdir $BACKUPDIR
fi
if [ -n "$1" ]; then
SITES=($1)
fi
echo BACKUP OF SITES AT $DATE >> $BACKUPDIR/result_sites.txt
for i in "${SITES[@]}"; do
SITE=${i}
SITENAME=backup-$SITE-$DATE.tgz
TARTMP=/tmp/$SITENAME
cd $DIR
tar -czvf $TARTMP $SITE
mv $TARTMP $BACKUPDIR
RESULT='';
if [ -f "$BACKUPDIR/$SITENAME" ]; then
RESULT="$SITE Backup Complete and compression done"
fi
echo $RESULT >> $BACKUPDIR/result_sites.txt
done
# erase backups older than one week
find $BACKUPDIR/* -mtime +7 -exec rm {} \;
# send an email with the result_sites.txt
# mail -s "SITE BACKUPS $DATE" [email protected] <<< $BACKUPDIR/result_sites.txt
# echo "Subject: SITE BACKUPS $DATE" | cat - $BACKUPDIR/result_sites.txt | sendmail -F [email protected] -t [email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment