Skip to content

Commit cc4385b

Browse files
Added Command to Backup Files
1 parent 709ab46 commit cc4385b

File tree

6 files changed

+114
-5
lines changed

6 files changed

+114
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ coverage.xml
1313
dist
1414
.env
1515
*.db
16-
src/masonite_backup.egg-info
16+
src/masonite_backup.egg-info
17+
storage

src/backup/Backup.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
import pathlib
3+
import shutil
4+
import tempfile
5+
from unittest.mock import patch
6+
from masonite.configuration import config
7+
from shutil import make_archive
8+
from datetime import datetime
9+
from masonite.utils.location import base_path
10+
11+
class Backup:
12+
13+
def __init__(self, application) -> None:
14+
self.app = application
15+
self.backup_config = config("backup")
16+
17+
def accept(self, path):
18+
for pattern in self.backup_config.get("source").get("excludes"):
19+
if pattern in path:
20+
return False
21+
return True
22+
23+
def database(self):
24+
"""
25+
Backup the database.
26+
"""
27+
pass
28+
29+
def files(self):
30+
"""
31+
Backup the files.
32+
"""
33+
filename = self.backup_config.get("filename", "backup") + "-" + str(datetime.timestamp(datetime.now()))
34+
# make_archive(filename, "zip", self.backup_config.get("source").get("includes"))
35+
36+
output_dir = base_path("storage/backup")
37+
38+
if not pathlib.Path(output_dir).exists():
39+
pathlib.Path(output_dir).mkdir(parents=True, exist_ok=True)
40+
41+
path_to_archive = pathlib.Path(output_dir).joinpath(filename)
42+
43+
with tempfile.TemporaryDirectory() as tmp:
44+
shutil.copytree(
45+
self.backup_config.get("source").get("root"),
46+
pathlib.Path(tmp).joinpath('temporary_backup'),
47+
ignore=shutil.ignore_patterns(*self.backup_config.get("source").get("excludes"))
48+
)
49+
50+
with patch('os.path.isfile', side_effect=self.accept):
51+
make_archive(path_to_archive, "zip", tmp)
52+
print("ok")
53+
54+
print("final")
55+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from masonite.commands import Command
2+
3+
4+
class BackupRunCommand(Command):
5+
"""
6+
Start the backup process.
7+
8+
backup:run
9+
{--filename : Backup filename}
10+
{--only-db : Backup database only}
11+
{--only-files : Backup files only}
12+
"""
13+
14+
def __init__(self, application):
15+
super().__init__()
16+
self.app = application
17+
18+
def handle(self):
19+
if not self.validate_options():
20+
return
21+
22+
self.info("Starting backup...")
23+
24+
if self.option("only-db"):
25+
self.app.make("backup").database()
26+
elif self.option("only-files"):
27+
self.app.make("backup").files()
28+
29+
self.info("Backup complete...")
30+
31+
def validate_options(self):
32+
if self.option("only-db") and self.option("only-files"):
33+
self.error("You can only pass either files or database backup option.")
34+
return False
35+
return True

src/backup/commands/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .BackupRunCommand import BackupRunCommand

src/backup/config/backup.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
"""Backup Settings"""
22

3+
from masonite.utils.location import base_path
4+
35
"""
46
|--------------------------------------------------------------------------
5-
| A Heading of The Setting Being Set
7+
| Masonite Backup
68
|--------------------------------------------------------------------------
79
|
8-
| A quick description
10+
| This is the configuration file for the Masonite Backup package.
911
|
1012
"""
1113

12-
SETTING = "some value"
14+
FILENAME = "backup"
15+
DIRECTORY = "backup"
16+
SOURCE = {
17+
"root": base_path(),
18+
"excludes": [
19+
".git",
20+
"storage",
21+
"venv",
22+
"node_modules",
23+
"__pycache__",
24+
],
25+
}

src/backup/providers/BackupProvider.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""A BackupProvider Service Provider."""
22

33
from masonite.packages import PackageProvider
4-
4+
from ..Backup import Backup
5+
from ..commands import BackupRunCommand
56

67
class BackupProvider(PackageProvider):
78

@@ -15,6 +16,9 @@ def configure(self):
1516

1617
def register(self):
1718
super().register()
19+
20+
self.application.bind("backup", Backup(application=self.application))
21+
self.application.make("commands").add(BackupRunCommand(application=self.application))
1822

1923
def boot(self):
2024
"""Boots services required by the container."""

0 commit comments

Comments
 (0)