Created
August 24, 2018 08:47
-
-
Save maaddae/c303907b4fbc6a91ec712b645bbf0a10 to your computer and use it in GitHub Desktop.
A simple backup python script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Title: WordPress backup script | |
""" | |
import sys,os,statvfs | |
import tarfile | |
import time | |
# --- Variables --- # | |
backup_path = '/home/eklhenam/backup' | |
source_directorie = '/var/www/html/my-site-name' | |
todays_date = (time.strftime("%d-%m-%Y")) | |
free_space_needed = 1000000 | |
backup_site_name = 'my-site-name' | |
database_name = 'mysitedb' | |
target_dir = '/home/eklhenam/backup' | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment