From 47e9328f2eabd6f07b2a09408bb31d20c7ac793e Mon Sep 17 00:00:00 2001 From: kolaente Date: Fri, 19 Jun 2020 21:56:43 +0200 Subject: [PATCH] Dump config --- pkg/cmd/dump.go | 97 +++++++++++++++++++++++++++++++++++++++++++++++ pkg/db/dump.go | 23 +++++++++++ pkg/files/dump.go | 24 ++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 pkg/cmd/dump.go create mode 100644 pkg/db/dump.go create mode 100644 pkg/files/dump.go diff --git a/pkg/cmd/dump.go b/pkg/cmd/dump.go new file mode 100644 index 000000000..56646fd9f --- /dev/null +++ b/pkg/cmd/dump.go @@ -0,0 +1,97 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-2020 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// 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 +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package cmd + +import ( + "archive/zip" + "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/files" + "code.vikunja.io/api/pkg/log" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "io" + "os" + "time" +) + +func init() { + rootCmd.AddCommand(dumpCmd) +} + +var dumpCmd = &cobra.Command{ + Use: "dump", + Short: "Dump all vikunja data into a zip file. Includes config, files and db.", + PreRun: func(cmd *cobra.Command, args []string) { + fullInit() + }, + Run: func(cmd *cobra.Command, args []string) { + filename := "vikunja-dump_" + time.Now().Format("2006-01-02_15-03-05") + ".zip" + dumpFile, err := os.Create(filename) + if err != nil { + log.Criticalf("Error opening dump file: %s", err) + } + defer dumpFile.Close() + + dumpWriter := zip.NewWriter(dumpFile) + defer dumpWriter.Close() + + // Config + err = writeFileToZip(viper.ConfigFileUsed(), dumpWriter) + if err != nil { + log.Criticalf("Error saving config file: %s", err) + } + + // Database + db.Dump() + // Files + files.Dump() + }, +} + +func writeFileToZip(filename string, writer *zip.Writer) error { + 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 + } + + // Using FileInfoHeader() above only uses the basename of the file. If we want + // to preserve the folder structure we can overwrite this with the full path. + header.Name = info.Name() + + // Change to deflate to gain better compression + // see http://golang.org/pkg/archive/zip/#pkg-constants + header.Method = zip.Deflate + + w, err := writer.CreateHeader(header) + if err != nil { + return err + } + _, err = io.Copy(w, fileToZip) + return err +} diff --git a/pkg/db/dump.go b/pkg/db/dump.go new file mode 100644 index 000000000..b1927b73c --- /dev/null +++ b/pkg/db/dump.go @@ -0,0 +1,23 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-2020 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// 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 +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package db + +// Dump dumps all database tables +func Dump() (err error) { + + return +} diff --git a/pkg/files/dump.go b/pkg/files/dump.go new file mode 100644 index 000000000..e5cdabb7d --- /dev/null +++ b/pkg/files/dump.go @@ -0,0 +1,24 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-2020 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// 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 +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package files + +// Dump dumps all saved files +// This only includes the raw files, no db entries. +func Dump() (err error) { + + return +}