mirror of
https://github.com/eiblog/eiblog.git
synced 2026-02-07 23:32:27 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
64a754167a | ||
|
|
af2a20c34a | ||
|
|
f28d0e77e0 | ||
|
|
9a1b4db61a |
@@ -2,6 +2,13 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
||||||
|
|
||||||
|
### [2.2.8](https://github.com/eiblog/eiblog/compare/v2.2.7...v2.2.8) (2023-07-12)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **backup:** restore db and tests ([f28d0e7](https://github.com/eiblog/eiblog/commit/f28d0e77e06dd435dc13a1867f18a011e34b8f53))
|
||||||
|
|
||||||
### [2.2.7](https://github.com/eiblog/eiblog/compare/v2.2.6...v2.2.7) (2023-07-12)
|
### [2.2.7](https://github.com/eiblog/eiblog/compare/v2.2.6...v2.2.7) (2023-07-12)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
5
pkg/cache/store/mongodb.go
vendored
5
pkg/cache/store/mongodb.go
vendored
@@ -326,6 +326,11 @@ func (db *mongodb) LoadArticleList(ctx context.Context, search SearchArticles) (
|
|||||||
return articles, int(count), nil
|
return articles, int(count), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DropDatabase drop eiblog database
|
||||||
|
func (db *mongodb) DropDatabase(ctx context.Context) error {
|
||||||
|
return db.Database(mongoDBName).Drop(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
// counter counter
|
// counter counter
|
||||||
type counter struct {
|
type counter struct {
|
||||||
Name string
|
Name string
|
||||||
|
|||||||
84
pkg/cache/store/mongodb_test.go
vendored
84
pkg/cache/store/mongodb_test.go
vendored
@@ -13,7 +13,7 @@ var (
|
|||||||
store Store
|
store Store
|
||||||
acct *model.Account
|
acct *model.Account
|
||||||
blogger *model.Blogger
|
blogger *model.Blogger
|
||||||
series *model.Series
|
series *model.Serie
|
||||||
article *model.Article
|
article *model.Article
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,12 +25,12 @@ func init() {
|
|||||||
}
|
}
|
||||||
// account
|
// account
|
||||||
acct = &model.Account{
|
acct = &model.Account{
|
||||||
Username: "deepzz",
|
Username: "deepzz",
|
||||||
Password: "deepzz",
|
Password: "deepzz",
|
||||||
Email: "deepzz@example.com",
|
Email: "deepzz@example.com",
|
||||||
PhoneN: "12345678900",
|
PhoneN: "12345678900",
|
||||||
Address: "address",
|
Address: "address",
|
||||||
CreateTime: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
}
|
}
|
||||||
// blogger
|
// blogger
|
||||||
blogger = &model.Blogger{
|
blogger = &model.Blogger{
|
||||||
@@ -41,11 +41,11 @@ func init() {
|
|||||||
Copyright: "Copyright",
|
Copyright: "Copyright",
|
||||||
}
|
}
|
||||||
// series
|
// series
|
||||||
series = &model.Series{
|
series = &model.Serie{
|
||||||
Slug: "slug",
|
Slug: "slug",
|
||||||
Name: "series name",
|
Name: "series name",
|
||||||
Desc: "series desc",
|
Desc: "series desc",
|
||||||
CreateTime: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
}
|
}
|
||||||
// article
|
// article
|
||||||
article = &model.Article{
|
article = &model.Article{
|
||||||
@@ -55,21 +55,20 @@ func init() {
|
|||||||
Count: 0,
|
Count: 0,
|
||||||
Content: "### count",
|
Content: "### count",
|
||||||
SerieID: 0,
|
SerieID: 0,
|
||||||
Tags: "",
|
Tags: nil,
|
||||||
IsDraft: false,
|
IsDraft: false,
|
||||||
|
|
||||||
UpdateTime: time.Now(),
|
UpdatedAt: time.Now(),
|
||||||
CreateTime: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadInsertAccount(t *testing.T) {
|
func TestLoadInsertAccount(t *testing.T) {
|
||||||
acct2, err := store.LoadInsertAccount(context.Background(), acct)
|
ok, err := store.LoadInsertAccount(context.Background(), acct)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Log(acct2)
|
t.Log(ok)
|
||||||
t.Log(acct == acct2)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateAccount(t *testing.T) {
|
func TestUpdateAccount(t *testing.T) {
|
||||||
@@ -86,12 +85,11 @@ func TestUpdateAccount(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadInsertBlogger(t *testing.T) {
|
func TestLoadInsertBlogger(t *testing.T) {
|
||||||
blogger2, err := store.LoadInsertBlogger(context.Background(), blogger)
|
ok, err := store.LoadInsertBlogger(context.Background(), blogger)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Log(blogger2)
|
t.Log(ok)
|
||||||
t.Log(blogger == blogger2)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateBlogger(t *testing.T) {
|
func TestUpdateBlogger(t *testing.T) {
|
||||||
@@ -104,21 +102,21 @@ func TestUpdateBlogger(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInsertSeries(t *testing.T) {
|
func TestInsertSeries(t *testing.T) {
|
||||||
err := store.InsertSeries(context.Background(), series)
|
err := store.InsertSerie(context.Background(), series)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRemoveSeries(t *testing.T) {
|
func TestRemoveSeries(t *testing.T) {
|
||||||
err := store.RemoveSeries(context.Background(), 1)
|
err := store.RemoveSerie(context.Background(), 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateSeries(t *testing.T) {
|
func TestUpdateSeries(t *testing.T) {
|
||||||
err := store.UpdateSeries(context.Background(), 2, map[string]interface{}{
|
err := store.UpdateSerie(context.Background(), 2, map[string]interface{}{
|
||||||
"desc": "update desc",
|
"desc": "update desc",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -127,7 +125,7 @@ func TestUpdateSeries(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadAllSeries(t *testing.T) {
|
func TestLoadAllSeries(t *testing.T) {
|
||||||
series, err := store.LoadAllSeries(context.Background())
|
series, err := store.LoadAllSerie(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -136,7 +134,7 @@ func TestLoadAllSeries(t *testing.T) {
|
|||||||
|
|
||||||
func TestInsertArticle(t *testing.T) {
|
func TestInsertArticle(t *testing.T) {
|
||||||
article.ID = 12
|
article.ID = 12
|
||||||
err := store.InsertArticle(context.Background(), article)
|
err := store.InsertArticle(context.Background(), article, 10)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -150,14 +148,14 @@ func TestRemoveArticle(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDeleteArticle(t *testing.T) {
|
func TestDeleteArticle(t *testing.T) {
|
||||||
err := store.DeleteArticle(context.Background(), 12)
|
err := store.RemoveArticle(context.Background(), 12)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCleanArticles(t *testing.T) {
|
func TestCleanArticles(t *testing.T) {
|
||||||
err := store.CleanArticles(context.Background())
|
err := store.CleanArticles(context.Background(), time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -173,33 +171,13 @@ func TestUpdateArticle(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRecoverArticle(t *testing.T) {
|
|
||||||
err := store.RecoverArticle(context.Background(), 12)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLoadAllArticle(t *testing.T) {
|
func TestLoadAllArticle(t *testing.T) {
|
||||||
articles, err := store.LoadAllArticle(context.Background())
|
_, total, err := store.LoadArticleList(context.Background(), SearchArticles{
|
||||||
|
Page: 1,
|
||||||
|
Limit: 1000,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Logf("load all articles: %d", len(articles))
|
t.Logf("load all articles: %d", total)
|
||||||
}
|
|
||||||
|
|
||||||
func TestLoadTrashArticles(t *testing.T) {
|
|
||||||
articles, err := store.LoadTrashArticles(context.Background())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Logf("load trash articles: %d", len(articles))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLoadDraftArticles(t *testing.T) {
|
|
||||||
articles, err := store.LoadDraftArticles(context.Background())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
t.Logf("load draft articles: %d", len(articles))
|
|
||||||
}
|
}
|
||||||
|
|||||||
8
pkg/cache/store/rdbms.go
vendored
8
pkg/cache/store/rdbms.go
vendored
@@ -3,6 +3,7 @@ package store
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/eiblog/eiblog/pkg/model"
|
"github.com/eiblog/eiblog/pkg/model"
|
||||||
@@ -129,7 +130,7 @@ func (db *rdbms) InsertArticle(ctx context.Context, article *model.Article, star
|
|||||||
if id < startID {
|
if id < startID {
|
||||||
id = startID
|
id = startID
|
||||||
} else {
|
} else {
|
||||||
id += 1
|
id++
|
||||||
}
|
}
|
||||||
article.ID = id
|
article.ID = id
|
||||||
}
|
}
|
||||||
@@ -190,6 +191,11 @@ func (db *rdbms) LoadArticleList(ctx context.Context, search SearchArticles) (mo
|
|||||||
return articles, int(count), err
|
return articles, int(count), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DropDatabase drop eiblog database
|
||||||
|
func (db *rdbms) DropDatabase(ctx context.Context) error {
|
||||||
|
return errors.New("can not drop eiblog database in rdbms")
|
||||||
|
}
|
||||||
|
|
||||||
// register store
|
// register store
|
||||||
func init() {
|
func init() {
|
||||||
Register("mysql", &rdbms{})
|
Register("mysql", &rdbms{})
|
||||||
|
|||||||
3
pkg/cache/store/store.go
vendored
3
pkg/cache/store/store.go
vendored
@@ -64,6 +64,9 @@ type Store interface {
|
|||||||
LoadArticle(ctx context.Context, id int) (*model.Article, error)
|
LoadArticle(ctx context.Context, id int) (*model.Article, error)
|
||||||
// LoadArticleList 查找文章列表
|
// LoadArticleList 查找文章列表
|
||||||
LoadArticleList(ctx context.Context, search SearchArticles) (model.SortedArticles, int, error)
|
LoadArticleList(ctx context.Context, search SearchArticles) (model.SortedArticles, int, error)
|
||||||
|
|
||||||
|
// 危险操作
|
||||||
|
DropDatabase(ctx context.Context) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Driver 存储驱动
|
// Driver 存储驱动
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/eiblog/eiblog/pkg/cache/store"
|
||||||
"github.com/eiblog/eiblog/pkg/config"
|
"github.com/eiblog/eiblog/pkg/config"
|
||||||
"github.com/eiblog/eiblog/pkg/internal"
|
"github.com/eiblog/eiblog/pkg/internal"
|
||||||
)
|
)
|
||||||
@@ -24,7 +25,8 @@ func (s Storage) BackupData(now time.Time) error {
|
|||||||
case "mongodb":
|
case "mongodb":
|
||||||
return backupFromMongoDB(now)
|
return backupFromMongoDB(now)
|
||||||
default:
|
default:
|
||||||
return errors.New("unsupported database source backup to qiniu")
|
return errors.New("unsupported source backup to qiniu: " +
|
||||||
|
config.Conf.Database.Driver)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,7 +36,8 @@ func (s Storage) RestoreData() error {
|
|||||||
case "mongodb":
|
case "mongodb":
|
||||||
return restoreToMongoDB()
|
return restoreToMongoDB()
|
||||||
default:
|
default:
|
||||||
return errors.New("unsupported database source backup to qiniu")
|
return errors.New("unsupported source restore from qiniu: " +
|
||||||
|
config.Conf.Database.Driver)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,6 +98,7 @@ func backupFromMongoDB(now time.Time) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func restoreToMongoDB() error {
|
func restoreToMongoDB() error {
|
||||||
|
// backup file
|
||||||
params := internal.ContentParams{
|
params := internal.ContentParams{
|
||||||
Prefix: "blog/",
|
Prefix: "blog/",
|
||||||
|
|
||||||
@@ -113,6 +117,16 @@ func restoreToMongoDB() error {
|
|||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*20)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*20)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
// drop database
|
||||||
|
store, err := store.NewStore(config.Conf.Database.Driver,
|
||||||
|
config.Conf.Database.Source)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = store.DropDatabase(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
// unarchive
|
// unarchive
|
||||||
arg := fmt.Sprintf("tar xzf /tmp/eiblog.tar.gz -C /tmp")
|
arg := fmt.Sprintf("tar xzf /tmp/eiblog.tar.gz -C /tmp")
|
||||||
cmd := exec.CommandContext(ctx, "sh", "-c", arg)
|
cmd := exec.CommandContext(ctx, "sh", "-c", arg)
|
||||||
@@ -121,7 +135,11 @@ func restoreToMongoDB() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// restore
|
// restore
|
||||||
arg = fmt.Sprintf("mongorestore -h %s -d eiblog /tmp/eiblog", config.Conf.Database.Source)
|
u, err := url.Parse(config.Conf.Database.Source)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
arg = fmt.Sprintf("mongorestore -h %s -d eiblog /tmp/eiblog", u.Host)
|
||||||
cmd = exec.CommandContext(ctx, "sh", "-c", arg)
|
cmd = exec.CommandContext(ctx, "sh", "-c", arg)
|
||||||
return cmd.Run()
|
return cmd.Run()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ func Start(restore bool) (err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
logrus.Info("timer: RestoreData success")
|
||||||
}
|
}
|
||||||
// parse duration
|
// parse duration
|
||||||
interval, err := ParseDuration(config.Conf.BackupApp.Interval)
|
interval, err := ParseDuration(config.Conf.BackupApp.Interval)
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ func QiniuContent(params ContentParams) ([]byte, error) {
|
|||||||
// manager
|
// manager
|
||||||
bucketManager := storage.NewBucketManager(mac, cfg)
|
bucketManager := storage.NewBucketManager(mac, cfg)
|
||||||
// list file
|
// list file
|
||||||
files, _, _, _, err := bucketManager.ListFiles(params.Conf.Bucket, params.Prefix, "", "", 2)
|
files, _, _, _, err := bucketManager.ListFiles(params.Conf.Bucket, params.Prefix, "", "", 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user