backup

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package backup 负责配置备份的打包、加密、解析与保留策略,供手动导出、定时备份与恢复共用。

Index

Constants

View Source
const (
	// NamePrefix 是备份文件名前缀,保留策略只处理带此前缀的文件
	NamePrefix = "gately-backup-"
	// ExtZip 是未加密备份的扩展名
	ExtZip = ".zip"
	// ExtEncrypted 是加密备份的扩展名
	ExtEncrypted = ".zip.enc"
)

文件名约定

Variables

View Source
var ErrBadPassword = errors.New("备份口令错误或文件已损坏")

ErrBadPassword 表示口令错误或备份已损坏。

View Source
var ErrPasswordRequired = errors.New("备份已加密,需要提供备份口令")

ErrPasswordRequired 表示备份已加密但未提供口令。

Functions

func Build

func Build(dir, password string, now time.Time) ([]byte, error)

Build 打包配置目录。password 非空时加密输出。

func Collect

func Collect(dir string) (map[string][]byte, error)

Collect 读取配置目录下全部 *.json。

func Decrypt

func Decrypt(data []byte, password string) ([]byte, error)

Decrypt 解密 Encrypt 的输出。

func Encrypt

func Encrypt(data []byte, password string) ([]byte, error)

Encrypt 用口令以 scrypt + AES-256-GCM 加密。

func IsBackupName

func IsBackupName(name string) bool

IsBackupName 判断文件名是否为本包生成的备份。

func IsEncrypted

func IsEncrypted(data []byte) bool

IsEncrypted 判断数据是否为加密备份。

func IsOwnName added in v0.7.2

func IsOwnName(name, label string) bool

IsOwnName 判断文件名是否为服务器标识 label 生成的备份:前缀后恰好是时间戳加扩展名。 label 为空时只匹配没有标识的旧命名。

func Name

func Name(label string, now time.Time, encrypted bool) string

Name 生成备份文件名。label 为服务器标识(见 SanitizeLabel),非空时写在时间戳之前, 多台服务器备份到同一个目录或邮箱时可从文件名区分来源。

func Parse

func Parse(data []byte, password string) (map[string][]byte, error)

Parse 解析备份(自动识别是否加密),返回其中的配置文件。 只接受位于 zip 根目录、非隐藏的 *.json,且内容必须是合法 JSON。

func Pick

func Pick(names []string, keep int) []string

Pick 返回 names 中应被删除的旧备份:只统计备份文件,按时间倒序保留 keep 个。 keep 小于 1 时不删除任何文件。用于只存放本机备份的目录(服务器标识可能变化,所以不按标识过滤)。

func PickOwn added in v0.7.2

func PickOwn(names []string, keep int, labels ...string) []string

PickOwn 与 Pick 相同,但只统计服务器标识为 labels 之一的备份,用于多台服务器共用的目录, 避免清理掉其他服务器的备份。labels 为本服务器当前与以往用过的标识(服务器名或容器主机名变化后, 旧标识的备份仍按同一保留份数清理)。

func SanitizeLabel added in v0.7.2

func SanitizeLabel(host string) string

SanitizeLabel 把主机名、域名或 IP(可带端口)规范为可用于文件名的服务器标识: 去掉端口,只保留字母、数字、点和连字符,其余字符(如 IPv6 的冒号)替换为连字符,统一小写。 无法得到有效标识时返回空串。

func ServerLabel added in v0.7.2

func ServerLabel() string

ServerLabel 返回当前服务器写入备份文件名的标识:取全局服务器名称并按 SanitizeLabel 规范, 名称全是中文等无法写入文件名的字符时回退到系统主机名。

func StampOf added in v0.7.2

func StampOf(name string) string

StampOf 取出备份文件名中的时间戳,供跨服务器标识排序;格式不符时返回整个文件名。

func Zip

func Zip(files map[string][]byte, now time.Time) ([]byte, error)

Zip 把文件集合打包为 zip,文件按名称排序以保证输出稳定。

Types

This section is empty.

Jump to

Keyboard shortcuts

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