mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2024-12-05 02:19:00 +08:00
88 lines
2.3 KiB
Go
88 lines
2.3 KiB
Go
package client
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
"github.com/1Panel-dev/1Panel/backend/init/db"
|
|
"github.com/1Panel-dev/1Panel/backend/init/log"
|
|
"github.com/1Panel-dev/1Panel/backend/init/viper"
|
|
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
|
)
|
|
|
|
func TestCron(t *testing.T) {
|
|
viper.Init()
|
|
log.Init()
|
|
db.Init()
|
|
|
|
var backup model.BackupAccount
|
|
if err := global.DB.Where("id = ?", 2).First(&backup).Error; err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
varMap := make(map[string]interface{})
|
|
if err := json.Unmarshal([]byte(backup.Vars), &varMap); err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
varMap["type"] = backup.Type
|
|
varMap["bucket"] = backup.Bucket
|
|
switch backup.Type {
|
|
case constant.Sftp:
|
|
varMap["password"] = backup.Credential
|
|
case constant.OSS, constant.S3, constant.MinIo:
|
|
varMap["secretKey"] = backup.Credential
|
|
}
|
|
endpoint := varMap["endpoint"].(string)
|
|
accessKey := varMap["accessKey"].(string)
|
|
secretKey := varMap["secretKey"].(string)
|
|
client, err := oss.New(endpoint, accessKey, secretKey)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
bucket, err := client.Bucket(backup.Bucket)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
lor, err := bucket.ListObjects(oss.Prefix("directory/directory-test1/"))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
fmt.Println("my objects:", getObjectsFormResponse(lor))
|
|
|
|
name := "directory/directory-test1/20220928104331.tar.gz"
|
|
targetPath := constant.DownloadDir + "directory/directory-test1/"
|
|
if _, err := os.Stat(targetPath); err != nil && os.IsNotExist(err) {
|
|
if err = os.MkdirAll(targetPath, os.ModePerm); err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
if err := bucket.GetObjectToFile(name, targetPath+"20220928104231.tar.gz"); err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
|
|
func TestDir(t *testing.T) {
|
|
files, err := ioutil.ReadDir("/opt/1Panel/task/directory/directory-test1-3")
|
|
if len(files) <= 10 {
|
|
return
|
|
}
|
|
for i := 0; i < len(files)-10; i++ {
|
|
os.Remove("/opt/1Panel/task/directory/directory-test1-3/" + files[i].Name())
|
|
}
|
|
fmt.Println(files, err)
|
|
}
|
|
|
|
func getObjectsFormResponse(lor oss.ListObjectsResult) string {
|
|
var output string
|
|
for _, object := range lor.Objects {
|
|
output += object.Key + " "
|
|
}
|
|
return output
|
|
}
|