-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron_images_backup.go
65 lines (57 loc) · 1.83 KB
/
cron_images_backup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"context"
"os"
"github.com/kothar/go-backblaze"
"github.com/mholt/archiver/v4"
)
// Make comprasion of database to gzip file
func zip_images() {
files, _ := archiver.FilesFromDisk(nil, map[string]string{
config.PATH + "/images": "",
})
to, _ := os.Create(config.PATH + "/cron_dump/images.tar.gz")
format := archiver.CompressedArchive{
Compression: archiver.Gz{},
Archival: archiver.Tar{},
}
err := format.Archive(context.Background(), to, files)
to.Close()
if err == nil {
upload_images_zip_to_backblaze()
}
}
/*
1. Open connection to backblaze
2. Getting fielId that was uploaded before to delete him
3. Upload the new file
4. After upload i save the fileId of uploaded file in txt document to delete it later
*/
func upload_images_zip_to_backblaze() {
b2, connection_error := backblaze.NewB2(backblaze.Credentials{
AccountID: config.BACKBLAZE_ID,
ApplicationKey: config.BACKBLAZE_KEY,
})
if connection_error == nil {
b2.CreateBucket(config.PRODUCT_NAME, backblaze.AllPrivate)
bucket, bucket_error := b2.Bucket(config.PRODUCT_NAME)
if bucket_error == nil {
old_file_id, err := os.Open(config.PATH + "/cron_dump/images_dump_id.txt")
if err == nil {
content, _ := os.ReadFile(config.PATH + "/cron_dump/images_dump_id.txt")
file_id := string(content)
old_file_id.Close()
bucket.DeleteFileVersion("images.tar.gz", file_id)
}
reader, _ := os.Open(config.PATH + "/cron_dump/images.tar.gz")
metadata := make(map[string]string)
res, _ := bucket.UploadFile("images.tar.gz", metadata, reader)
reader.Close()
os.Remove(config.PATH + "/cron_dump/images_dump_id.txt")
new_file_id, _ := os.Create(config.PATH + "/cron_dump/images_dump_id.txt")
new_file_id.WriteString(res.ID)
new_file_id.Close()
os.RemoveAll(config.PATH + "/cron_dump/images.tar.gz")
}
}
}