Jedv

Rsync backup script for websites (document root) & MySQL databases – to rsync.net or others

I have just got an account with rsync.net to backup one of my server’s websites, including the document root for the webserver and all the MySQL databases for them. I made a little more fancy Bash script for storing everything that is a little more complex than just one rsync command and caters for the dumping of the databases to individual named/dated files.

Read the header comments of the file for instructions on usage.

#!/bin/bash
# Backup a directory (document root) and all MySQL databases and send to rsync.net.
# You must have an account with rsync.net backup service (or similar) and have setup automatic ssh login via keys.
# See: http://www.rsync.net/resources/howto/ssh_keys.html
# Create the remote RSYNC_PATH using ssh before running the script.
# It is best to run this with a cronjob either daily/weekly/monthly,
# run it with --quiet in the crontab for no output except from errors
# <root@livingthedream.fun>

RSYNC_HOST=""
RSYNC_USER=""
RSYNC_PATH="" # Do not start or end the path with a /
MYSQL_USER=""
MYSQL_PASSWORD=""
DOCUMENT_ROOT_PATH="" # Do put trailing slash on this path.

# No need to edit below lines unless you need to modify behaviour.

databases=`mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`

QUIET=false
RSYNC_OPT="-avH"
if [ "$@" ] && [ "$@" == "--quiet" ]; then
 QUIET=true
 RSYNC_OPT="-avHq"
 echo "Starting backup.."
 echo "Dumping all MySQL databases.."
fi
mkdir -p sql
for db in $databases; do
    if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
        if [ "$QUIET" = false ]; then
          echo "Dumping database: $db"
        fi
        mysqldump -u $MYSQL_USER -p$MYSQL_PASSWORD --databases $db > sql/`date +%Y%m%d`.$db.sql
    fi
done
if [ "$QUIET" = false ]; then
  echo "Done."
  echo "Sending SQL databases to $RSYNC_PATH/sql"
fi
rsync $RSYNC_OPT sql/ $RSYNC_USER@$RSYNC_HOST:$RSYNC_PATH/sql/
if [ "$QUIET" = false ]; then
  echo "Done."
  echo "Backing up document root to $RSYNC_PATH/sites"
fi
rsync $RSYNC_OPT $DOCUMENT_ROOT_PATH $RSYNC_USER@$RSYNC_HOST:$RSYNC_PATH/sites/
if [ "$QUIET" = false ]; then
  echo "Done."
  echo "All Done."
fi

Make sure to fill in the variables at the top and setup your cron job once you have tested it – checkout rsync.net for their awesome setup and nicely priced remote backup solutions. Their technical support is also most helpful.

 

Jedv

Fixing: PHP Fatal error: Cannot redeclare wp_ajax_press_this_save_post() on WordPress

So today a client’s site that had its WordPress version updated (to 4.9.1) started throwing this error when they tried to use the Metaslider plugin to upload new images. Some searching confirmed this was happening to more than a few other people but I did not easily find any actual solutions by people asking who probably like me had sites that needed the functionality this presented. The error:

PHP Fatal error:
Cannot redeclare wp_ajax_press_this_save_post()
(previously declared in wp-includes/deprecated.php:3921)
in wp-admin/includes/ajax-actions.php on line 3197

Some digging later I found out the function was not just deprecated but in 4.9.1 and above moved outside of the core entirely to an optional plugin that you can download called Press This. This is part of a general clean up initiative where non-essential functionality is being slowly moved out of a more streamlined core and moved into optional plugins.

Just installing the now optional plugin is not enough to fix the issue entirely though at least for now the easiest fix I could come up with that was the least intrusive was to comment out two functions in this file:

wp-admin/includes/ajax-actions.php

If you use your text editor to locate the top function, which is wp_ajax_press_this_save_post() you will find the other one below it and they should look like this:

/**
 * Ajax handler for saving a post from Press This.
 *
 * @since 4.2.0
 */
function wp_ajax_press_this_save_post() {
      include( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
      $wp_press_this = new WP_Press_This();
      $wp_press_this->save_post();
}

/**
 * Ajax handler for creating new category from Press This.
 *
 * @since 4.2.0
 */
function wp_ajax_press_this_add_category() {
      include( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
      $wp_press_this = new WP_Press_This();
      $wp_press_this->add_category();
}

Now use PHP comments to just comment out both functions entirely from being declared, so then only does the deprecation/new plugin functionality take over to make them appear like below and save the file.

/**
 * Ajax handler for saving a post from Press This.
 *
 * @since 4.2.0
 */
//function wp_ajax_press_this_save_post() {
//      include( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
//      $wp_press_this = new WP_Press_This();
//      $wp_press_this->save_post();
//}

/**
 * Ajax handler for creating new category from Press This.
 *
 * @since 4.2.0
 */
//function wp_ajax_press_this_add_category() {
//      include( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' );
//      $wp_press_this = new WP_Press_This();
//      $wp_press_this->add_category();
//}

Then your all set, hopefully WordPress have picked up on this and will just take these two declarations out in the next version or two before it gets annoying.