-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
758 additions
and
24 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,76 @@ | ||
// Package services 备份服务 | ||
package services | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"github.com/goravel/framework/support/carbon" | ||
|
||
"panel/app/models" | ||
"panel/packages/helper" | ||
) | ||
|
||
type Backup interface { | ||
BackupWebSite(string) error | ||
BackupDatabase(string, string) error | ||
WebsiteList() ([]BackupFile, error) | ||
WebSiteBackup(website models.Website) error | ||
} | ||
|
||
type BackupFile struct { | ||
Name string `json:"name"` | ||
Size string `json:"size"` | ||
} | ||
|
||
type BackupImpl struct { | ||
setting Setting | ||
} | ||
|
||
func NewBackupImpl() *BackupImpl { | ||
return &BackupImpl{ | ||
setting: NewSettingImpl(), | ||
} | ||
} | ||
|
||
func (s *BackupImpl) WebsiteList() ([]BackupFile, error) { | ||
path := s.setting.Get(models.SettingKeyBackupPath) | ||
if len(path) == 0 { | ||
return []BackupFile{}, nil | ||
} | ||
|
||
path += "/website" | ||
|
||
files, err := os.ReadDir(path) | ||
if err != nil { | ||
return []BackupFile{}, err | ||
} | ||
var backupList []BackupFile | ||
for _, file := range files { | ||
info, err := file.Info() | ||
if err != nil { | ||
continue | ||
} | ||
backupList = append(backupList, BackupFile{ | ||
Name: file.Name(), | ||
Size: helper.FormatBytes(float64(info.Size())), | ||
}) | ||
} | ||
|
||
return backupList, nil | ||
} | ||
|
||
func (s *BackupImpl) WebSiteBackup(website models.Website) error { | ||
backupPath := s.setting.Get(models.SettingKeyBackupPath) | ||
if len(backupPath) == 0 { | ||
return errors.New("未正确配置备份路径") | ||
} | ||
|
||
backupPath += "/website" | ||
if !helper.Exists(backupPath) { | ||
helper.Mkdir(backupPath, 0644) | ||
} | ||
|
||
backupFile := backupPath + "/" + website.Name + carbon.Now().ToShortDateTimeString() + ".zip" | ||
helper.ExecShell("cd " + website.Path + " && zip -r " + backupFile + " .") | ||
|
||
return nil | ||
} |
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
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
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
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
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
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