Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created August 14, 2016 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepmal/382223b365092d4bc6e8fccbaa483d37 to your computer and use it in GitHub Desktop.
Save trepmal/382223b365092d4bc6e8fccbaa483d37 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Back up my files and databases
#
#
# backup_static_if <fullpath> <shortname>
# use directory fingerprint to avoid unnecessary backups
#
function backup_static_if {
local fullpath=$1
local shortname=$2
# flight check
if [ -z $fullpath ]; then echo "please provide 1st argument, fullpath"; return; fi;
if [ ! -d $fullpath ]; then echo "$fullpath doesn't exist"; return; fi;
if [ -z $shortname ]; then echo "please provide 2nd argument, shortname"; return; fi;
local fingerprintfile="${fullpath%/}/.fingerprint"
local fingerprint=''
# get last fingerprint
if [ -f $fingerprintfile ]; then
fingerprint=`cat $fingerprintfile`
fi
# get new fingerprint
local newprint=`find $fullpath -type f ! -name ".fingerprint" -exec ls -l {} \; | awk '{print $5, $6, $7, $8, $9}' | sort -k5 | md5sum | awk '{print $1}'`
# compare
if [ "$fingerprint" = "$newprint" ]; then
echo 'Fingerprint match, do nothing'
# touch file so it doesn't get bumped out of local backups
touch `ls -t /var/www/_backups/${shortname}_files_* | head -1`
else
echo 'No fingerprint match, prepare archive'
backup_static $fullpath $shortname
echo 'Update fingerprint'
echo -n $newprint > $fingerprintfile
echo 'Done'
fi
}
# backup_static <fullpath> <shortname>
#
function backup_static {
local fullpath=$1
local shortname=$2
if [ -z $fullpath ]; then echo "please provide 1st argument, fullpath"; return; fi;
if [ ! -d $fullpath ]; then echo "$fullpath doesn't exist"; return; fi;
if [ -z $shortname ]; then echo "please provide 2nd argument, shortname"; return; fi;
local oneup=`dirname $fullpath`
local dir=`basename $fullpath`
local date=`date +%d%m%y%H%M`
# archive it. can safely remove `--transform` if needed, it keeps the extracted dir prefixed, instead of generic "wp-content"
echo 'Pack files...'
tar czf /var/www/_backups/${shortname}_files_${date}.tar --transform="s,^,$shortname-," -C ${oneup} $dir
echo 'Done'
}
# backup_wp <fullpath> <shortname>
#
function backup_wp {
local fullpath=$1
local shortname=$2
if [ -z $fullpath ]; then echo "please provide 1st argument, fullpath"; return; fi;
if [ ! -d $fullpath ]; then echo "$fullpath doesn't exist"; return; fi;
if [ -z $shortname ]; then echo "please provide 2nd argument, shortname"; return; fi;
local date=`date +%d%m%y%H%M`
echo 'Export database...';
# export db, gzip, and place in backups dir
wp --allow-root db export --path=$fullpath - | gzip > /var/www/_backups/${shortname}_db_${date}.gz
echo 'Done.'
# get content dir, we don't need to backup wp core files
local wpcontent=`wp --allow-root eval "echo WP_CONTENT_DIR;" --path=$fullpath`
# then backup files
backup_static_if $wpcontent $shortname
}
# remove local backups older than 5 days
#
function cleanup_backups {
echo 'Clean up...'
local before=`ls -1 | wc -l`
find /var/www/_backups/ -maxdepth 1 -type f -mtime +5 -exec rm {} \;
local after=`ls -1 | wc -l`
local remainder=`expr $before - $after`
echo "${remainder} files removed. Done."
}
# sync to s3
#
function sync_backups {
/usr/local/bin/aws s3 sync /var/www/_backups s3://BUCKET-NAME
}
#
#
#
#
backup_static_if /path/to/foo foo
backup_wp /path/to/wpbar/ wpbar
cleanup_backups
sync_backups
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment