Apr
Wordpress backup script
Posted by jerry as hosting
I was looking to upgrade wordpress today, and realized that there MUST be a better way. So, I hit google up for a “wordpress upgrade script”. Sure enough, I found a great script at http://pthree.org/2007/03/02/wordpress-upgrade-script/. I played with it, but it still required a lot of work - looking up database usernames and whatnot. I figured that I could pull the username, database name and password out of the wp-config.php file. And I could.
So, now the script just pauses to remind you to deactivate your plugins. I tried to find a way to automate that, but have struck out so far. If you know how, let me know! I’ll give you credit.
Script follows:
#!/bin/bash
# Updated by: Jerry Bell # License remains intact # Changes: # - Added a backup directory that is outside the document root. The previous script left copies of the database accessible # - Added code to automatically determine the database name, database username, and database password from the wp-config.php file.
# - Removed "read" prompts - they're not really needed any more
# - Set tar to send the archive to the backup directory instead of document root. # # This program upgrades your existing Wordpress installations that you are running on your server. # # You need to make the necessary adjustments to this script as needed for your situation. # # Make this script executable: 'chmod 777 wp_upgrade.sh' # Run the script: './wp_upgrade.sh' # # Author: Aaron Toponce # License: GPL v2 # Version: 0.1.2
# =================== Start of Script =================== #
# Provide the necessary directories to what Wordpress installations need to be backed up space delimited
# Change as necessary and uncomment
# For example, if you had 3 sites in /var/www/site1, /var/www/site2 and /var/www/site3
# then it would look like below (do not add the trailing slash):
# directories=(/var/www/site1 /var/www/site2 /var/www/site3)
backupdir=/var/tmp
number=${#directories[@]}
# Testing that all directories specified above are valid before beginning
for (( i = 0 ; i < number ; i++ )); do
if [[ ! -d ${directories[$i]} ]]; then
echo “Directory ${$directories[$i]} does not exist.”
return 1
fi
done
# First, we need to get the necessary file cd ~
if [[ -f wordpress.tar.gz ]]; then echo “wordpress.tar.gz exists. Please take notice to this upgrade before continuing.” return 1 fi
wget -O latest.tar.gz http://wordpress.org/latest.tar.gz
echo "First disable all plugins on all installations before continuing." echo "Press ENTER to continue..." read blah
for (( i = 0 ; i < number ; i++ )); do
clear
cd ${directories[$i]}
back_ts=$(date +%s)
mkdir ${backupdir}/backup_${back_ts}
echo “We are backing up the full directory, in case anything goes wrong. Press ENTER…”
read blah
tar -cvvf ${backupdir}/backup_${back_ts}/backup.tar ${directories[$i]}
gzip ${backupdir}/backup_${back_ts}/backup.tar
# Timestamp in unix epoch format to create unique backup directories
echo "backing up database for ${directories[$i]}:”
# Backing up the necessary Wordpress database
wp_db=`grep DB_NAME ${directories[$i]}/wp-config.php | cut -f 4 -d “‘”`
wp_user=`grep DB_USER ${directories[$i]}/wp-config.php | cut -f 4 -d “‘”`
wp_pass=`grep DB_PASSWORD ${directories[$i]}/wp-config.php | cut -f 4 -d “‘”`
mysqldump –add-drop-table -u ${wp_user} -p${wp_pass} ${wp_db} > ${backupdir}/backup_${back_ts}/${wp_db}.sql
# Make the necessary changes for what to backup. This is the default as provided by Wordpress.
echo “Backing up the important files. Press ENTER…”
read blah
cp .htaccess wp-config.php ${backupdir}/backup_${back_ts}
cp -r wp-content wp-images wp-includes/languages ${backupdir}/backup_${back_ts}
# Time to copy the latest wordpress that we downloaded and overwrite all files echo "Getting the latest cp of wordpress. Press ENTER..." read blah cp ~/latest.tar.gz ./wordpress.tar.gz tar -zxvf wordpress.tar.gz
# Overwrite all files echo "Overwriting all old Wordpress files with the new." cd wordpress cp -rf * ../
# Copy the files that we backed up back
echo "Coping the important backed up files back in."
cp -rf ${backupdir}/backup_${back_ts}/.htaccess ${directories[$i]}
cp -rf ${backupdir}/backup_${back_ts}/wp-config.php ${directories[$i]}
cp -rf ${backupdir}/backup_${back_ts}/wp-content ${directories[$i]}/wp-content
cp -rf ${backupdir}/backup_${back_ts}/wp-images ${directories[$i]}/wp-images
cp -rf ${backupdir}/backup_${back_ts}/wp-includes/languages ${directories[$i]}/wp-includes/languages
echo "Point your browser to the necessary site and run the upgrade script." echo "EG: http://example.com/wp-admin/upgrade.php” echo “Update your permalinks and .htaccess.” echo “Install updated plugins and themes” echo “Reactivate plugins” echo “Press ENTER to continue…” read blah done
clear echo "Congratulations! You have successfully upgraded your Wordpress." echo "Please review that your browser resolves your site." echo "Enjoy!"
Leave a Comment:
You must be logged in to post a comment.
-
Categories
-
Archives
-
Pages
-
Recent Posts
- [6.5.2008]
What happened to Chuck E Cheeze? - [1.5.2008]
Some cool scripts I’ve found - [30.4.2008]
Iran’s decision to trade oil in Euros instead of dollars - [29.4.2008]
Wordpress backup script - [17.2.2008]
Authentication in the real world
- [6.5.2008]
-
Meta