So if you need a quick way to archive your WordPress install and you have shell access through SSH, this script could help you do that quickly. You will need to make sure that ‘zip’ is installed (of course you could also modify the script below to use something like gzip or bz2 or whatever).
So the steps to getting this to work are:
- Upload the script below to the server as “wordpress-arch.sh”
- Make it executable by running “chmod +x wordpress-arch.sh”
- Execute the script by running “./wordpress-arch.sh” or “/bin/sh /path/to/script/wordpress-arch.sh”
- Specify a file name without the zip extension
- Then provide the full path to the WordPress root folder
The script will then copy all of the existing files to a directory with the same name as the file name you specified. Next it will find the wp-config.php and find the database connection info. Then runs mysqldump and saves a .sql to the root of the copied files. Zips up that directory to a zip archive in the same directory the script was executed from, and deletes the copy of the wordpress installation.
Some assumptions it makes is that you want to put the database dump in the root of the archive, and that you want to clean out your database username and password before passing it to someone else for install.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# !bin/bash # wordpress-arch.sh # run 'chmod +x wordpress-arch.sh' to allow for execution # execute by running './wordpress-arch.sh' or '/bin/sh wordpress-arch.sh' # get unknown info read -p "Enter final archive file name without the extension (.zip) " archPath read -e -p "Enter the full path to the root of the wordpress installation " indir # find database information WPDBNAME=`cat $indir/wp-config.php | grep DB_NAME | cut -d \' -f 4` WPDBUSER=`cat $indir/wp-config.php | grep DB_USER | cut -d \' -f 4` WPDBPASS=`cat $indir/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4` # echo the Wordpress config info echo "Found this database information" echo "DBNAME $WPDBNAME" echo "DBUSER $WPDBUSER" echo "DBPASS $WPDBPASS" # make a new directory for the migration mkdir $archPath # copy all of the files to a new directory cp -rfv $indir/* $archPath # dump database using the found wordpress database settings mysqldump -u$WPDBUSER -p$WPDBPASS $WPDBNAME > "$archPath/$WPDBNAME.sql" # get rid of Wordpress Database User & Password before zipping to archive sed -ie "s/$WPDBUSER//gI" $archPath/wp-config.php sed -ie "s/$WPDBPASS//gI" $archPath/wp-config.php # zip up all of the files zip -r "$archPath.zip" $archPath # remove the copied files rm -rf $archPath |
I know, I know… pretty basic. However, it’s something I needed, and maybe just maybe someone else does as well. I might change this up to make it an automated migration script that could deploy to a remote server, install all of the files, import the database, and setup an Apache host. But for now, this is all I need. 🙂