Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@petermolnar
Last active January 5, 2018 10:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petermolnar/f22a8c80dbc412b8c0f0936346b52ff2 to your computer and use it in GitHub Desktop.
Save petermolnar/f22a8c80dbc412b8c0f0936346b52ff2 to your computer and use it in GitHub Desktop.
cron file for wordpress backup and wp-cron using wp-cli
MAILTO="you@domain.com"
SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
# prerequisities:
# sudo wget -q -O/usr/local/bin/wpcli https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar; sudo chmod 0755 /usr/local/bin/wpcli
# WP CRON
* * * * * wpuser cd /path/to/wordpress; wpcli cron event run --due-now --quiet
# WP CLI EXPORT
30 11 * * * wpuser cd /path/to/wordpress; wpcli export --quiet --max_file_size=500 --dir=/path/to/backups; cd /path/to/backups; for xml in $(ls *.xml); do xz -z6 ${xml}; done
# WP DB backup
1 11 * * * wpuser CONF="/path/to/wordpress/wp-config.php"; if [ ! -f "$CONF" ]; then echo "no WP config!"; exit 1; fi; USER=`grep DB_USER ${CONF} | cut -d"'" -f4`; PASS=`grep DB_PASSWORD ${CONF} | cut -d"'" -f4`; DB=`grep DB_NAME ${CONF} | cut -d"'" -f4`; /usr/bin/mysqldump -S /var/run/mysqld/mysqld.sock -u${USER} -p${PASS} ${DB} | xz -z6 >/path/to/backups/wp-mysql-`date +"\%Y-\%m-\%d"`.sql.xz
# WP update
1 16 * * * wpuser cd /path/to/wordpress; wpcli core update --quiet; wpcli plugin update --all --quiet; wpcli theme update --all --quiet;
# Cleanup old exports
1 21 * * * wpuser cd /path/to/backups; find . -mtime +30 -exec rm {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment