Skip to content

Commit b1a743f

Browse files
committed
Refactor
1 parent 8252a5e commit b1a743f

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

azure/azure.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (c *StorageClient) UploadFile(container string, fileName string, data []byt
7575
}
7676

7777
// UploadFileFromDisk Uploads a file to azure from disk
78-
func (c *StorageClient) UploadFileFromDisk(container string, name string, path string, progress pipeline.ProgressReceiver) error {
78+
func (c *StorageClient) UploadFileFromDisk(container string, name string, path string, tier azblob.AccessTierType) error {
7979
containerURL := c.createContainerURL(container)
8080
blobURL := containerURL.NewBlockBlobURL(name)
8181
ctx := context.WithoutCancel(context.Background())
@@ -89,9 +89,9 @@ func (c *StorageClient) UploadFileFromDisk(container string, name string, path s
8989
defer file.Close()
9090

9191
_, err = azblob.UploadFileToBlockBlob(ctx, file, blobURL, azblob.UploadToBlockBlobOptions{
92-
BlockSize: 4 * 1024 * 1024,
93-
Parallelism: 16,
94-
Progress: progress,
92+
BlockSize: 4 * 1024 * 1024,
93+
Parallelism: 16,
94+
BlobAccessTier: tier,
9595
})
9696

9797
if err != nil {

cmd/console/commands/database_backup.go

+24-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commands
33
import (
44
"bytes"
55
"fmt"
6+
"github.com/Azure/azure-storage-blob-go/azblob"
67
"github.com/Quaver/api2/azure"
78
"github.com/Quaver/api2/config"
89
"github.com/Quaver/api2/webhooks"
@@ -23,49 +24,37 @@ var DatabaseBackupCmd = &cobra.Command{
2324
Use: "backup:database",
2425
Short: "Backs up the database and uploads to azure",
2526
Run: func(cmd *cobra.Command, args []string) {
26-
currentTime := time.Now()
27-
2827
if err := deletePreviousBackups(); err != nil {
2928
logrus.Error(err)
29+
_ = webhooks.SendBackupWebhook(false, err)
3030
return
3131
}
3232

3333
backupDir := fmt.Sprintf("%v/backups", config.Instance.Cache.DataDirectory)
34+
path, _ := filepath.Abs(fmt.Sprintf("%v/backup.sql", backupDir))
3435

3536
if err := os.MkdirAll(backupDir, os.ModePerm); err != nil {
3637
logrus.Error("[Database Backup] Error creating backup directory", err)
3738
_ = webhooks.SendBackupWebhook(false, err)
3839
return
3940
}
4041

41-
path, _ := filepath.Abs(fmt.Sprintf("%v/backup.sql", backupDir))
42-
4342
if err := os.Remove(path); err != nil {
4443
logrus.Error("[Database Backup] Error deleting existing backup")
4544
}
4645

47-
logrus.Info("[Database Backup] Dumping database...")
48-
4946
if err := dumpDatabase(path); err != nil {
5047
logrus.Error("[Database Backup] Error dumping database: ", err)
5148
_ = webhooks.SendBackupWebhook(false, err)
5249
return
5350
}
5451

55-
logrus.Info("[Database Backup] Finished dumping database at path: ", path)
56-
logrus.Info("[Database Backup] Uploading to azure...: ", path)
57-
58-
fileName := fmt.Sprintf("%d-%d-%d-time-%d-%d.sql", currentTime.Year(), currentTime.Month(), currentTime.Day(), currentTime.Hour(), currentTime.Minute())
59-
60-
err := azure.Client.UploadFileFromDisk(databaseBackupContainer, fileName, path, nil)
61-
62-
if err != nil {
52+
if err := uploadToAzure(path); err != nil {
6353
logrus.Error("[Database Backup] Error uploading database backup", err)
6454
_ = webhooks.SendBackupWebhook(false, err)
6555
return
6656
}
6757

68-
logrus.Info("[Database Backup] Database backup complete!")
6958
_ = webhooks.SendBackupWebhook(true)
7059
},
7160
}
@@ -90,6 +79,8 @@ func deletePreviousBackups() error {
9079
}
9180

9281
func dumpDatabase(path string) error {
82+
logrus.Info("[Database Backup] Dumping database...")
83+
9384
hostSplit := strings.Split(config.Instance.SQL.Host, ":")
9485

9586
cmd := exec.Command(
@@ -117,5 +108,23 @@ func dumpDatabase(path string) error {
117108
return fmt.Errorf("%v\n\n```%v```", err, stderr.String())
118109
}
119110

111+
logrus.Info("[Database Backup] Finished dumping database at path: ", path)
112+
return nil
113+
}
114+
115+
func uploadToAzure(path string) error {
116+
logrus.Info("[Database Backup] Uploading to azure...:")
117+
118+
currentTime := time.Now()
119+
120+
fileName := fmt.Sprintf("%d-%d-%d-time-%d-%d.sql", currentTime.Year(), currentTime.Month(), currentTime.Day(), currentTime.Hour(), currentTime.Minute())
121+
122+
err := azure.Client.UploadFileFromDisk(databaseBackupContainer, fileName, path, azblob.AccessTierHot)
123+
124+
if err != nil {
125+
return err
126+
}
127+
128+
logrus.Info("[Database Backup] Finished uploading to azure!")
120129
return nil
121130
}

0 commit comments

Comments
 (0)