I have this blog hosted on a LINODE dedicated LINUX server. It’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.
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 |
# Linux server kernel version details. [22:47:51] root@jasonralph:~/py_backup_site_full # uname -a Linux 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 # Linux distro information [22:52:15] root@jasonralph:~/py_backup_site_full # cat /etc/centos-release CentOS release 6.7 (Final) # Free output [22:59:58] root@jasonralph:~/py_backup_site_full # free -m total used free shared buffers cached Mem: 991 796 195 0 62 329 -/+ buffers/cache: 403 587 Swap: 511 0 511 # Here you can see the python binary location on the filesystem. [22:46:36] root@jasonralph:~/py_backup_site_full # which python /usr/local/bin/python # Now you can see the python version I am using. [22:46:50] root@jasonralph:~/py_backup_site_full # python --version Python 2.7.6 # Long listing of the backup folder I have setup. [22:47:48] root@jasonralph:~/py_backup_site_full # ls -l total 4 -rwxr-xr-x 1 root root 1560 May 7 22:46 wordpress_backup.py |
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 39 40 41 |
# cat of the python script to see the source code. [23:02:39] root@jasonralph:~/py_backup_site_full # cat wordpress_backup.py #!/usr/local/bin/python import optparse import os import datetime import shutil from subprocess import Popen, PIPE date = datetime.datetime.now().strftime('%Y%m%d-%s') f_date = datetime.datetime.now().strftime('%Y%m%d') def backup_all_databases(): args = ['mysqldump', '-u', 'root', '-pPASSWORD', '--all-databases'] with open("%s.sql.gz" % f_date, 'wb') as f: p1 = Popen(args, stdout=PIPE) p2 = Popen('gzip', stdin=p1.stdout, stdout=f) p1.stdout.close() p2.wait() p1.wait() def tar_html_folder(): output_filename_1 = "%s.html_dir" % f_date output_filename_2 = "%s.html_dir.zip" % f_date dir_name = '/var/www/html' dst = "%s" % date shutil.make_archive(output_filename_1, 'zip', dir_name) shutil.move(output_filename_2, dst) def main(): archive_path = "%s" % date os.mkdir(archive_path, 0755) backup_all_databases() src_file = "%s.sql.gz" % f_date dst = "%s" % date shutil.move(src_file, dst) tar_html_folder() if __name__ == "__main__": main() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Lets execute the script and check the contents of the directory it creates. [23:02:41] root@jasonralph:~/py_backup_site_full # python wordpress_backup.py -- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly. # Long listing of the directoy. [23:03:47] root@jasonralph:~/py_backup_site_full # ls -l total 8 drwxr-xr-x 2 root root 4096 May 7 23:03 20160507-1462676621 -rwxr-xr-x 1 root root 1059 May 7 23:02 wordpress_backup.py # Change directory to the newly created folder. [23:03:50] root@jasonralph:~/py_backup_site_full # cd 20160507-1462676621/ # Long listing of the files in the newly created folder. [23:03:55] root@jasonralph:~/py_backup_site_full/20160507-1462676621 # ls -l total 79084 -rw-r--r-- 1 root root 80614105 May 7 23:03 20160507.html_dir.zip -rw-r--r-- 1 root root 360709 May 7 23:03 20160507.sql.gz |
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.
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 |
# unzipping the files using gunzip and zip [23:03:55] root@jasonralph:~/py_backup_site_full/20160507-1462676621 # unzip 20160507.html_dir.zip && gunzip 20160507.sql.gz # Long listing of the files [23:08:31] root@jasonralph:~/py_backup_site_full/20160507-1462676621 # ls -l total 80660 -rw-r--r-- 1 root root 80614105 May 7 23:03 20160507.html_dir.zip -rw-r--r-- 1 root root 1973075 May 7 23:03 20160507.sql drwxr-xr-x 3 root root 4096 May 7 23:08 jasonralph # Head command showing the first 10 lines of the sql file. [23:11:33] root@jasonralph:~/py_backup_site_full/20160507-1462676621 # head -n 10 20160507.sql -- MySQL dump 10.13 Distrib 5.1.73, for redhat-linux-gnu (x86_64) -- -- Host: localhost Database: -- ------------------------------------------------------ -- Server version 5.1.73 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; # Long listing of the jasonralph folder in /var/www/html [23:12:48] root@jasonralph:~/py_backup_site_full/20160507-1462676621 # ls jasonralph/wordpress/ black_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 delete_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 expolit.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 |
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.
Best,
Jason