Sep
11
2017




Automate WordPress backup with Python / Fabric

Python’s Fabric module makes automation easy.  This post shows how to use a Fabric script to backup a WordPress installation, but could be used for most LAMP backup situations!

Goals of this Post:

  • backup SQL to /tmp
  • backup WEB ROOT to /tmp
  • tar SQL + site & zip
  • download
  • delete /tmp

The Fabric script is written for to be executed as a ‘sudo’ user.  If you want to perform the actions in the script as root or another user,  replace ‘sudo()’ with ‘run()’.

For help installing Fabric look at my previous post: https://www.savelono.com/linux/how-to-install-python-3-and-pip-3-for-centos-6.html

For more information on Fabric: http://www.fabfile.org/

Example:

[matt@mattcom1 wpbackup]$ fab backup_all
[savelono.com] Executing task ‘backup_all’
[savelono.com] sudo: mysqldump -u savelonoadmin -p45482lonoF#m! –database savelono > savelono-$(date +%m-%d-%Y).sql
[savelono.com] out: sudo password:
[savelono.com] out:
[savelono.com] sudo: cp -rfp /var/www/html savelono-site-$(date +%m-%d-%Y)
[savelono.com] out: sudo password:
[savelono.com] out:
[savelono.com] sudo: tar -czvf savelono-site-$(date +%m-%d-%Y).tar.gz savelono-*
[savelono.com] out: sudo password:
[savelono.com] out: savelono-09-11-2017.sql
[savelono.com] out: savelono-site-09-11-2017/
[savelono.com] out: savelono-site-09-11-2017/readme.9d9380967341b4551da5eb9ee499

[savelono.com] out: savelono-site-09-11-2017/json2.js
[savelono.com] out: savelono-site-09-11-2017/wp-login.php
[savelono.com] out: savelono-site-09-11-2017/images
[savelono.com] out:

[savelono.com] download: /home/matt/backup/savelono-site-backups/savelono.com/savelono-site-09-11-2017.tar.gz <- /tmp/savelono-site-09-11-2017.tar.gz
[savelono.com] sudo: rm -rf /tmp/savelono*
[savelono.com] out: sudo password:
[savelono.com] out:

Done.
Disconnecting from savelono.com:7022… done.

And that’s it!  Easy Python syntax for issuing remote and local shell commands through SSH.  Thank you for reading!  I hope you find this useful.

fabfile.py:

 

If you like this article tell Google!!









Written by mattb in: Automation,Python | Tags: , , ,

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment