2020-04-14 05:01:07 +00:00
|
|
|
package backup
|
|
|
|
|
|
|
|
import (
|
2020-11-28 23:57:10 +00:00
|
|
|
"errors"
|
2020-04-14 05:01:07 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2020-04-17 20:46:36 +00:00
|
|
|
type LocalBackup struct {
|
2020-05-02 22:02:02 +00:00
|
|
|
Backup
|
2020-04-17 20:46:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 22:02:02 +00:00
|
|
|
var _ BackupInterface = (*LocalBackup)(nil)
|
2020-04-17 20:46:36 +00:00
|
|
|
|
2021-01-18 05:05:51 +00:00
|
|
|
func NewLocal(uuid string, ignore string) *LocalBackup {
|
|
|
|
return &LocalBackup{
|
|
|
|
Backup{
|
|
|
|
Uuid: uuid,
|
|
|
|
Ignore: ignore,
|
|
|
|
adapter: LocalBackupAdapter,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 02:06:22 +00:00
|
|
|
// LocateLocal finds the backup for a server and returns the local path. This
|
|
|
|
// will obviously only work if the backup was created as a local backup.
|
2020-04-17 20:46:36 +00:00
|
|
|
func LocateLocal(uuid string) (*LocalBackup, os.FileInfo, error) {
|
|
|
|
b := &LocalBackup{
|
2020-05-02 22:02:02 +00:00
|
|
|
Backup{
|
2020-12-25 19:52:57 +00:00
|
|
|
Uuid: uuid,
|
|
|
|
Ignore: "",
|
2020-05-02 22:02:02 +00:00
|
|
|
},
|
2020-04-14 05:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
st, err := os.Stat(b.Path())
|
|
|
|
if err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return nil, nil, err
|
2020-04-14 05:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if st.IsDir() {
|
2020-09-01 03:24:07 +00:00
|
|
|
return nil, nil, errors.New("invalid archive, is directory")
|
2020-04-14 05:01:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b, st, nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 02:06:22 +00:00
|
|
|
// Remove removes a backup from the system.
|
2020-04-17 20:46:36 +00:00
|
|
|
func (b *LocalBackup) Remove() error {
|
|
|
|
return os.Remove(b.Path())
|
|
|
|
}
|
|
|
|
|
2021-01-17 02:06:22 +00:00
|
|
|
// WithLogContext attaches additional context to the log output for this backup.
|
2020-12-28 00:16:40 +00:00
|
|
|
func (b *LocalBackup) WithLogContext(c map[string]interface{}) {
|
|
|
|
b.logContext = c
|
|
|
|
}
|
|
|
|
|
2021-01-17 02:06:22 +00:00
|
|
|
// Generate generates a backup of the selected files and pushes it to the
|
|
|
|
// defined location for this instance.
|
2020-12-25 19:52:57 +00:00
|
|
|
func (b *LocalBackup) Generate(basePath, ignore string) (*ArchiveDetails, error) {
|
2021-01-18 05:05:51 +00:00
|
|
|
a := &Archive{
|
2020-12-25 19:52:57 +00:00
|
|
|
BasePath: basePath,
|
|
|
|
Ignore: ignore,
|
2020-04-17 20:46:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 00:16:40 +00:00
|
|
|
b.log().Info("creating backup for server...")
|
2020-12-25 19:52:57 +00:00
|
|
|
if err := a.Create(b.Path()); err != nil {
|
2020-11-28 23:57:10 +00:00
|
|
|
return nil, err
|
2020-05-10 02:24:30 +00:00
|
|
|
}
|
2020-12-28 00:16:40 +00:00
|
|
|
b.log().Info("created backup successfully")
|
2020-04-19 06:26:23 +00:00
|
|
|
|
2020-05-10 02:24:30 +00:00
|
|
|
return b.Details(), nil
|
2020-04-17 20:46:36 +00:00
|
|
|
}
|
2021-01-17 02:06:22 +00:00
|
|
|
|