Created
April 10, 2022 00:25
-
-
Save trafficinc/f753318f9ea459e4ee0cf84af8ac7f58 to your computer and use it in GitHub Desktop.
PHP Web Project Backup Python Script
This file contains 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/local/bin/python3 | |
""" | |
Back-up PHP web projects and not include the modules or vendor directories, will put back up in same DIR as script' | |
* need TAR installed on machine | |
To run: $ python3 backup_projects.py | |
""" | |
from datetime import datetime | |
from pathlib import Path | |
import os | |
import subprocess | |
projects = ('/Users/me/code/project',) | |
exclude_dirs = ("vendor", "node_modules") | |
def not_in_dirs(myfile): | |
if any(x in myfile.name for x in exclude_dirs): | |
return myfile.name | |
def build_cmd(project_name, backup_file_name, dirs, path): | |
cmd = ['tar', '-pczf', backup_file_name] | |
for dir in dirs: | |
cmd.append("--exclude") | |
cmd.append("*%s/%s*" % (project_name, dir)) | |
cmd.append("-P") | |
cmd.append(str(path)) | |
return cmd | |
def backup(project): | |
project_name = os.path.basename(os.path.normpath(project)) | |
print("Backing Up... " + project_name + " in `" + project + "`") | |
path = Path(project) | |
if path.exists(): | |
backup_file_name = f'backup-{datetime.now().strftime("%Y-%m-%d-%H%M%S")}-{path.name}.tgz' | |
if path.is_dir(): | |
cmd = build_cmd(project_name, backup_file_name, exclude_dirs, path) | |
with subprocess.Popen(cmd) as proc: | |
try: | |
proc.wait(timeout=15) | |
except subprocess.TimeoutExpired: | |
assert proc.returncode is None | |
proc.kill() | |
print("Done.") | |
else: | |
print("Path does not exist.") | |
if __name__ == "__main__": | |
for project in projects: | |
backup(project) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment