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% [?]




