Last active
August 29, 2018 09:56
-
-
Save bastibeckr/ff8e0d3c32b7ba8dba47bf6ae007b5c2 to your computer and use it in GitHub Desktop.
WordPress Backup via wp-cli & rsync
This file contains 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
#!/usr/bin/env bash | |
# | |
# Will output a list of commands to backup WordPress web-root + Database: | |
# This script won't execute the commands, so you can review them before executing. | |
# | |
# Requirements: wp-cli, rsync | |
# | |
# 1. create backup folder based on date | |
# 2. backup WordPress DB via wp-cli | |
# 3. backup WordPress web-root via rsync | |
# 4. tar + gzip backup folder | |
# 5. remove backup folder | |
# 6. output a list of backups older than X days | |
# | |
# Variables to set: | |
# | |
# BACKUP_BASE_PATH: Base path to your backups. Commands will generate yymmdd_backup.tar.gz files inside this directory. | |
# WP_HTTP_PATH: Path to WordPress-Installation | |
# WPCLI_PATH: Path to wp-cli executable | |
# BACKUP_KEEP_DAYS: Output files older than ${BACKUP_KEEP_DAYS} days - you may want to remove or archive these files. | |
# BACKUP_PREFIX: Name for your backup e.g. "sitename" => files are named "yymmdd-HHMMSS_sitename.tar.gz" | |
# | |
# Example output: | |
# | |
# # Command to create backup folders: | |
# mkdir -p "/home/myname/backups/180829/db" "/home/myname/backups/180829/wp" | |
# | |
# # Command to backup WordPress DB: | |
# /usr/local/wp --path="/var/www/my-wordpress-blog.com" db export "/home/myname/backups/180829/db/my-wordpress-blog_180829.sql" | |
# | |
# # Command to backup WordPress web-root-folder: | |
# rsync -Cavz --no-o --no-g --omit-dir-times --delete --exclude=".DS_Store" "/var/www/my-wordpress-blog.com/" "/home/myname/backups/180829/wp/" | |
# | |
# # Command to compress backup folder: | |
# tar -zcvf "/home/myname/backups/180829-115405_my-wordpress-blog.tar.gz" "/home/myname/backups/180829" | |
# | |
# # Command to remove backup folder: | |
# rm -r "/home/myname/backups/180829" | |
# | |
# # Command to find backups older than 10 days: | |
# find /home/myname/backups -type f -name '*.tar.gz' -mtime +10 | |
# | |
# ===================== | |
# Use at your own risk! | |
# ===================== | |
BACKUP_BASE_PATH="" | |
WP_HTTP_PATH="" | |
BACKUP_KEEP_DAYS="10" | |
WPCLI_PATH="" | |
BACKUP_PREFIX="" | |
BACKUP_DATE="$(date '+%y%m%d')" | |
BACKUP_TIME="$(date '+%H%M%S')" | |
BACKUP_PATH_DATE="${BACKUP_BASE_PATH%/}/${BACKUP_DATE}" | |
BACKUP_PATH_DB="${BACKUP_PATH_DATE%/}/db" | |
BACKUP_PATH_WP="${BACKUP_PATH_DATE%/}/wp" | |
BACKUP_DB_FILENAME="${BACKUP_PATH_DB%/}/${BACKUP_PREFIX}_${BACKUP_DATE}.sql" | |
TAR_FILENAME="${BACKUP_BASE_PATH%/}/${BACKUP_DATE}-${BACKUP_TIME}_${BACKUP_PREFIX}.tar.gz" | |
BACKUP_DIR_CMD="mkdir -p \"${BACKUP_PATH_DB}\" \"${BACKUP_PATH_WP}\"" | |
BACKUP_DB_CMD="${WPCLI_PATH} --path=\"${WP_HTTP_PATH%/}\" db export \"${BACKUP_DB_FILENAME}\"" | |
RSYNC_CMD="rsync -Cavz --no-o --no-g --omit-dir-times --delete --exclude=\".DS_Store\" \"${WP_HTTP_PATH%/}/\" \"${BACKUP_PATH_WP%/}/\"" | |
TAR_CMD="tar -zcvf \"${TAR_FILENAME}\" \"${BACKUP_PATH_DATE}\"" | |
RM_BACKUP_DIR_CMD="rm -r \"${BACKUP_PATH_DATE}\"" | |
FIND_CMD="find ${BACKUP_BASE_PATH} -type f -name '*.tar.gz' -mtime +${BACKUP_KEEP_DAYS}" | |
echo -e "\n# Command to create backup folders:" | |
echo -e "${BACKUP_DIR_CMD}" | |
echo -e "\n# Command to backup WordPress DB:" | |
echo -e "${BACKUP_DB_CMD}" | |
echo -e "\n# Command to backup WordPress web-root-folder:" | |
echo -e "${RSYNC_CMD}" | |
echo -e "\n# Command to compress backup folder:" | |
echo -e "${TAR_CMD}" | |
echo -e "\n# Command to remove backup folder:" | |
echo -e "${RM_BACKUP_DIR_CMD}" | |
echo -e "\n# Command to find backups older than ${BACKUP_KEEP_DAYS} days:" | |
echo -e "${FIND_CMD}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment