backup

package
v1.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2026 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BackupStatusPending = "pending"
	BackupStatusRunning = "running"
	BackupStatusSuccess = "success"
	BackupStatusFailed  = "failed"
)

BackupStatus 备份状态常量

Variables

This section is empty.

Functions

func FormatFileSize

func FormatFileSize(size int64) string

FormatFileSize 格式化文件大小

func ValidateCronExpr

func ValidateCronExpr(cronExpr string) error

ValidateCronExpr 验证 cron 表达式

Types

type BackupConfig

type BackupConfig struct {
	ID             int64     `json:"id"`
	Enabled        bool      `json:"enabled"`
	Prefix         string    `json:"prefix"`          // 备份路径前缀
	CronExpr       string    `json:"cron_expr"`       // cron表达式
	RetentionCount int       `json:"retention_count"` // 保留数量
	DataDir        string    `json:"data_dir"`        // WuKongIM 数据目录
	CreatedAt      time.Time `json:"created_at"`
	UpdatedAt      time.Time `json:"updated_at"`
}

BackupConfig 备份配置模型(存储配置复用 ctx.GetConfig().COS)

type BackupConfigReq

type BackupConfigReq struct {
	Enabled        *bool  `json:"enabled,omitempty"`
	Prefix         string `json:"prefix,omitempty"`
	CronExpr       string `json:"cron_expr,omitempty"`
	RetentionCount *int   `json:"retention_count,omitempty"`
	DataDir        string `json:"data_dir,omitempty"`
}

BackupConfigReq 备份配置请求

type BackupConfigResp

type BackupConfigResp struct {
	Enabled        bool   `json:"enabled"`
	Prefix         string `json:"prefix"`
	CronExpr       string `json:"cron_expr"`
	RetentionCount int    `json:"retention_count"`
	DataDir        string `json:"data_dir"`
	// 以下字段从 ctx.GetConfig() 读取,只读展示
	StorageType string `json:"storage_type"`
	Bucket      string `json:"bucket"`
	Region      string `json:"region"`
}

BackupConfigResp 备份配置响应

type BackupHistory

type BackupHistory struct {
	ID           int64      `json:"id"`
	BackupID     string     `json:"backup_id"`
	Status       string     `json:"status"` // pending/running/success/failed
	FileName     string     `json:"file_name"`
	FileSize     int64      `json:"file_size"`
	StoragePath  string     `json:"storage_path"`
	StartedAt    *time.Time `json:"started_at"`
	FinishedAt   *time.Time `json:"finished_at"`
	ErrorMessage string     `json:"error_message"`
	CreatedAt    time.Time  `json:"created_at"`
	UpdatedAt    time.Time  `json:"updated_at"`
}

BackupHistory 备份历史模型

type BackupHistoryResp

type BackupHistoryResp struct {
	ID           int64  `json:"id"`
	BackupID     string `json:"backup_id"`
	Status       string `json:"status"`
	FileName     string `json:"file_name"`
	FileSize     int64  `json:"file_size"`
	FileSizeStr  string `json:"file_size_str"`
	StoragePath  string `json:"storage_path"`
	StartedAt    string `json:"started_at,omitempty"`
	FinishedAt   string `json:"finished_at,omitempty"`
	ErrorMessage string `json:"error_message,omitempty"`
	CreatedAt    string `json:"created_at"`
}

BackupHistoryResp 备份历史响应

type COSStorage

type COSStorage struct {
	log.Log
	// contains filtered or unexported fields
}

COSStorage 腾讯云 COS 存储(通过 S3 兼容协议)

func NewCOSStorage

func NewCOSStorage(cfg *StorageConfig) (*COSStorage, error)

NewCOSStorage 创建 COS 存储实例

func (*COSStorage) Delete

func (s *COSStorage) Delete(ctx context.Context, remotePath string) error

Delete 删除 COS 上的文件

func (*COSStorage) GetPresignedURL

func (s *COSStorage) GetPresignedURL(ctx context.Context, remotePath string, expires time.Duration) (string, error)

GetPresignedURL 获取预签名下载 URL

func (*COSStorage) List

func (s *COSStorage) List(ctx context.Context, prefix string) ([]string, error)

List 列出前缀下的文件

func (*COSStorage) TestConnection

func (s *COSStorage) TestConnection(ctx context.Context) error

TestConnection 测试 COS 连接

func (*COSStorage) Upload

func (s *COSStorage) Upload(ctx context.Context, localPath, remotePath string) error

Upload 上传文件到 COS

type DiskChecker

type DiskChecker struct{}

DiskChecker 磁盘空间检查器

func NewDiskChecker

func NewDiskChecker() *DiskChecker

