http://www.guyrutenberg.com/2009/01/06/wordpress-backup-to-ftp/
This script allows you to easily backup your WordPress blog to an FTP server. It’s actually a modification of my WordPress Backup to Amazon S3 Script, but instead of saving the backup to Amazon S3 it uploads it to an FTP server. Another update is that now the SQL dump includes the database creation instructions so you don’t need to create it manually before restoring from the backup.
Although I’ve written it with WordPress in mind (to creates backups of my blog), it isn’t WordPress specific. It can be used to backup any website that consists of a MySQL database and files. I’ve successfully used it to backup MediaWiki installation.
#!/bin/bash
# (C) 2008 Guy Rutenberg
# This is a script that creates backups of my blog.
DB_NAME=guy_blog
DB_USER=******
DB_PASS=******
DB_HOST=localhost
BLOG_DIR=/home/guyru/guyrutenberg.com/
BACKUP_DIR=/home/guyru/backups/
FTP_HOST=88.198.42.4
FTP_USER=******
FTP_PASS=******
FTP_BACKUP_DIR=guyru/
# end of configuration - you probably don't need to touch anything bellow
BLOG_DIR=`dirname "$BLOG_DIR"`/`basename "$BLOG_DIR"`
BACKUP_DIR=`dirname "$BACKUP_DIR"`/`basename "$BACKUP_DIR"`
echo -n "dumping database... "
DUMP_NAME=${DB_NAME}-$(date +%Y%m%d).sql.bz2
mysqldump --user=${DB_USER} --password=${DB_PASS} --host=${DB_HOST} \
--databases ${DB_NAME} \
| bzip2 -c > ${BACKUP_DIR}/${DUMP_NAME}
if [ "$?" -ne "0" ]; then
echo "failed!"
exit 1
fi
echo "done"
echo -n "Creating tarball... "
TAR_NAME=${BLOG_DIR##*/}-$(date +%Y%m%d).tar.bz2
tar -cjf ${BACKUP_DIR}/${BLOG_DIR##*/}-$(date +%Y%m%d).tar.bz2 ${BLOG_DIR}
if [ "$?" -ne "0" ]; then
echo "failed!"
exit 2
fi
echo "done"
echo -n "Uploading SQL dump and tarball to FTP... "
lftp -u ${FTP_USER},${FTP_PASS} ${FTP_HOST} <<EOF
cd "${FTP_BACKUP_DIR}"
put "${BACKUP_DIR}/${DUMP_NAME}"
put "${BACKUP_DIR}/${TAR_NAME}"
EOF
if [ "$?" -ne "0" ]; then
echo "failed!"
exit 3
fi
echo "done"
The configuration is done using the variables in the beginning of the script (until the “end of configuration” line). They are pretty self explanatory, and you should adjust them for your site before using the script. If there are variables you don’t understand what they do, see the post about the previous script, and don’t hesitate to comment.
I usually prefer to keep a copy of my backups on the server as an extra precaution. But if you want them to be removed after a successful FTP upload, just append the following to the script:
rm -f "${BACKUP_DIR}/${DUMP_NAME}"
rm -f "${BACKUP_DIR}/${TAR_NAME}
Copy the above code and save it to a file called backup-blog. Before you can use it there are some configurations to be done. At the beginning of the script (right after the copyright notice), you will find some variable you need to set.
The first set of variables is the database configuration. Set DB_NAME to the name of your database, DB_USER to your database username, DB_PASS to the user’s password and DB_HOST to the hostname of the database server. For most users the DB_HOST should be set for localhost (but some users need to specify a different value here).
The next variable is BLOG_DIR. Set it to the full path of the directory where you installed WordPress. BACKUP_DIR should be set to the full path of the directory you to keep the backups in, make sure the user which will run the script has write access to it. Notice that both paths shouldn’t end with a trailing ‘/’ (slash).
Now that the configuration is over, make the script executable:
chmod +x backup-blog
Now you can test that everything works ok by running the script: ./backup-blog. If the script failed for some reason make sure your configuration is correct.
The next step is to automate the backup process using cron. Run crontab -e to add an entry to the backup script. Append the following line to the crontab:
30 1 * * 4 /path/to/backup-blog
This will run the script automatically for you once a week, on 01:30 AM every Friday. Be sure to check up the backup folder from time to time to make sure everything is running smooth.
Note that while the script was originally intended to create backups of WordPress blogs it can also be used to backup any kind of website that needs both files and database backups, like MediaWiki, Drupal, or almost any kind of CMS and blogging platform.
近期评论