This is a quick addenda to yesterday’s notes on backing up self-hosted WordPress blogs. I noticed that I neglected to make the scripts I described available as downloads, and that I didn’t illustrate the final piece of the puzzle: a one-line script which obviates the need to specify any of the backup script’s 6(!) arguments. I’d like to correct those oversights today.
Scripts
Two pieces of code, for your consideration:
- The Python wrapper around Amazon’s S3.py library
- The bash backup script
Convenience Code
The backup script takes 6 arguments, 5 of which will rarely, if ever, change for a given blog. (The sixth, the archive name, could be programatically generated.) This is a nuisance, easily solved with a one-line wrapper bash script.
The script takes these arguments:
- MySQL username (passed to mysqldump)
- MySQL database (containing the blog)
- S3 credentials filename (containing S3 username and password)
- S3 bucket name (which will contain the backups)
- Archive name (the name of this particular backup)
- Blog directory (the location of your blog in the filesystem)
If we assume that:
- You have a MySQL user (with appropriate permissions) named “admin”
- Your blog is backed by a database named “blog_db”
- Your S3 credentials are stored in a file named “aws.ak”
- You’re storing your backups in a bucket named “blog_backups”
- You will back up your blog no more than once per day
- Your blog is rooted in /www/var/html/blog
Then the following command line will always invoke the backup script properly:
./backup admin blog_db aws.ak blog_backups $(date +%Y%m%d).tar /var/www/html/blog
This line can be stored in its own (no-argument) bash script named, e.g., “runme”, which will be much easier to invoke than the 6-argument form.
S3 Credentials
Yesterday and today, I made numerous references to a file containing your S3 credentials. I use such a file to open connections to S3 in my s3_post.py script. Other people might choose to store credentials in other ways, but this one works for me. Here is a sample of what such a file might look like:
GG2OGG6NAZ1KG3XYUI28
eA84DgexAJQeUJwYfH869rjGilLDEqnBwQA3Er99