Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

Backup system

Smaug6739f5733900ce14
diff --git a/backend/controllers/ressources.controller.go b/backend/controllers/ressources.controller.go
index c18208ae..54e31a91 100644
--- a/backend/controllers/ressources.controller.go
+++ b/backend/controllers/ressources.controller.go
@@ -4,7 +4,9 @@ import (
"alexandrie/app"
"alexandrie/models"
"alexandrie/utils"
+ "bytes"
"context"
+ "encoding/json"
"errors"
"fmt"
"net/http"
@@ -20,6 +22,7 @@ import (
type RessourceController interface {
GetAllUploads(c *gin.Context) (int, any)
+ GetBackup(c *gin.Context) (int, any)
UploadFile(c *gin.Context) (int, any)
UploadAvatar(c *gin.Context) (int, any)
DeleteUpload(c *gin.Context) (int, any)
@@ -43,6 +46,53 @@ func (ctr *Controller) GetAllUploads(c *gin.Context) (int, any) {
return http.StatusOK, uploads
}
+// Get backup
+// @Summary Get backups files of database (documents, categories, ressources)
+// @Summary Save database as json file and save it in minio (/userid/backups)
+// @Method GET
+// @Router /uploads/backup [get]
+// @Security Authenfification: Auth
+// @Success 200 {object} Success(string)
+// @Failure 400 {object} Error
+// @Failure 401 {object} Error
+func (ctr *Controller) GetBackup(c *gin.Context) (int, any) {
+ userId, err := utils.GetUserIdCtx(c)
+ if err != nil {
+ return http.StatusBadRequest, err
+ }
+
+ // Create the backup file from database data
+ documents_list, err := ctr.app.Services.Document.GetAllDocumentBackup(userId)
+ if err != nil {
+ return http.StatusInternalServerError, err
+ }
+ categories_list, err := ctr.app.Services.Category.GetAllCategories(userId)
+ if err != nil {
+ return http.StatusInternalServerError, err
+ }
+ ressources_list, err := ctr.app.Services.Ressource.GetAllUploadsByUserId(userId)
+ if err != nil {
+ return http.StatusInternalServerError, err
+ }
+ backup := map[string]interface{}{
+ "documents": documents_list,
+ "categories": categories_list,
+ "ressources": ressources_list,
+ }
+ jsonString, err := json.Marshal(backup)
+ if err != nil {
+ return http.StatusInternalServerError, err
+ }
+ objectName := fmt.Sprintf("%d/backups/backup.json", userId)
+ _, err = ctr.app.MinioClient.PutObject(c, os.Getenv("MINIO_BUCKET"), objectName, bytes.NewReader(jsonString), int64(len(jsonString)), minio.PutObjectOptions{ContentType: "application/json"})
+ if err != nil {
+ return http.StatusInternalServerError, err
+ }
+ return http.StatusCreated, gin.H{
+ "link": objectName,
+ }
+}
+
// UploadFile
// @Summary Upload a file
// @Method POST
diff --git a/backend/router/routes/ressources.go b/backend/router/routes/ressources.go
index 1ddf7ab7..7851d837 100644
--- a/backend/router/routes/ressources.go
+++ b/backend/router/routes/ressources.go
@@ -15,6 +15,7 @@ func Uploads(app *app.App, router *gin.RouterGroup) {
ressources.Use(middlewares.Auth())
ressources.GET(("/:userId"), utils.WP(ressourcesCtrl.GetAllUploads))
+ ressources.GET(("/backup"), utils.WP(ressourcesCtrl.GetBackup))
ressources.POST("", utils.WP(ressourcesCtrl.UploadFile))
ressources.POST("/avatar", utils.WP(ressourcesCtrl.UploadAvatar))
ressources.DELETE("/:id", utils.WP(ressourcesCtrl.DeleteUpload))
diff --git a/backend/services/document.service.go b/backend/services/document.service.go
index 560dea42..da4f068c 100644
--- a/backend/services/document.service.go
+++ b/backend/services/document.service.go
@@ -8,6 +8,7 @@ import (
type DocumentService interface {
GetAllDocuments(userId types.Snowflake) ([]*models.Document, error)
+ GetAllDocumentBackup(author_id types.Snowflake) ([]*models.Document, error)
GetDocument(documentId types.Snowflake) (*models.Document, error)
CreateDocument(document *models.Document) error
UpdateDocument(document *models.Document) error
@@ -46,6 +47,34 @@ func (s *Service) GetAllDocuments(userId types.Snowflake) ([]*models.Document, e
return documents, nil
}
+func (s *Service) GetAllDocumentBackup(author_id types.Snowflake) ([]*models.Document, error) {
+ var documents []*models.Document
+ rows, err := s.db.Query("SELECT id, name, description, tags, category, parent_id, accessibility, content_markdown, content_html, author_id, created_timestamp, updated_timestamp FROM documents WHERE author_id = ?", author_id)
+ if err != nil {
+ return nil, err
+ }
+ for rows.Next() {
+ var document models.Document
+ if err := rows.Scan(
+ &document.Id,
+ &document.Name,
+ &document.Description,
+ &document.Tags,
+ &document.Category,
+ &document.ParentId,
+ &document.Accessibility,
+ &document.ContentMarkdown,
+ &document.ContentHtml,
+ &document.AuthorId,
+ &document.CreatedTimestamp,
+ &document.UpdatedTimestamp); err != nil {
+ return nil, err
+ }
+ documents = append(documents, &document)
+ }
+ return documents, nil
+}
+
func (s *Service) GetDocument(documentId types.Snowflake) (*models.Document, error) {
var document models.Document
err := s.db.QueryRow("SELECT id, name, description, tags, category, parent_id, accessibility, content_markdown, content_html, author_id, created_timestamp, updated_timestamp FROM documents WHERE id = ?", documentId).Scan(