Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Scripts, WordPress

WordPress Backup Script

Submitted by on October 23, 2009 – 1:04 pm 7 Comments

Whenever a new WordPress version comes out, I get an itch to upgrade as quickly as possible. Generally, this is not a good idea, unless you enjoy working out new software bugs and dealing with broken plugins. Still, sooner or later you still have to upgrade and there is always a chance that something will go terribly wrong. You always need to make a backup.

The WP-DB-Backup plugin is, shall we say, an industry-standard tool for backing up WordPress database. A full backup needs to be performed prior to upgrading WordPress. You should also backup the database before adding, removing or upgrading any plugins. However, the database backup is just the first half of the process: you also need to back up your WordPress installation, along with all your plugins, themes and configuration files.

Below is the script that will created a compressed tarball of your entire WordPress installation directory (but of not the database). The resulting file may be quite large, depending on how much stuff you uploaded to your site. You will need to modify these two variables: $WORKDIR and $WWWHOME. With most hosting providers, your WordPress installation will be located in /home/your_username/public_html. You will also need to create the backup directory: /home/your_username/backups

Do not forget to cleanup old backup files once in a while, otherwise you may run out of disk space allocated by your hosting provider.

#!/bin/ksh

# This script will do a full backup of the public_html directory

# -----------------------
# FUNCTIONS
# -----------------------

configure() {
        WORKDIR="/home/your_username"
        WWWHOME="${WORKDIR}/public_html"
        BACKUPDIR="${WORKDIR}/backups"
        DATE=`/usr/bin/date +'%Y-%m-%d_%H-%M'`
        BACKUPFILE="public_html_backup_${DATE}.tar"
        LOG="${BACKUPDIR}/backup_site.log"
        if [ ! -d "${BACKUPDIR}" ]
        then
                mkdir "${BACKUPDIR}"
        fi
}

log() {
       LINELIM=1000 ; LINEBUF=50 ; LINEMAX=`echo "${LINELIM}+${LINEBUF}"|bc -l`
       if [ ! -r "$LOG" ]
       then
               touch "$LOG"
       fi

       if [ -f "$LOG" ] && [ `wc -l "$LOG" | awk '{print $1}'` -gt $LINEMAX ]
       then
               cat "$LOG" | tail -$LINELIM > "$TMP"
               mv "$TMP" "$LOG"
       fi
}

message() {
       if [ ! $TAIL ]
       then
               TAIL=1
       else
               TAIL=`echo "${TAIL}+1"|bc -l`
       fi
       echo "`hostname`        `date +'%Y-%m-%d %T'`   $MSG" >> "$LOG"
       tail -1 "$LOG"
}

verify() {
        STATUS=0
        for DIR in "${WORKDIR}" "${WWWHOME}" "${BACKUPDIR}"
        do
                if [ ! -d "${DIR}" ]
                then
                        MSG=`echo "ERROR: Directory ${DIR} not found."` ; message
                        STATUS=1
                fi
        done

        if [ $STATUS -ne 0 ]
        then
                echo "Backup encountered fatal error! Exiting..."
                exit $STATUS
        fi
}

backup() {
        tar -cvf ${BACKUPDIR}/${BACKUPFILE} ${WWWHOME}

        if [ $? -eq 0 ]
        then
                if [ -r "${BACKUPDIR}/${BACKUPFILE}" ]
                then
                        gzip "${BACKUPDIR}/${BACKUPFILE}"
                        if [ $? -eq 0 ]
                        then
                                MSG=`echo "INFO: Backup completed on $DATE"` ; message
                        else
                                MSG=`echo "ERROR: Backup failed on $DATE"` ; message
                        fi
                fi
        else
                MSG=`echo "ERROR: Backup failed on $DATE"` ; message
        fi
}

# -----------------------
# RUNTIME
# -----------------------

configure
log
message
verify
backup
Print Friendly, PDF & Email

7 Comments »

  • Dark_LovexXx says:

    What Free web hosting with sub domain is the best…?

    post now

  • stealspartansbcglobalnet says:

    I am on a Journalism degree and it’s essential for us to keep a blog. Now, I’m not too sure which one is better and which is the most popular. Any ideas? Thanks

  • Roar me R says:

    I would also like it to be user-friendly and able to customise it too.

  • Phillip123 says:

    Hi

    I’m running WP over Xampp.
    Till last night everything was working fine, after that whenever i browse http://127.0.0.1/wordpress i get 404 page not found error, it’s funny ! i can browse admin panel. even other scripts i installed except this one !
    using ‘localhost’, wordpress/index.php didn’t answer.

    Reinstalled xampp (didn’t help).
    Restore backup sql (didn’t work).

    Would u plz tell me what’s going and what should i do?

  • Matthew says:

    Hi everyone,

    what would happen if changed

    with

    Does anyone know what it is and why it is used?
    Thanks in advance

  • Alina Elliott says:

    After attempting to upgrade to WordPress 3.3.1 using the automatic upgrade, and then receiving an error message, I tried to fix it via ftp. I’ve replaced all of wp-admin and wp-includes, as well as the individual loose files from wp-content and the home directory. Now when I go to my site admin I get the following:

    Warning: require(/home/seekinge/public_html/wp-includes/classes.php) [function.require]: failed to open stream: No such file or directory in /home/seekinge/public_html/wp-settings.php on line 68

    Fatal error: require() [function.require]: Failed opening required ‘/home/seekinge/public_html/wp-includes/classes.php’ (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home/seekinge/public_html/wp-settings.php on line 68

  • JimT says:

    Well, I think something may have happened in the FTP, I use cPanel (HostGator). So when i type my website URL: http://www.chessnoir.com, the site dones’t come up, it is all gebrish….

    When I try a backup, I get this:

    This script must be placed at the root of a valid WordPress installation. Please install WordPress via cPanel –> Fantastico and then run this script again.

    What should I do? Thanks!
    tthe ‘Script’ would be the backup

Leave a Reply

%d bloggers like this: