-
Notifications
You must be signed in to change notification settings - Fork 0
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
191 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package backup | ||
|
||
import "github.com/gelleson/packup/pkg/database" | ||
|
||
type BackupService struct { | ||
database *database.Database | ||
} |
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,4 +1,78 @@ | ||
package backup | ||
|
||
import ( | ||
"github.com/gelleson/packup/pkg/compress" | ||
"github.com/pkg/errors" | ||
"gorm.io/gorm" | ||
"time" | ||
) | ||
|
||
type SourceType string | ||
|
||
const ( | ||
PostgresType SourceType = "postgres" | ||
MySQLType SourceType = "mysql" | ||
OracleType SourceType = "oracle" | ||
SqliteType SourceType = "sqlite" | ||
FileType SourceType = "file" | ||
) | ||
|
||
type ExecutionType string | ||
|
||
func (et ExecutionType) Validate() error { | ||
|
||
if et != OnceExecution && et != CronExecution { | ||
return errors.New("execution type should be equal to cron or once") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
const ( | ||
OnceExecution ExecutionType = "once" | ||
CronExecution ExecutionType = "cron" | ||
) | ||
|
||
type Template string | ||
|
||
const DefaultBackupTemplate Template = "{{ .Namespace }}/{{ .Name }}-{{ .Timestamp }}.{{ .Ext }}" | ||
|
||
type Backup struct { | ||
gorm.Model | ||
Name string `validate:"required"` | ||
Compress compress.Type `validate:"required"` | ||
ExecutionType ExecutionType `validate:"required"` | ||
ExecutionTime string `validate:"required"` | ||
Tag string `validate:"required"` | ||
Keystore string | ||
Bucket string `validate:"required"` | ||
BackupNameTemplate Template | ||
Namespace string | ||
Timezone string | ||
LastExecutionTime time.Time | ||
} | ||
|
||
type Status string | ||
|
||
const ( | ||
FailedStatus Status = "failed" | ||
OkStatus Status = "ok" | ||
) | ||
|
||
type History struct { | ||
gorm.Model | ||
Status Status | ||
Message string | ||
Size uint | ||
Tag string | ||
AgentID uint | ||
BackupID uint | ||
ExecutedAt time.Time | ||
} | ||
|
||
type Pending struct { | ||
gorm.Model | ||
BackupID uint | ||
Backup Backup | ||
ExecutionTime time.Time | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package keystore | ||
|
||
import "github.com/gelleson/packup/pkg/database" | ||
|
||
type Cipher string | ||
|
||
type KeystoreService struct { | ||
db *database.Database | ||
cipherKey string | ||
} | ||
|
||
func (s KeystoreService) Get(key string) (Credential, error) { | ||
|
||
cred := Credential{} | ||
|
||
if tx := s.db.Conn().Where("key = ?", key).First(&cred); tx.Error != nil { | ||
return Credential{}, tx.Error | ||
} | ||
|
||
return cred, nil | ||
} | ||
|
||
func (s KeystoreService) encrypt(c Credential) (Credential, error) { | ||
|
||
return Credential{}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package keystore | ||
|
||
import ( | ||
"github.com/gelleson/packup/pkg/validators" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Credential struct { | ||
gorm.DB | ||
Key string `validate:"required"` | ||
Username string | ||
Password string | ||
Host string | ||
Token string | ||
Database string | ||
} | ||
|
||
func (c Credential) Validate() error { | ||
return validators.Struct(c) | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
package cipher |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package cipher | ||
|
||
type Cipher string | ||
|
||
const ( | ||
AESCipher Cipher = "AES" | ||
RSACipher Cipher = "RSA" | ||
) |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package compress | ||
|
||
import ( | ||
"compress/gzip" | ||
"io" | ||
"os" | ||
) | ||
|
||
type compressObject struct { | ||
os.File | ||
} | ||
|
||
func (c compressObject) Close() error { | ||
|
||
if err := c.File.Close(); err != nil { | ||
return err | ||
} | ||
|
||
return os.Remove(c.Name()) | ||
} | ||
|
||
func Compress(compressType Type, body io.Reader, path string) (io.ReadCloser, error) { | ||
|
||
switch compressType { | ||
case GzipType: | ||
return gzipCompress(body) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func gzipCompress(body io.Reader) (io.ReadCloser, error) { | ||
|
||
object, err := gzip.NewReader(body) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var compressedObject compressObject | ||
|
||
if _, err := io.Copy(&compressedObject, object); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &compressedObject, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package compress | ||
|
||
type Type string | ||
|
||
const ( | ||
GzipType Type = "gzip" | ||
LZType Type = "lz" | ||
) |