Small WordPress backup script that sends email on failed backups and deletes old backups

This is a simple backup script in bash, for WordPress on Linux. It is based on the script from Small WordPress backup script post.
Like the script there it will dump MySQL database used by WordPress site and create a tar.gz file that will have WordPress site files and database dump.
This script will also send out an email if the backup process fails on any stage and delete old backups files.

#!/bin/bash

bkpDir=/local/backup/directory
webroot=/wordpress/install/directory/
bkpAge=90 #How many days of backup to keep

dbUser=$(grep DB_USER $webroot/wp-config.php | awk -F\' '{print$4}')
dbName=$(grep DB_NAME $webroot/wp-config.php | awk -F\' '{print$4}')
dbPassword=$(grep DB_PASSWORD $webroot/wp-config.php | awk -F\' '{print$4}')
dbDump="$bkpDir""$dbName"_$(date +'%F_%H_%M').sql

rUser=remoteuser
rHost=remoteserver
rDir=/remote/backup/directory/
rSSHport=22

email="[email protected]"
errorFile="$bkpDir"errors

rm -f "$errorFile"

mysqldump -u $dbUser -p$dbPassword $dbName > $dbDump || echo "$(date +'%F %T') MySQL backup of $dbName failed." >> "$errorFile"

tar -czf "$bkpDir"srvfail_$(date +'%F_%H_%M').tar.gz $webroot $dbDump || echo "$(date +'%F %T') Tar process failed." >> "$errorFile"

rsync -az $bkpDir -e "ssh -p $rSSHport" $rUser@$rHost:$rDir --delete || echo "$(date +'%F %T') rsync to $rHost failed." >> "$errorFile"

rm -f "$bkpDir"*$(date -d "$bkpAge days ago" +'%F')*

if test -f "$errorFile"; then
    cat "$errorFile" | mail -s "Backup process on $(hostname) failed" "$email"
fi

It will create two backup files, one for database dump in format that looks like “dbname__2021-12-01-06-00.sql” and one tar.gz file that will look like “wpbackup_2021-12-01_06-00.tar.gz”.

Script can be saved as wpbackup.sh and put in crontab to run at a certain time like 6:00AM.

0 6 * * * /path/to/script/wpbackup.sh >/dev/null 2>&1

or if you want to have output of it in case of errors

0 6 * * * /path/to/script/wpbackup.sh > /var/log/wpbackup.log 2>&1

Small WordPress backup script

Simple backup script written in bash, for WordPress on Linux, that will dump MySQL database used by WordPress site and create tar.gz file consisting of WordPress site files and database dump

#!/bin/bash

BKPDIR=/local/backup/directory
WEBROOT=/wordpress/install/directory/

DBUSER=$(grep DB_USER $WEBROOT/wp-config.php | awk -F\' '{print$4}')
DBNAME=$(grep DB_NAME $WEBROOT/wp-config.php | awk -F\' '{print$4}')
DBPASSWORD=$(grep DB_PASSWORD $WEBROOT/wp-config.php | awk -F\' '{print$4}')
DBDUMP="$BKPDIR""$DBNAME"_$(date +"%Y-%m-%d-%H-%M").sql

#In case you want to rsync backups to remote server
RUSER=remoteuser
RHOST=remoteserver
RDIR=/remote/backup/directory/
RSSHPORT=22

mysqldump -u $DBUSER -p$DBPASSWORD $DBNAME > $DBDUMP

tar -czvf "$BKPDIR"wpbackup_$(date +"%Y-%m-%d_%H-%M").tar.gz $WEBROOT $DBDUMP

rsync -az $BKPDIR -e "ssh -p $RSSHPORT" $RUSER@$RHOST:$RDIR

It will create two backup files, one for database dump in format that looks like “dbname_2018-01-04-06-00.sql” and one tar.gz file that will look like “wpbackup_2018-01-04_06-00.tar.gz”.

Script can be saved as wpbackup.sh and put in crontab to run at a certain time like 6:00AM.

0 6 * * * /path/to/script/wpbackup.sh 2>/dev/null

It can also be run manually with bash wpbackup.sh

WordPress http to https switch using Cloudflare SSL

If you have a WordPress site behind Cloudflare free account and you want to make your site or part of it accessible over HTTPS you can use free Cloudflare Universal SSL certificate.

Enable SSL on Cloudflare

To enable your site being served over HTTPS from Cloudflare, login to your Cloudflare account, select domain you want to enable SSL on and click on the Crypto page with lock icon, to manage cryptography settings for your website.

Cloudflare Crypto

On the Crypto page first setting on top will be SSL, where you can switch SSL from Off to one of the three other options available.

There are three options you can use to enable SSL:

  • Flexible: Cloudflare will serve site to end users on https, but will communicate with your site over http, in this case you don’t have to install SSL on your site’s server or make any changes on your site, if you are using relative links, where http is not hardcoded in any links.
  • Full: Cloudflare will serve end users over https, communicate with your site over https, but will not verify the authenticity of the SSL. In this case you need to install SSL on your site and have your site’s server listen for connections on port 443, but you can use self-signed certificate as it’s authenticity is not verified.
  • Full (strict): Cloudflare will server end users over https, communicate with your site over https and will verify the validity of your SSL. As with Full option above you will need to set port 443 for listener and install SSL, but in this case you will need to install a valid SSL containing your site hostname and signed by certificate authority. You do not need to buy SSL for this to work, you can configure Let’s Encrypt SSL or use Origin CA certificates, generated by Cloudflare.

It would be recommended to use Full (strict option) for SSL as it maximizes security of your site data.

You can see all options explained with more details in this Cloudflare support article.

Create Edge and Origin certificates

If you don’t already have SSL with your site hostname listed under Edge Certificates, click on Order SSL certificate button and select Universal SSL (Shared) option. This will be SSL certificate server to visitors of your site by Cloudflare.

Edge Certificates

To get SSL certificate that will be used for communication between Cloudflare and your site, go to Origin Certificates section of page and click on Create Certificate button. You do not need to create Origin Certificate if you used Flexible option when you enabled SSL or if you already have some other SSL on your web server.

Origin Certificates

Once you click on Create Certificate you will be presented with Origin Certificate Installation window where in most cases you can just leave default options, after which you will be presented with Certificate and Private key which you need to install on your web server.

Change your WordPress to https URLs

If you have used Full of Full (strict) option when enabling SSL you need to edit your WordPress settings in Settings » General” and change your WordPress Address (URL) and Site Address (URL) fields to https URL instead of http URL.

You do not need to make this change if you are using Flexible option when enabling SSL on Cloudflare.

If you want to have your whole site being served over HTTPs and not just some subsets of it, like wp-admin or a store page, enable Always use HTTPS option on Crypto page of your Cloudflare account.

Always use HTTPS

If you don’t want to have whole site over https, but only some parts of URL, like example.com/wp-admin, example.com/wp-login.php or example.com/section leave this option Off, and setup redirection to https for those pages either on your web server or in Page Rules page on your Cloudflare account.

To avoid getting mixed content warning in browser when your page is visited over https enable Automatic HTTPS Rewrites option, which will try to rewrite your content from http to https if a link or a reference to a http URL is available over https.

Automatic HTTPS rewrites

Change to https in webmaster tools

If you are using webmaster tools in Bing, Google or Yandex here is how to notify those search engines about your change to https:

Google
Google doesn’t at this time support http to https change in their Change of Address option, so you will need to add a new property with https URL, and data from http property will eventually change to your new https property.

Bing
Go to Diagnostics & Tools » Site Move” and select  I am moving URLs from one place to another within the current site” and then input your old URL in Source field and new https URL in Destination field.

Yandex
Add new site address with URL using https and then on the old site with http go to “Indexing » Relocate site” and tick “Add HTTPS” box.

Add sidebar in WordPress Twenty Eleven single post pages

This is a repost of a post from an old blog, made on January 3, 2012, that used to be on:

http://adminramble.com/add-sidebar-wordpress-twenty-eleven-single-post-pages/

Original post:

The big disappointment to me was the fact that on single-post pages you don’t get to have sidebars, which made the page really odd looking  to me, so I started looking how to add it through the editor directly in code of the pages.

I looked through several sites and blogs that tell you how to do that but then found that there’s no need to add any child themes or make any modification through the editor.

There is a simple WordPress plugin that lets you do that with only a few clicks:

Twenty Eleven Theme Extensions

Once you install it and activate it you will get Theme Extensions option under the Appearance section which lets add sidebar widget to single-post pages and pages, plus a few other options.

Hope that helps someone.

How to export WordPress comments to csv, xls, or some other file format

This is a repost of a post from an old blog, made on September 18, 2012, that used to be on:

http://wp.me/p25nt4-8z

http://adminramble.com/export-wordpress-comments-csv-xls-file-format/

Original post:

Here is a short tutorial on how to export WordPress comments to a excel .xls file, but you can also export them to PDF, Word or some other file type if you want.

You can export your comment either through the phpMyAdmin which most web hosting providers today have or by connecting to you MySQL server some other way and using a SQL query. The both are nothing else then just exporting the wp_comments table to a file

In both cases you will need to have a username and password for your MySQL server, to connect with.

There is also a plugin called Export comments to export comments to a csv file, which seems to be working with WordPress 3.4.2, but the file you get this way is pretty messy, you will get much more structured data by exporting directly from the MySQL.

Export through phpMyAdmin web interface

Connect to your phpMyAdmin and login with you username and password.

On home screen, click the plus sign by the database for your WordPress blog and select the wp_comments table. Then click on the export tab and select the format of the file you want to export to. Press the Go button and download the file to your computer. (The procedure might be a little different for you, depending on which version of phpMyAdmin you have installed)

You can then edit your .xls file further to contain only the information you need.