NewDiskChecker 创建磁盘检查器

func (*DiskChecker) CheckAvailableSpace

func (d *DiskChecker) CheckAvailableSpace(path string, requiredBytes int64) (available int64, sufficient bool, err error)

CheckAvailableSpace 检查目录可用空间是否满足需求 path: 要检查的目录路径 requiredBytes: 需要的字节数 返回: 可用空间(bytes), 是否满足需求, error

func (*DiskChecker) CheckBeforeBackup

func (d *DiskChecker) CheckBeforeBackup(sourcePath, tempPath string) error

CheckBeforeBackup 备份前检查磁盘空间 sourcePath: 源数据目录 tempPath: 临时文件目录 (通常是 os.TempDir()) 返回: error 如果空间不足

func (*DiskChecker) EstimateArchiveSize

func (d *DiskChecker) EstimateArchiveSize(sourcePath string, compressionRatio float64) (int64, error)

EstimateArchiveSize 估算压缩包大小 sourcePath: 源目录路径 compressionRatio: 压缩比 (0.0-1.0, 例如 0.5 表示压缩后为原大小的 50%)

type IStorage

type IStorage interface {
	// Upload 上传文件
	Upload(ctx context.Context, localPath, remotePath string) error
	// Delete 删除文件
	Delete(ctx context.Context, remotePath string) error
	// GetPresignedURL 获取预签名下载URL
	GetPresignedURL(ctx context.Context, remotePath string, expires time.Duration) (string, error)
	// TestConnection 测试连接
	TestConnection(ctx context.Context) error
	// List 列出文件
	List(ctx context.Context, prefix string) ([]string, error)
}

IStorage 存储接口

type Manager

type Manager struct {
	log.Log
	// contains filtered or unexported fields
}

Manager 备份管理

func NewManager

func NewManager(ctx *config.Context) *Manager

NewManager 创建备份管理

func (*Manager) Route

func (m *Manager) Route(r *wkhttp.WKHttp)

Route 配置路由规则

type ProgressReader

type ProgressReader struct {
	// contains filtered or unexported fields
}

ProgressReader 进度读取器

func NewProgressReader

func NewProgressReader(reader io.Reader, total int64, onProgress func(read, total int64)) *ProgressReader

func (*ProgressReader) Read

func (pr *ProgressReader) Read(p []byte) (int, error)

type Scheduler

type Scheduler struct {
	log.Log
	// contains filtered or unexported fields
}

Scheduler 定时调度器

func NewScheduler

func NewScheduler(service *Service) *Scheduler

NewScheduler 创建调度器

func (*Scheduler) GetNextRun

func (s *Scheduler) GetNextRun() string

GetNextRun 获取下次执行时间

func (*Scheduler) Start

func (s *Scheduler) Start() error

Start 启动调度器

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop 停止调度器

func (*Scheduler) UpdateSchedule

func (s *Scheduler) UpdateSchedule(cronExpr string, enabled bool) error

UpdateSchedule 更新调度计划

type Service

type Service struct {
	log.Log
	// contains filtered or unexported fields
}

Service 备份服务

func NewService

func NewService(ctx *config.Context, db *backupDB) *Service

NewService 创建备份服务

func (*Service) DeleteHistory

func (s *Service) DeleteHistory(id int64) error

DeleteHistory 删除备份历史

func (*Service) ExecuteBackup

func (s *Service) ExecuteBackup(backupID string) error

ExecuteBackup 执行备份

func (*Service) GetConfig

func (s *Service) GetConfig() (*BackupConfig, error)

GetConfig 获取备份配置

func (*Service) GetDownloadURL

func (s *Service) GetDownloadURL(id int64) (string, error)

GetDownloadURL 获取下载链接

func (*Service) GetHistoryList

func (s *Service) GetHistoryList(pageIndex, pageSize int) ([]*BackupHistoryResp, int64, error)

GetHistoryList 获取备份历史列表

func (*Service) IsRunning

func (s *Service) IsRunning() bool

IsRunning 检查是否正在运行备份

func (*Service) SaveConfig

func (s *Service) SaveConfig(cfg *BackupConfig) error

SaveConfig 保存备份配置

func (*Service) TestConnection

func (s *Service) TestConnection() error

TestConnection 测试存储连接(使用系统 COS 配置)

func (*Service) TriggerBackup

func (s *Service) TriggerBackup() (string, error)

TriggerBackup 手动触发备份

type StorageConfig

type StorageConfig struct {
	Bucket    string
	AccessKey string
	SecretKey string
	Region    string
	Prefix    string // 备份路径前缀
}

StorageConfig 存储配置(复用系统 COS 配置)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL