99from masonite .utils .location import base_path
1010import subprocess
1111import gzip
12+ from masonite .facades import Mail
13+ from .mailables .Backup import Backup as BackupMailable
1214
1315
1416class Backup :
1517 def __init__ (self , application ) -> None :
1618 self .app = application
1719 self .backup_config = config ("backup" )
1820
21+ self .db_file_path = None
22+ self .archive_file_path = None
23+
1924 def accept (self , path ):
2025 for pattern in self .backup_config .get ("source" ).get ("excludes" ):
2126 if pattern in path :
@@ -32,7 +37,7 @@ def database(self):
3237 connection = db_config .get (default )
3338 driver = connection .get ("driver" )
3439
35- db_file_path = base_path (
40+ self . db_file_path = base_path (
3641 "{}.gz" .format ("database-" + str (datetime .timestamp (datetime .now ())))
3742 )
3843
@@ -54,7 +59,7 @@ def database(self):
5459 command_str = f"sqlplus -S{ connection .get ('user' )} /{ connection .get ('password' )} @{ connection .get ('host' )} :{ connection .get ('port' )} /{ connection .get ('database' )} "
5560
5661 if command_str :
57- with gzip .open (db_file_path , "wb" ) as f :
62+ with gzip .open (self . db_file_path , "wb" ) as f :
5863 popen = subprocess .Popen (
5964 [command_str ],
6065 stdout = subprocess .PIPE ,
@@ -65,7 +70,7 @@ def database(self):
6570 f .write (stdout_line .encode ("utf-8" ))
6671 popen .stdout .close ()
6772 popen .wait ()
68- return db_file_path
73+ return self . db_file_path
6974
7075 def files (self ):
7176 """
@@ -82,7 +87,7 @@ def files(self):
8287 if not pathlib .Path (output_dir ).exists ():
8388 pathlib .Path (output_dir ).mkdir (parents = True , exist_ok = True )
8489
85- path_to_archive = pathlib .Path (output_dir ).joinpath (filename )
90+ self . archive_file_path = pathlib .Path (output_dir ).joinpath (filename )
8691
8792 with tempfile .TemporaryDirectory () as tmp :
8893 shutil .copytree (
@@ -92,5 +97,36 @@ def files(self):
9297 )
9398
9499 with patch ("os.path.isfile" , side_effect = self .accept ):
95- make_archive (path_to_archive , "zip" , tmp )
96- return path_to_archive
100+ make_archive (self .archive_file_path , "zip" , tmp )
101+
102+ return f"{ self .archive_file_path } .zip"
103+
104+ def email (self ):
105+ """
106+ Email the backup.
107+ """
108+
109+ if not self .backup_config .get ("email_backup" , False ):
110+ return
111+
112+ if self .archive_file_path != None and pathlib .Path (f"{ self .archive_file_path } .zip" ).exists ():
113+ Mail .mailable (
114+ BackupMailable ().attach (f"System Backup.zip" , f"{ self .archive_file_path } .zip" )
115+ ).send ()
116+
117+ if self .db_file_path != None and pathlib .Path (self .db_file_path ).exists ():
118+ ext = self .db_file_path .split ("." )[- 1 ]
119+ Mail .mailable (
120+ BackupMailable ().attach (f"Database Backup.{ ext } " , self .db_file_path )
121+ ).send ()
122+
123+ self .cleanup ()
124+
125+ def cleanup (self ):
126+ """
127+ Cleanup the backup files.
128+ """
129+ if self .archive_file_path != None and pathlib .Path (self .archive_file_path ).exists ():
130+ pathlib .Path (self .archive_file_path ).unlink ()
131+ if self .db_file_path != None and pathlib .Path (self .db_file_path ).exists ():
132+ pathlib .Path (self .db_file_path ).unlink ()
0 commit comments