Tag Archives: wordpress

A Backup Script For WordPress

I’ve written a backup script in order to get my WordPress blog backup automatically. The script is only usable on a Linux/Unix box, since it uses default GNU tools.

The script connects to the server via ssh, copies a folder to a location, dumps a database to the same place with the copied folder, creates a tar.gz out of it, then gets the new file via ftp to a prefered location.

The important thing here is that, you should add your ssh public key to the server so that ssh will connect automatically. I also use .my.cnf files to login mysql without specifying password, so you’d better do that. I’ve talked about it in an earlier post here.

Keep in mind that you need an ftp client to connect. If you don’t have it, install it using yum, apt or whatever.

#!/bin/bash

###  START OF EDIT THESE ###
############################
HOST='192.168.1.1' # ip address of your server
SSHUser='root' # user to connect as ssh
FTPUser='myfunkyftpusername' # user to connect as ftp
FTPPass='mysupersecretFTPpassword!' # ftp connection password
MYSQLUser='root' # # user to connect as MySQL

SSHPort=22 # change if different
FTPPort=21 # change if different
DB=wordpress # which database to backup?
DIRECTORY='/home/eaydin/public_html/wp-content' # directory to back up - server side
DIRWRITE='/home/eaydin/' # move the backup here on the server.
DROPBOX='/home/eaydin/Dropbox' # local file path to backup - host side. use your Dropbox folder?
FILENAME='wp-backup' # Filename to use for backups

### END OF EDIT THESE ###
#########################
DIRWRITE=${DIRWRITE%/} # remove trailing / from dir name.
FILENAME=${FILENAME%/} # remove trailing / from filename in case the user types it.
DATE=`eval date +%d%m%Y"-"%H%M` # create date format. (created on the host side, not server. depends on the host time setings.)
FILETAR=$FILENAME-$DATE.tar.gz # name of the tar.gz file (not path!)

ssh -t $SSHUser@$HOST -p $SSHPort "\
cp -R $DIRECTORY $DIRWRITE/$FILENAME-$DATE ;\
mysqldump --add-drop-table -u $MYSQLUser $DB > $DIRWRITE/$FILENAME-$DATE/wordpress.sql ;\
tar -cvzf $DIRWRITE/$FILETAR $DIRWRITE/$FILENAME-$DATE ;\
chown $FTPUser:$FTPser $DIRWRITE/$FILETAR ;\
rm -rf $DIRWRITE/$FILENAME-$DATE
"
ftp -n $HOST $FTPPort <<END_SCRIPT
quote USER $FTPUser
quote PASS $FTPPass
lcd $DROPBOX
cd $DIRWRITE
binary
get $FILETAR
quit
END_SCRIPT
exit 0

The lines between 5 and 17 are the ones you should edit, they’re all self explained in the comments.

It’s a good idea to add the script to your crontab.
In order to do it, especially on Ubuntu systems, just add your current PATH value right below the /bin/sh line. Like this,

eaydin@eaVT:~$ echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

So you should add this line at the top of the script,

#!/bin/env bash
PATH=/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

Or, just run the script whenever you want. I usually set the download path (defined as the $DROPBOX variable on line 16) to my Dropbox folder, this way my backups get automatically synced on the Dropbox server.

Installing WordPress on CentOS 6.x

Let’s say you’ve got your new CentOS 6.x server up and running, with a minimal install. It is very easy to install the necessary items (including Apache, MySQL etc.) alongside WordPress to fully function.

First, let’s install the plugged.in LAMP installer for CentOS 6.x With this script, you’ll get everything you need to turn your Linux box into a web server. Details on how to use it is available on plugged.in

After this, we can easily get the wordpress installation and simply extract it.

wget http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz

Now get into MySQL using the password you’ve specified to plugged.sh

mysql -u root -p

Below, we’ll create a new database and new user to affiliate with.

CREATE DATABASE wordpress;
CREATE USER wordpressuser@localhost;
SET PASSWORD FOR wordpressusername@localhost= PASSWORD("password");
GRANT ALL PRIVILEGES ON wordpress.* TO wordpressusername@localhost IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
exit

Of course, you’ll have to replace “wordpressuser” and “password” with yours.
After creating the necessary database, we tell wordpress itself about these.

cp ~/wordpress/wp-config-sample.php ~/wordpress/wp-config.php
vi ~/wordpress/wp-config.php

Open and edit the wp-config.php file according to change the database info as below (yet again, replace with yours)

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'wordpressuser');

/** MySQL database password */
define('DB_PASSWORD', 'password');

Save and exit.
Now we should move our files to the user folder we've created with plugged.sh and change the files' owner.
cp -r ~/wordpress/* /home/username/public_html/
chown -R username:username /home/username/public_html/
chmod -R 755 /home/username/public_html/

Now visit your domain, or IP address on your browser. It will redirect you to the WordPress onscreen installation instructions. If it doesn’t, add “wp-admin/install.php” to the end. (like example.com/wp-admin/install.php).

And you’re done!

Also, if you want to import an XML file from your old WordPress blog or something, you’ll need to install the php-xml package.

yum install php-xml