Backup WordPress website with Python script

This is very simple script to backup wordpress based website. It could be used to backup any LAMP based site as well. There are many ways to backup wordpress site. This particular python script is very simple. It cosists of few variables

backup_path = '/mnt/backup'  # where backup files should be placed
source_directorie = '/var/www/html/mysite.com' # where web root files located 
todays_date = (time.strftime("%d-%m-%Y"))  # todays date 
free_space_needed = 1000000 # in this case indicates 1G 
backup_site_name = 'mysite.com' # name of the site being backed up
database_name = 'mysitedb' # mysql database name
target_dir = '/mnt/backup/dir1/'  #directory where backup files will be placed
username = 'myusername' # mysql username for database
password = 'mypassword' # password

 
and 3 functions which will compress and tar webroot as well as do sql dump. If condition also exists to make sure at least 1G of space left on your backup drive.
Script below should be used as cron job. How to use crantab

#!/usr/bin/env python
"""
Title:   WordPress backup script
Author:  prolinuxhub.com
Date:	 Sun 27 2016
"""
import sys,os,statvfs
import tarfile
import time
# --- Variables --- #
backup_path = '/mnt/backup'
source_directorie = '/var/www/html/mysite.com'
todays_date = (time.strftime("%d-%m-%Y"))
free_space_needed = 1000000
backup_site_name = 'mysite.com'
database_name = 'mysitedb'
target_dir = '/mnt/backup/dir1/'
username = 'myusername'
password = 'mypassword'
######################
# --- Check to make sure backup directory has enough space ---#
def check_space_for_backup():
	size = os.statvfs(backup_path)
	free_space=(size.f_bavail * size.f_frsize) / 1024
	return free_space
# --- Backup web root --- #
def backup_webroot():
   os.system("tar -zcvf "+target_dir+todays_date+backup_site_name+".tar.gz "+source_directorie)
# --- Backup sql database --- #
def backup_database():
   os.system("mysqldump -u "+database_name+" -p"+password+" "+database_name+"  > "+target_dir+todays_date+database_name+".sql")
# --- MAIN --- # 
def main():
	if check_space_for_backup() > free_space_needed:
		backup_webroot()
		backup_database()
if __name__ == '__main__':
	main()