Skip to content

Instantly share code, notes, and snippets.

@projectivemotion
Created August 11, 2016 08:57
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 projectivemotion/550c756eac8473461c8ec9aa7586b990 to your computer and use it in GitHub Desktop.
Save projectivemotion/550c756eac8473461c8ec9aa7586b990 to your computer and use it in GitHub Desktop.
Create a full backup of a Wordpress Website in one single step with this bash script.
#!/bin/env bash
#
# Author: Amado Martinez - AmadoMartinez.mx
# License: MIT License
# Date: 2016-08-11
#
# Backup a complete wordpress installation
#
if [ $# -lt 2 ] ; then
head -n 10 "$0" | grep "^#" | tail -n +2
echo -e "\nUsage:\n\t$0 wordpress-directory backup-storage-directory"
echo
exit
fi
# todo add support for PORT config
readconf (){
# $1 can be: NAME, USER, PASSWORD, HOST, CHARSET, COLLATE
valstr=$(grep "DB_$1" "$2/wp-config.php" | cut -d"'" -f4)
if [ "$1" == "HOST" ] ; then
echo "$valstr" | cut -d: -f1
else
echo "$valstr"
fi
}
datestr=$(date +%Y%m%d)
destinationdir="$2/$datestr/"
wpdir="$1"
db_name=$(readconf NAME "$wpdir")
db_user=$(readconf USER "$wpdir")
db_pass=$(readconf PASSWORD "$wpdir")
db_host=$(readconf HOST "$wpdir")
echo "Creating $destinationdir .."
mkdir -p "$destinationdir"
echo "Backing up database.."
mysqldump -u "$db_user" "-p$db_pass" -h "$db_host" "$db_name" > "$destinationdir/$db_name.sql"
echo "Creating Tar File"
tar -czf "$destinationdir/wordpress-site.tar.gz" "$wpdir"
echo "Finished backup to $destinationdir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment