备份上传Minio

This commit is contained in:
gotoeasy 2022-09-25 22:43:44 +08:00
parent 9d538fc74b
commit 6939b3d4ff
7 changed files with 362 additions and 5 deletions

View File

@ -32,6 +32,13 @@ var username string
var password string
var slaveHosts []string
var enableSlaveTransfer bool
var enableBackup bool
var glcGroup string
var minioUrl string
var minioUser string
var minioPassword string
var minioBucket string
var enableUploadMinio bool
func init() {
UpdateConfigByEnv()
@ -67,7 +74,48 @@ func UpdateConfigByEnv() {
password = Getenv("GLC_PASSWORD", "GLogCenter100%666") // 登录密码默认“GLogCenter100%666”
splitHost(Getenv("GLC_SLAVE_HOSTS", "")) // 从服务器地址,多个时逗号分开,默认“”
enableSlaveTransfer = GetenvBool("GLC_SLAVE_TRANSFER", false) // 是否开启转发日志到其他GLC服务默认false
enableBackup = GetenvBool("GLC_ENABLE_BACKUP", false) // 是否开启备份默认false
glcGroup = Getenv("GLC_GROUP", "default") // 日志中心分组名默认“default”
minioUrl = Getenv("GLC_MINIO_URL", "") // MINIO地址默认“”
minioUser = Getenv("GLC_MINIO_USER", "") // MINIO用户名默认“”
minioPassword = Getenv("GLC_MINIO_PASS", "") // MINIO密码默认“”
minioBucket = Getenv("GLC_MINIO_BUCKET", "") // MINIO桶名默认“”
enableUploadMinio = GetenvBool("GLC_ENABLE_UPLOAD_MINIO", false) // 是否开启上传备份至MINIO服务器默认false
}
// 取配置: 是否开启MINIO备份可通过环境变量“GLC_ENABLE_BACKUP”设定默认false
func IsEnableBackup() bool {
return enableBackup
}
// 取配置: 日志中心分组名可通过环境变量“GLC_GROUP”设定默认“default”
func GetGlcGroup() string {
return glcGroup
}
// 取配置: MINIO地址可通过环境变量“GLC_MINIO_URL”设定默认“”
func GetMinioUrl() string {
return minioUrl
}
// 取配置: MINIO用户名可通过环境变量“GLC_MINIO_USER”设定默认“”
func GetMinioUser() string {
return minioUser
}
// 取配置: MINIO密码可通过环境变量“GLC_MINIO_PASS”设定默认“”
func GetMinioPassword() string {
return minioPassword
}
// 取配置: MINIO桶名可通过环境变量“GLC_MINIO_BUCKET”设定默认“”
func GetMinioBucket() string {
return minioBucket
}
// 取配置: 是否开启上传备份至MINIO服务器可通过环境变量“GLC_ENABLE_UPLOAD_MINIO”设定默认false
func IsEnableUploadMinio() bool {
return enableUploadMinio
}
// 取配置: 是否开启转发日志到其他GLC服务可通过环境变量“GLC_SLAVE_TRANSFER”设定默认false

View File

@ -5,6 +5,7 @@ go 1.19
require github.com/syndtr/goleveldb v1.0.0
require (
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-playground/locales v0.14.0 // indirect
@ -12,20 +13,28 @@ require (
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/klauspost/cpuid/v2 v2.1.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/rs/xid v1.4.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/stretchr/testify v1.7.5 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
@ -33,6 +42,7 @@ require (
github.com/gin-contrib/gzip v0.0.5
github.com/gin-gonic/gin v1.8.1
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
github.com/minio/minio-go/v7 v7.0.37
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/streadway/amqp v1.0.0
github.com/wangbin/jiebago v0.3.2

View File

@ -2,6 +2,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gin-contrib/gzip v0.0.5 h1:mhnVU32YnnBh2LPH2iqRqsA/eR7SAqRaD388jL2s/j0=
github.com/gin-contrib/gzip v0.0.5/go.mod h1:OPIK6HR0Um2vNmBUTlayD7qle4yVVRZT0PyhdUigrKk=
@ -34,11 +36,19 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.1.0 h1:eyi1Ad2aNJMW95zcSbmGg7Cg6cq3ADwLpMAP96d8rF0=
github.com/klauspost/cpuid/v2 v2.1.0/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
@ -53,8 +63,16 @@ github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ic
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
github.com/minio/minio-go/v7 v7.0.37 h1:aJvYMbtpVPSFBck6guyvOkxK03MycxDOCs49ZBuY5M8=
github.com/minio/minio-go/v7 v7.0.37/go.mod h1:nCrRzjoSUQh8hgKKtu3Y708OLvRLtuASMg2/nvmbarw=
github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g=
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
@ -71,8 +89,12 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rs/xid v1.4.0 h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo=
github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@ -99,10 +121,14 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@ -115,12 +141,18 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
@ -133,6 +165,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/ini.v1 v1.66.6 h1:LATuAqN/shcYAOkv3wl2L4rkaKqkcgTBQjOyYDvcPKI=
gopkg.in/ini.v1 v1.66.6/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

82
glc/ldb/backup/backup.go Normal file
View File

@ -0,0 +1,82 @@
package backup
import (
"glc/cmn"
"glc/conf"
"glc/onexit"
"log"
"math"
"path/filepath"
"time"
)
var backupBusy bool = false
func init() {
if !conf.IsEnableBackup() {
return
}
onexit.RegisterExitHandle(onExit)
}
func Start() {
if !conf.IsEnableBackup() {
return
}
// TODO BackupStorage("sssssssss")
}
// 指定的日志仓打包为tar文件后上传Minio
func BackupStorage(storeName string) bool {
if !conf.IsEnableBackup() {
return false
}
backupBusy = true
defer (func() { backupBusy = false })()
// 打包tar
dir := filepath.Join(conf.GetStorageRoot(), storeName)
if cmn.IsExistDir(dir) {
return false // 目录不存在
}
ymd := cmn.StringToUint32(cmn.RightRune(storeName, 8), math.MaxUint32)
today := cmn.StringToUint32(cmn.GetYyyymmdd(0), 0)
if ymd >= today {
return false // 仅支持压缩过去日期的日志仓目录
}
tarfile := storeName + ".tar"
tarfilename := filepath.Join(conf.GetStorageRoot(), ".bak", tarfile)
err := TarDir(dir, tarfilename)
if err != nil {
log.Println(err.Error())
return false
}
// 上传Minio
if conf.IsEnableUploadMinio() {
group := conf.GetGlcGroup()
err := UploadMinio(tarfilename, group+"/"+tarfile)
if err != nil {
log.Println(err.Error())
return false
}
}
return err == nil
}
func onExit() {
if !backupBusy {
return
}
ticker := time.NewTicker(time.Second)
for {
<-ticker.C
if !backupBusy {
ticker.Stop()
break
}
}
}

40
glc/ldb/backup/minio.go Normal file
View File

@ -0,0 +1,40 @@
package backup
import (
"context"
"glc/conf"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func UploadMinio(localPathFile string, minioObjectName string) error {
ctx := context.Background()
endpoint := conf.GetMinioUrl()
accessKeyID := conf.GetMinioUser()
secretAccessKey := conf.GetMinioPassword()
bucketName := conf.GetMinioBucket()
// Initialize minio client object.
minioClient, err := minio.New(endpoint, &minio.Options{Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), Secure: false})
if err != nil {
return err
}
// Make a new bucket called mymusic.
exists, err := minioClient.BucketExists(ctx, bucketName)
if !(err == nil && exists) {
err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{})
if err != nil {
return err
}
}
// Upload
_, err = minioClient.FPutObject(ctx, bucketName, minioObjectName, localPathFile, minio.PutObjectOptions{ContentType: "application/x-tar"})
if err != nil {
return err
}
return nil
}

133
glc/ldb/backup/tar.go Normal file
View File

@ -0,0 +1,133 @@
package backup
import (
"archive/tar"
"errors"
"fmt"
"glc/cmn"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
)
func TarDir(directory string, tarfilename string) error {
if !cmn.IsExistDir(directory) {
return errors.New("目录不存在")
}
dir, err := filepath.Abs(directory)
if err != nil {
return err
}
lenPrefix := cmn.LenRune(filepath.Dir(dir)) // 绝对路径除去末尾目录名后的长度
// 创建文件
f, err := os.OpenFile(tarfilename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
return err
}
defer f.Close()
// 创建一个Writer
writer := tar.NewWriter(f)
defer writer.Close()
// 遍历需要归档的目录
return filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
// 如果是目录跳过(空目录?)
if info.IsDir() {
return nil
}
// 打开文件
f, err := os.Open(path)
if err != nil {
fmt.Println(err)
return err
}
// 写入文件头
name := cmn.SubStringRune(path, lenPrefix+1, cmn.LenRune(path))
name = strings.ReplaceAll(name, "\\", "/")
hr := &tar.Header{
Name: name, // 用相对目录名
Format: tar.FormatGNU, // 支持中文目录文件名
Size: info.Size(),
Mode: 0666,
ModTime: info.ModTime(),
}
// 将文件头写入文件中
writer.WriteHeader(hr)
var buff [1024]byte
// 不断读取文件中的内容并且写入tar文件中
for {
n, err := f.Read(buff[:])
writer.Write(buff[:n])
if err != nil {
if err == io.EOF {
break
}
return nil
}
}
return nil
})
}
// 把tar文件解压到指定目录中
func UnTar(tarFile string, dist string) error {
if dist == "" {
dist, _ = filepath.Abs(".") // 默认解压到当前目录
}
distDir, _ := filepath.Abs(dist) // 转绝对路径
// 打开 tar 包
fr, err := os.Open(tarFile)
if err != nil {
fmt.Println(err)
return err
}
defer fr.Close()
tr := tar.NewReader(fr)
for hdr, err := tr.Next(); err != io.EOF; hdr, err = tr.Next() {
if err != nil {
fmt.Println(err)
return err
}
full := filepath.Join(distDir, hdr.Name)
if runtime.GOOS == "windows" {
full = filepath.Join(distDir, strings.ReplaceAll(hdr.Name, "/", "\\"))
}
if hdr.FileInfo().IsDir() {
os.MkdirAll(full, 0666)
continue
} else {
os.MkdirAll(filepath.Dir(full), 0666)
}
fw, err := os.Create(full) // 创建一个空文件,用来写入解包后的数据
if err != nil {
fmt.Println(err)
return err
}
if _, err := io.Copy(fw, tr); err != nil {
fmt.Println(err)
fw.Close()
return err
}
os.Chmod(full, fs.FileMode(hdr.Mode))
fw.Close()
}
return nil
}

View File

@ -0,0 +1,10 @@
package backup
import (
"testing"
)
func Test_tar(t *testing.T) {
//TarDir("F:\\222/default", "F:\\222\\000.tar")
//UnTar("F:\\222\\000.tar", "F:\\222\\xxxooo/aaaa")
}