{"id":477,"date":"2016-05-07T23:15:39","date_gmt":"2016-05-08T03:15:39","guid":{"rendered":"http:\/\/jasonralph.org\/?p=477"},"modified":"2016-05-07T23:27:20","modified_gmt":"2016-05-08T03:27:20","slug":"python-backup-wordpress-site-database-and-html","status":"publish","type":"post","link":"https:\/\/jasonralph.org\/?p=477","title":{"rendered":"Python Backup WORDPRESS Site \/ DATABASE and HTML"},"content":{"rendered":"<p>I have this blog hosted on a LINODE dedicated LINUX server.  It&#8217;s about 10 dollars a month for a 1 core system with about 250GB of disk space and 1GB of RAM, this server runs the common LAMP stack, I needed a quick and dirty script to backup MYSQL database and the PHP code contained in the \/var\/www\/html folder.  I wanted the script to compress the contents of both and move them into a directory with the correct date. See the comments below outlining the code and the action of running it. <\/p>\n<pre class=\"theme:solarized-dark lang:default decode:true \" >\r\n# Linux server kernel version details.\r\n[22:47:51] root@jasonralph:~\/py_backup_site_full # uname -a\r\nLinux jasonralph.jasonralph.org 4.5.0-x86_64-linode65 #2 SMP Mon Mar 14 18:01:58 EDT 2016 x86_64 x86_64 x86_64 GNU\/Linux\r\n\r\n# Linux distro information\r\n[22:52:15] root@jasonralph:~\/py_backup_site_full # cat \/etc\/centos-release\r\nCentOS release 6.7 (Final)\r\n\r\n# Free output\r\n[22:59:58] root@jasonralph:~\/py_backup_site_full # free -m\r\n             total       used       free     shared    buffers     cached\r\nMem:           991        796        195          0         62        329\r\n-\/+ buffers\/cache:        403        587\r\nSwap:          511          0        511\r\n\r\n# Here you can see the python binary location on the filesystem.\r\n[22:46:36] root@jasonralph:~\/py_backup_site_full # which python\r\n\/usr\/local\/bin\/python\r\n\r\n# Now you can see the python version I am using. \r\n[22:46:50] root@jasonralph:~\/py_backup_site_full # python --version\r\nPython 2.7.6\r\n\r\n# Long listing of the backup folder I have setup. \r\n[22:47:48] root@jasonralph:~\/py_backup_site_full # ls -l\r\ntotal 4\r\n-rwxr-xr-x 1 root root 1560 May  7 22:46 wordpress_backup.py\r\n<\/pre>\n<pre class=\"theme:solarized-dark lang:default decode:true \" >\r\n# cat of the python script to see the source code. \r\n\r\n[23:02:39] root@jasonralph:~\/py_backup_site_full # cat wordpress_backup.py\r\n#!\/usr\/local\/bin\/python\r\nimport optparse\r\nimport os\r\nimport datetime\r\nimport shutil\r\nfrom subprocess import Popen, PIPE\r\n\r\ndate = datetime.datetime.now().strftime('%Y%m%d-%s')\r\nf_date = datetime.datetime.now().strftime('%Y%m%d')\r\n\r\ndef backup_all_databases():\r\n    args = ['mysqldump', '-u', 'root', '-pPASSWORD', '--all-databases']\r\n    with open(\"%s.sql.gz\" % f_date, 'wb') as f:\r\n        p1 = Popen(args, stdout=PIPE)\r\n        p2 = Popen('gzip', stdin=p1.stdout, stdout=f)\r\n        p1.stdout.close()\r\n        p2.wait()\r\n        p1.wait()\r\n\r\ndef tar_html_folder():\r\n    output_filename_1 = \"%s.html_dir\"  % f_date\r\n    output_filename_2 = \"%s.html_dir.zip\"  % f_date\r\n    dir_name = '\/var\/www\/html'\r\n    dst = \"%s\" % date\r\n    shutil.make_archive(output_filename_1, 'zip', dir_name)\r\n    shutil.move(output_filename_2, dst)\r\n\r\ndef main():\r\n    archive_path = \"%s\" % date\r\n    os.mkdir(archive_path, 0755)\r\n    backup_all_databases()\r\n    src_file = \"%s.sql.gz\" % f_date\r\n    dst = \"%s\" % date\r\n    shutil.move(src_file, dst)\r\n    tar_html_folder()\r\n\r\nif __name__ == \"__main__\":\r\n    main()\r\n<\/pre>\n<pre class=\"theme:solarized-dark lang:default decode:true \" >\r\n# Lets execute the script and check the contents of the directory it creates. \r\n[23:02:41] root@jasonralph:~\/py_backup_site_full # python wordpress_backup.py\r\n-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.\r\n\r\n# Long listing of the directoy.\r\n[23:03:47] root@jasonralph:~\/py_backup_site_full # ls -l\r\ntotal 8\r\ndrwxr-xr-x 2 root root 4096 May  7 23:03 20160507-1462676621\r\n-rwxr-xr-x 1 root root 1059 May  7 23:02 wordpress_backup.py\r\n\r\n# Change directory to the newly created folder. \r\n[23:03:50] root@jasonralph:~\/py_backup_site_full # cd 20160507-1462676621\/\r\n\r\n# Long listing of the files in the newly created folder. \r\n[23:03:55] root@jasonralph:~\/py_backup_site_full\/20160507-1462676621 # ls -l\r\ntotal 79084\r\n-rw-r--r-- 1 root root 80614105 May  7 23:03 20160507.html_dir.zip\r\n-rw-r--r-- 1 root root   360709 May  7 23:03 20160507.sql.gz\r\n<\/pre>\n<p>So you can see we generated 2 files in a dated directory, I chose to use both zip and gunzip for compression algoritims. To view the contents you can run the normal linux commands to extract the files. <\/p>\n<pre class=\"theme:solarized-dark lang:default decode:true \" >\r\n# unzipping the files using gunzip and zip\r\n[23:03:55] root@jasonralph:~\/py_backup_site_full\/20160507-1462676621 # unzip 20160507.html_dir.zip && gunzip 20160507.sql.gz\r\n\r\n# Long listing of the files \r\n[23:08:31] root@jasonralph:~\/py_backup_site_full\/20160507-1462676621 # ls -l\r\ntotal 80660\r\n-rw-r--r-- 1 root root 80614105 May  7 23:03 20160507.html_dir.zip\r\n-rw-r--r-- 1 root root  1973075 May  7 23:03 20160507.sql\r\ndrwxr-xr-x 3 root root     4096 May  7 23:08 jasonralph\r\n\r\n# Head command showing the first 10 lines of the sql file. \r\n[23:11:33] root@jasonralph:~\/py_backup_site_full\/20160507-1462676621 # head -n 10 20160507.sql\r\n-- MySQL dump 10.13  Distrib 5.1.73, for redhat-linux-gnu (x86_64)\r\n--\r\n-- Host: localhost    Database:\r\n-- ------------------------------------------------------\r\n-- Server version       5.1.73\r\n\r\n\/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT *\/;\r\n\/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS *\/;\r\n\/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION *\/;\r\n\/*!40101 SET NAMES utf8 *\/;\r\n\r\n# Long listing of the jasonralph folder in \/var\/www\/html\r\n[23:12:48] root@jasonralph:~\/py_backup_site_full\/20160507-1462676621 # ls jasonralph\/wordpress\/\r\nblack_cat_blink.gif  image.jpg    MW_Line.mp4  rh.jpg               wp-admin              wp-config.php         wp-content   wp-links-opml.php  wp-mail.php      wp-settings.php   xmlrpc.php\r\ndelete_file.mvg      index.php    out.png      tux_linux_white.jpg  wp-blog-header.php    wp-config.php.bkup    wp-cron.php  wp-load.php        wp-pass.php      wp-signup.php\r\nexpolit.svg          license.txt  readme.html  wp-activate.php      wp-comments-post.php  wp-config-sample.php  wp-includes  wp-login.php       wp-register.php  wp-trackback.php\r\n\r\n<\/pre>\n<p>So there you have it, I can tar up the entire dated directory for easy offsite backup now of my entire site jasonralph.org.  Hope this helps someone, feel free to copy the source code and change at will. <\/p>\n<p>Best,<br \/>\nJason<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have this blog hosted on a LINODE dedicated LINUX server. It&#8217;s about 10 dollars a month for a 1 core system with about 250GB of disk space and 1GB of RAM, this server runs the common LAMP stack, I needed a quick and dirty script to backup MYSQL database and the PHP code contained [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,4],"tags":[46,44,12,24,45,43],"class_list":["post-477","post","type-post","status-publish","format-standard","hentry","category-coding-thoughts","category-python","tag-backup","tag-linode","tag-linux","tag-python-2","tag-script","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=477"}],"version-history":[{"count":13,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/477\/revisions"}],"predecessor-version":[{"id":490,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/477\/revisions\/490"}],"wp:attachment":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}