Add "none" compression type (#457)

* Add "none" compression type

* Add "none" compression to docs

* Use passThroughWriteCloser for "none" compression

* Add test for none compression

---------

Co-authored-by: Frederik Ring <frederik.ring@gmail.com>
This commit is contained in:
J. Zebedee 2024-08-11 03:11:09 -05:00 committed by GitHub
parent 336e12f874
commit f97ce11734
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 70 additions and 5 deletions

View File

@ -93,6 +93,8 @@ func compress(paths []string, outFilePath, algo string, concurrency int) error {
func getCompressionWriter(file *os.File, algo string, concurrency int) (io.WriteCloser, error) { func getCompressionWriter(file *os.File, algo string, concurrency int) (io.WriteCloser, error) {
switch algo { switch algo {
case "none":
return &passThroughWriteCloser{file}, nil
case "gz": case "gz":
w, err := pgzip.NewWriterLevel(file, 5) w, err := pgzip.NewWriterLevel(file, 5)
if err != nil { if err != nil {
@ -165,3 +167,15 @@ func writeTarball(path string, tarWriter *tar.Writer, prefix string) error {
return nil return nil
} }
type passThroughWriteCloser struct {
target io.WriteCloser
}
func (p *passThroughWriteCloser) Write(b []byte) (int, error) {
return p.target.Write(b)
}
func (p *passThroughWriteCloser) Close() error {
return nil
}

View File

@ -92,7 +92,7 @@ type CompressionType string
func (c *CompressionType) Decode(v string) error { func (c *CompressionType) Decode(v string) error {
switch v { switch v {
case "gz", "zst": case "none", "gz", "zst":
*c = CompressionType(v) *c = CompressionType(v)
return nil return nil
default: default:

View File

@ -86,7 +86,12 @@ func (s *script) init() error {
var bf bytes.Buffer var bf bytes.Buffer
if tErr := tmplFileName.Execute(&bf, map[string]string{ if tErr := tmplFileName.Execute(&bf, map[string]string{
"Extension": fmt.Sprintf("tar.%s", s.c.BackupCompression), "Extension": func() string {
if s.c.BackupCompression == "none" {
return "tar"
}
return fmt.Sprintf("tar.%s", s.c.BackupCompression)
}(),
}); tErr != nil { }); tErr != nil {
return errwrap.Wrap(tErr, "error executing backup file extension template") return errwrap.Wrap(tErr, "error executing backup file extension template")
} }

View File

@ -43,8 +43,8 @@ You can populate below template according to your requirements and use it as you
# BACKUP_CRON_EXPRESSION="0 2 * * *" # BACKUP_CRON_EXPRESSION="0 2 * * *"
# The compression algorithm used in conjunction with tar. # The compression algorithm used in conjunction with tar.
# Valid options are: "gz" (Gzip) and "zst" (Zstd). # Valid options are: "gz" (Gzip), "zst" (Zstd) or "none" (tar only).
# Note that the selection affects the file extension. # Default is "gz". Note that the selection affects the file extension.
# BACKUP_COMPRESSION="gz" # BACKUP_COMPRESSION="gz"
@ -60,7 +60,7 @@ You can populate below template according to your requirements and use it as you
# will result in the same filename for every backup run, which means previous # will result in the same filename for every backup run, which means previous
# versions will be overwritten on subsequent runs. # versions will be overwritten on subsequent runs.
# Extension can be defined literally or via "{{ .Extension }}" template, # Extension can be defined literally or via "{{ .Extension }}" template,
# in which case it will become either "tar.gz" or "tar.zst" (depending # in which case it will become either "tar.gz", "tar.zst" or ".tar" (depending
# on your BACKUP_COMPRESSION setting). # on your BACKUP_COMPRESSION setting).
# The default results in filenames like: `backup-2021-08-29T04-00-00.tar.gz`. # The default results in filenames like: `backup-2021-08-29T04-00-00.tar.gz`.

View File

@ -0,0 +1,21 @@
services:
backup:
image: offen/docker-volume-backup:${TEST_VERSION:-canary}
restart: always
environment:
BACKUP_FILENAME: test.{{ .Extension }}
BACKUP_COMPRESSION: none
volumes:
- app_data:/backup/app_data:ro
- /var/run/docker.sock:/var/run/docker.sock
- ${LOCAL_DIR:-./local}:/archive
offen:
image: offen/offen:latest
labels:
- docker-volume-backup.stop-during-backup=true
volumes:
- app_data:/var/opt/offen
volumes:
app_data:

25
test/tar/run.sh Executable file
View File

@ -0,0 +1,25 @@
#!/bin/sh
set -e
cd "$(dirname "$0")"
. ../util.sh
current_test=$(basename $(pwd))
export LOCAL_DIR=$(mktemp -d)
docker compose up -d --quiet-pull
sleep 5
docker compose exec backup backup
sleep 5
expect_running_containers "2"
tmp_dir=$(mktemp -d)
tar -xvf "$LOCAL_DIR/test.tar" -C $tmp_dir
if [ ! -f "$tmp_dir/backup/app_data/offen.db" ]; then
fail "Could not find expected file in untared archive."
fi
pass "Expected file was found."