mirror of
https://github.com/offen/docker-volume-backup.git
synced 2025-12-06 17:38:01 +01:00
Ensure destination exists when using SSH storage (#646)
* Ensure destination exists when using SSH storage * Import "errors" * New feature doc
This commit is contained in:
@@ -278,6 +278,7 @@ The values for each key currently match its default.
|
|||||||
# ---
|
# ---
|
||||||
|
|
||||||
# The Directory to place the backups to on the SSH server.
|
# The Directory to place the backups to on the SSH server.
|
||||||
|
# If the directory does not exist, it will be created automatically.
|
||||||
# Example: "/home/user/backups"
|
# Example: "/home/user/backups"
|
||||||
|
|
||||||
# SSH_REMOTE_PATH=""
|
# SSH_REMOTE_PATH=""
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
package ssh
|
package ssh
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@@ -107,6 +108,10 @@ func (b *sshStorage) Name() string {
|
|||||||
|
|
||||||
// Copy copies the given file to the SSH storage backend.
|
// Copy copies the given file to the SSH storage backend.
|
||||||
func (b *sshStorage) Copy(file string) (returnErr error) {
|
func (b *sshStorage) Copy(file string) (returnErr error) {
|
||||||
|
if err := b.sftpClient.MkdirAll(b.DestinationPath); err != nil {
|
||||||
|
return errwrap.Wrap(err, "error ensuring destination directory")
|
||||||
|
}
|
||||||
|
|
||||||
source, err := os.Open(file)
|
source, err := os.Open(file)
|
||||||
_, name := path.Split(file)
|
_, name := path.Split(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -170,6 +175,10 @@ func (b *sshStorage) Copy(file string) (returnErr error) {
|
|||||||
func (b *sshStorage) Prune(deadline time.Time, pruningPrefix string) (*storage.PruneStats, error) {
|
func (b *sshStorage) Prune(deadline time.Time, pruningPrefix string) (*storage.PruneStats, error) {
|
||||||
candidates, err := b.sftpClient.ReadDir(b.DestinationPath)
|
candidates, err := b.sftpClient.ReadDir(b.DestinationPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// If directory doesn't exist yet, nothing to prune
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
return &storage.PruneStats{}, nil
|
||||||
|
}
|
||||||
return nil, errwrap.Wrap(err, "error reading directory")
|
return nil, errwrap.Wrap(err, "error reading directory")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user