e15b908983
* EE-319: backup endpoint (#193) * feat(backup): * add an orbiter to block writes while backup * add backup handler * add an ability to tar.gz a dir * add aes encryption support * EE-320: restore endpoint (#196) * feat(backup): * add restore handler * re-init system state after restore * feat(backup): Update server to respect readonly lock (#199) * feat(backup): EE-322 Add backup and restore screen (#198) Co-authored-by: Simon Meng <simon.meng@portainer.io> * name archive as portainer-backup_yyyy-mm-dd_hh-mm-ss * backup custom templates and edge jobs * restart http and proxy servers after restore to re-init internal state * feat(backup): EE-322 hide password field if password protect toggle is off * feat(backup): EE-322 add tooltip for password field of restore backup * feat(backup): EE-322 wait for backend restart after restoring * Shutdown background go-routines * changed restore err message when cannot extract * fix: symlinks are ignored from backups * replace single admin check with a restartable monitor (#238) * clean log Co-authored-by: Maxime Bajeux <max.bajeux@gmail.com> Co-authored-by: cong meng <mcpacino@gmail.com> Co-authored-by: Simon Meng <simon.meng@portainer.io>
99 lines
2.5 KiB
Go
99 lines
2.5 KiB
Go
package archive
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
"path"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func listFiles(dir string) []string {
|
|
items := make([]string, 0)
|
|
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
if path == dir {
|
|
return nil
|
|
}
|
|
items = append(items, path)
|
|
return nil
|
|
})
|
|
|
|
return items
|
|
}
|
|
|
|
func Test_shouldCreateArhive(t *testing.T) {
|
|
tmpdir, _ := os.MkdirTemp("", "backup")
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
content := []byte("content")
|
|
ioutil.WriteFile(path.Join(tmpdir, "outer"), content, 0600)
|
|
os.MkdirAll(path.Join(tmpdir, "dir"), 0700)
|
|
ioutil.WriteFile(path.Join(tmpdir, "dir", ".dotfile"), content, 0600)
|
|
ioutil.WriteFile(path.Join(tmpdir, "dir", "inner"), content, 0600)
|
|
|
|
gzPath, err := TarGzDir(tmpdir)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, filepath.Join(tmpdir, fmt.Sprintf("%s.tar.gz", filepath.Base(tmpdir))), gzPath)
|
|
|
|
extractionDir, _ := os.MkdirTemp("", "extract")
|
|
defer os.RemoveAll(extractionDir)
|
|
|
|
cmd := exec.Command("tar", "-xzf", gzPath, "-C", extractionDir)
|
|
err = cmd.Run()
|
|
if err != nil {
|
|
t.Fatal("Failed to extract archive: ", err)
|
|
}
|
|
extractedFiles := listFiles(extractionDir)
|
|
|
|
wasExtracted := func(p string) {
|
|
fullpath := path.Join(extractionDir, p)
|
|
assert.Contains(t, extractedFiles, fullpath)
|
|
copyContent, _ := ioutil.ReadFile(fullpath)
|
|
assert.Equal(t, content, copyContent)
|
|
}
|
|
|
|
wasExtracted("outer")
|
|
wasExtracted("dir/inner")
|
|
wasExtracted("dir/.dotfile")
|
|
}
|
|
|
|
func Test_shouldCreateArhiveXXXXX(t *testing.T) {
|
|
tmpdir, _ := os.MkdirTemp("", "backup")
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
content := []byte("content")
|
|
ioutil.WriteFile(path.Join(tmpdir, "outer"), content, 0600)
|
|
os.MkdirAll(path.Join(tmpdir, "dir"), 0700)
|
|
ioutil.WriteFile(path.Join(tmpdir, "dir", ".dotfile"), content, 0600)
|
|
ioutil.WriteFile(path.Join(tmpdir, "dir", "inner"), content, 0600)
|
|
|
|
gzPath, err := TarGzDir(tmpdir)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, filepath.Join(tmpdir, fmt.Sprintf("%s.tar.gz", filepath.Base(tmpdir))), gzPath)
|
|
|
|
extractionDir, _ := os.MkdirTemp("", "extract")
|
|
defer os.RemoveAll(extractionDir)
|
|
|
|
r, _ := os.Open(gzPath)
|
|
ExtractTarGz(r, extractionDir)
|
|
if err != nil {
|
|
t.Fatal("Failed to extract archive: ", err)
|
|
}
|
|
extractedFiles := listFiles(extractionDir)
|
|
|
|
wasExtracted := func(p string) {
|
|
fullpath := path.Join(extractionDir, p)
|
|
assert.Contains(t, extractedFiles, fullpath)
|
|
copyContent, _ := ioutil.ReadFile(fullpath)
|
|
assert.Equal(t, content, copyContent)
|
|
}
|
|
|
|
wasExtracted("outer")
|
|
wasExtracted("dir/inner")
|
|
wasExtracted("dir/.dotfile")
|
|
}
|