2020-06-19 23:29:02 +02:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2020-06-19 23:29:02 +02:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2020-06-19 23:29:02 +02:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2020-06-19 23:29:02 +02:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2020-06-19 23:29:02 +02:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package dump
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
2020-10-11 22:10:03 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2020-06-19 23:29:02 +02:00
|
|
|
"code.vikunja.io/api/pkg/db"
|
|
|
|
"code.vikunja.io/api/pkg/files"
|
|
|
|
"code.vikunja.io/api/pkg/log"
|
2021-09-04 21:26:31 +02:00
|
|
|
"code.vikunja.io/api/pkg/utils"
|
2020-06-19 23:29:02 +02:00
|
|
|
"code.vikunja.io/api/pkg/version"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Dump creates a zip file with all vikunja files at filename
|
2020-06-20 11:37:51 +02:00
|
|
|
func Dump(filename string) error {
|
2020-06-19 23:29:02 +02:00
|
|
|
dumpFile, err := os.Create(filename)
|
|
|
|
if err != nil {
|
2020-06-20 11:37:51 +02:00
|
|
|
return fmt.Errorf("error opening dump file: %s", err)
|
2020-06-19 23:29:02 +02:00
|
|
|
}
|
|
|
|
defer dumpFile.Close()
|
|
|
|
|
|
|
|
dumpWriter := zip.NewWriter(dumpFile)
|
|
|
|
defer dumpWriter.Close()
|
|
|
|
|
|
|
|
// Config
|
|
|
|
log.Info("Start dumping config file...")
|
|
|
|
err = writeFileToZip(viper.ConfigFileUsed(), dumpWriter)
|
|
|
|
if err != nil {
|
2020-06-20 11:37:51 +02:00
|
|
|
return fmt.Errorf("error saving config file: %s", err)
|
2020-06-19 23:29:02 +02:00
|
|
|
}
|
|
|
|
log.Info("Dumped config file")
|
|
|
|
|
|
|
|
// Version
|
|
|
|
log.Info("Start dumping version file...")
|
2021-09-04 21:26:31 +02:00
|
|
|
err = utils.WriteBytesToZip("VERSION", []byte(version.Version), dumpWriter)
|
2020-06-19 23:29:02 +02:00
|
|
|
if err != nil {
|
2020-06-20 11:37:51 +02:00
|
|
|
return fmt.Errorf("error saving version: %s", err)
|
2020-06-19 23:29:02 +02:00
|
|
|
}
|
|
|
|
log.Info("Dumped version")
|
|
|
|
|
|
|
|
// Database
|
|
|
|
log.Info("Start dumping database...")
|
|
|
|
data, err := db.Dump()
|
|
|
|
if err != nil {
|
2020-06-20 11:37:51 +02:00
|
|
|
return fmt.Errorf("error saving database data: %s", err)
|
2020-06-19 23:29:02 +02:00
|
|
|
}
|
|
|
|
for t, d := range data {
|
2021-09-04 21:26:31 +02:00
|
|
|
err = utils.WriteBytesToZip("database/"+t+".json", d, dumpWriter)
|
2020-06-19 23:29:02 +02:00
|
|
|
if err != nil {
|
2020-06-20 11:37:51 +02:00
|
|
|
return fmt.Errorf("error writing database table %s: %s", t, err)
|
2020-06-19 23:29:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Info("Dumped database")
|
|
|
|
|
|
|
|
// Files
|
|
|
|
log.Info("Start dumping files...")
|
|
|
|
allFiles, err := files.Dump()
|
|
|
|
if err != nil {
|
2020-06-20 11:37:51 +02:00
|
|
|
return fmt.Errorf("error saving file: %s", err)
|
2020-06-19 23:29:02 +02:00
|
|
|
}
|
2021-09-04 21:26:31 +02:00
|
|
|
|
|
|
|
err = utils.WriteFilesToZip(allFiles, dumpWriter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-06-19 23:29:02 +02:00
|
|
|
}
|
2021-09-04 21:26:31 +02:00
|
|
|
|
2020-06-19 23:29:02 +02:00
|
|
|
log.Infof("Dumped files")
|
|
|
|
|
|
|
|
log.Info("Done creating dump")
|
|
|
|
log.Infof("Dump file saved at %s", filename)
|
2020-06-20 11:37:51 +02:00
|
|
|
return nil
|
2020-06-19 23:29:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeFileToZip(filename string, writer *zip.Writer) error {
|
|
|
|
// #nosec
|
|
|
|
fileToZip, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fileToZip.Close()
|
|
|
|
|
|
|
|
// Get the file information
|
|
|
|
info, err := fileToZip.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
header, err := zip.FileInfoHeader(info)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
header.Name = info.Name()
|
2021-09-04 21:26:31 +02:00
|
|
|
header.Method = utils.CompressionUsed
|
2020-06-19 23:29:02 +02:00
|
|
|
|
|
|
|
w, err := writer.CreateHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(w, fileToZip)
|
|
|
|
return err
|
|
|
|
}
|