-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement filesystem to filesystem backup (#21)
- Loading branch information
Florian Rusch (cluetec GmbH)
authored
Nov 9, 2023
1 parent
c5ffba2
commit a8257ce
Showing
10 changed files
with
202 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
source: | ||
type: filesystem | ||
path: source | ||
path: /tmp/source.txt | ||
|
||
destination: | ||
type: filesystem | ||
path: destination | ||
path: /tmp/destination.txt | ||
|
||
logLevel: debug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2023 cluetec GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package filesystem | ||
|
||
import ( | ||
"errors" | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
type Writer struct { | ||
file *os.File | ||
} | ||
|
||
func NewWriter(c *Config) (*Writer, error) { | ||
w := Writer{} | ||
|
||
// Check if destination file already exists | ||
_, err := os.Stat(c.Path) | ||
if err == nil { | ||
return nil, errors.New("destination file already exists") | ||
} else if !errors.Is(err, os.ErrNotExist) { | ||
slog.Error("error while checking if destination file already exists", "error", err) | ||
return nil, err | ||
} | ||
|
||
// Create file | ||
f, err := os.Create(c.Path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
w.file = f | ||
return &w, nil | ||
} | ||
|
||
func (w *Writer) Write(b []byte) (int, error) { | ||
return w.file.Write(b) | ||
} | ||
|
||
func (w *Writer) Close() error { | ||
slog.Debug("closing filesystem writer") | ||
|
||
if w.file != nil { | ||
if err := w.file.Close(); err != nil { | ||
return err | ||
} | ||
w.file = nil | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2023 cluetec GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package filesystem | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
type Reader struct { | ||
file *os.File | ||
} | ||
|
||
func NewReader(c *Config) (*Reader, error) { | ||
r := Reader{} | ||
|
||
f, err := os.Open(c.Path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r.file = f | ||
|
||
return &r, nil | ||
} | ||
|
||
func (r *Reader) Read(b []byte) (int, error) { | ||
return r.file.Read(b) | ||
} | ||
|
||
func (r *Reader) Close() error { | ||
slog.Debug("closing filesystem reader") | ||
|
||
if r.file != nil { | ||
if err := r.file.Close(); err != nil { | ||
return err | ||
} | ||
r.file = nil | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters