Last active
April 18, 2021 14:06
-
-
Save goblindegook/bca51d9784d3a1bebcad to your computer and use it in GitHub Desktop.
Backup WordPress files and database to Dropbox (shell script)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: | |
# $ backup-wp.sh <path> <sitename> | |
# Requires WP-CLI: | |
# http://wp-cli.org | |
# Requires Andrea Fabrizi's Dropbox Uploader script: | |
# https://github.com/andreafabrizi/Dropbox-Uploader | |
WORDPRESS=$1 | |
SITENAME=$2 | |
CURRENT=$(pwd) | |
NOW=$(date +"%Y-%m-%d_%H%M%S") | |
ARCHIVE="${SITENAME}_${NOW}.zip" | |
BACKUP="/tmp/backup-wp" | |
RED='\e[0;31m' | |
GREEN='\e[0;32m' | |
YELLOW='\e[0;33m' | |
RESET='\e[0m' # No Color | |
if [[ -z "$WORDPRESS" ]]; then | |
WORDPRESS="$CURRENT" | |
fi | |
if [[ -z "$SITENAME" ]]; then | |
SITENAME=$(basename "$WORDPRESS") | |
fi | |
cd "$WORDPRESS"; | |
if ! $(wp core is-installed); then | |
echo -e "${RED}Could not find a WordPress install in ${WORDPRESS}. Aborting.${RESET}" | |
exit 1 | |
fi | |
mkdir -p "${BACKUP}/${SITENAME}" | |
echo -e "${YELLOW}Backing up files...${RESET}" | |
rsync -a --delete \ | |
--exclude *.log \ | |
--exclude .git/ \ | |
--exclude .svn/ \ | |
--exclude cache/ \ | |
--exclude logs/ \ | |
--exclude node_modules/ \ | |
"${WORDPRESS}/" "${BACKUP}/${SITENAME}/" | |
echo -e "${YELLOW}Backing up the database...${RESET}" | |
wp db export "${BACKUP}/${SITENAME}/dump.sql" | |
echo -e "${YELLOW}Creating the archive...${RESET}" | |
cd "${BACKUP}" | |
zip -q -r "${ARCHIVE}" "${SITENAME}" | |
echo -e "${YELLOW}Uploading to Dropbox...${RESET}" | |
cd "${CURRENT}" | |
./dropbox_uploader.sh mkdir ${SITENAME} | |
./dropbox_uploader.sh upload "${BACKUP}/${ARCHIVE}" "${SITENAME}/${ARCHIVE}" | |
rm "${BACKUP}/${ARCHIVE}" | |
echo -e "${GREEN}Done.${RESET}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not using WP-CLI's
--path
option because it doesn't appear to work well with subdirectory installs.