commit b5e8dc72cbf949542a97beb196a0da5a5dd42a8d Author: Shane Peters Date: Wed May 10 21:23:04 2023 -0400 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..471ad3f --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +## wp-backup.sh + +This is a stupid script to backup client wordpress sites and upload them to a hosted nextcloud instance. + +### To Use + +Simply update the nextcloud URL, user, and password. diff --git a/wp-backup.sh b/wp-backup.sh new file mode 100644 index 0000000..e485da9 --- /dev/null +++ b/wp-backup.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# Wordpress backup script. +# Uses zip compression since most clients won't know how +# to decompress other formats like .tgz or .7z. + +# Example cron entry: +# 0 6 * * 1 /root/wp-backup.sh /var/www/wordpress /tmp/ >/root/wp-backup.log 2>&1 + +TODAY=$(date +%Y%m%d) +WP_PATH=${1} +ARCHIVE_DIR=${2}/backup-${TODAY} +NCUSER=__CHANGEME__ +NCURL="https://__CHANGEME__/remote.php/dav/files/$NCUSER/HostingBackups" +NCPASS='__CHANGEME__' + +if [ $# -lt 2 ]; then + echo "$0 /dev/null +fi + +if [ ! -f /usr/bin/zip ]; then + echo downloading zip... + sudo apt-get install -y zip 2>/dev/null +fi + +upload() { + client_dir=$(hostname) + printf "[*] CREATING DIRECTORY ${client_dir}..." + if curl -u $NCUSER:$NCPASS -X MKCOL "$NCURL/${client_dir}">/dev/null 2>&1; then + printf "OK\n" + else + printf "FAILED\n" + fi + printf "[*] UPLOADING ${1}..." + if curl -u $NCUSER:$NCPASS -T ${1} "$NCURL/${client_dir}/${1}"; then + printf "OK\n" + rm -f ${1} + else + printf "FAILED\n" + fi +} + +mkdir -p ${ARCHIVE_DIR} +cp -r ${WP_PATH} ${ARCHIVE_DIR}/ +sudo -u www-data wp db export --path=${1} ${ARCHIVE_DIR}/website.sql >/dev/null 2>&1 +cd $(dirname ${ARCHIVE_DIR}) +zip -r backup-${TODAY}.zip $(basename ${ARCHIVE_DIR}) +rm -rf ${ARCHIVE_DIR} +upload backup-${TODAY}.zip