Server and Network Monitoring with iPhone
February 25, 2010 – 6:53 pm | No Comment

What is a Unix sysadmin doing with an iPhone, you ask? It was a birthday present, if that’s all right with you. I know, I should have gotten something odd with a beta version of …

Read the full story »
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 and 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 Igor on October 23, 2009 – 1:04 pmNo Comment
WordPress Backup Script

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

Popularity: 3% [?]

Related posts

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.