From 4797768ba954e865b9d4ee1fa17d4e75db60e7f2 Mon Sep 17 00:00:00 2001 From: Merlin <158784988+merlinz01@users.noreply.github.com> Date: Fri, 27 Nov 2026 16:16:04 -0500 Subject: [PATCH] backend/sftp: Add sftp.server-alive-interval and sftp.server-alive-count-max options (#5314) --- changelog/unreleased/issue-5313 | 12 ++++++++++++ doc/030_preparing_a_new_repo.rst | 20 +++++++++++++++----- internal/backend/sftp/config.go | 4 ++++ internal/backend/sftp/sftp.go | 6 ++++++ internal/backend/sftp/sshcmd_test.go | 27 +++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 changelog/unreleased/issue-5313 diff --git a/changelog/unreleased/issue-5313 b/changelog/unreleased/issue-5313 new file mode 100644 index 000000000..f80c3b566 --- /dev/null +++ b/changelog/unreleased/issue-5313 @@ -0,0 +1,12 @@ +Enhancement: Add options to configure SSH keepalive for SFTP backend + +SFTP connections could be interrupted when no data was sent for a long time, +for example while restic processes large amounts of unchanged data. The new +`sftp.server-alive-interval` and `sftp.server-alive-count-max` options set the +corresponding `ServerAliveInterval` and `ServerAliveCountMax` SSH options to +keep the connection alive. If unset, the values from the SSH configuration +are used. + +https://github.com/restic/restic/issues/5313 +https://github.com/restic/restic/pull/5314 +https://forum.restic.net/t/robustness-of-repository-issue-resolved/1475 diff --git a/doc/030_preparing_a_new_repo.rst b/doc/030_preparing_a_new_repo.rst index 2b6b2ecba..808a860aa 100644 --- a/doc/030_preparing_a_new_repo.rst +++ b/doc/030_preparing_a_new_repo.rst @@ -179,13 +179,23 @@ setting the arguments passed to the default SSH command (ignored when .. note:: Please be aware that SFTP servers close connections when no data is received by the client. This can happen when restic is processing huge - amounts of unchanged data. To avoid this issue add the following lines - to the client's .ssh/config file: + amounts of unchanged data. To keep the connection alive, set the + ``ServerAliveInterval`` and ``ServerAliveCountMax`` SSH options using + restic's ``sftp.server-alive-interval`` and ``sftp.server-alive-count-max`` + options. For example, the following sends a keepalive message every + 60 seconds and closes the connection after 5 unanswered messages: -:: + :: - ServerAliveInterval 60 - ServerAliveCountMax 240 + $ restic -o sftp.server-alive-interval=60 -o sftp.server-alive-count-max=5 -r sftp:user@host:/srv/restic-repo backup ~/work + + Alternatively, add the following lines to the client's ``.ssh/config`` + file: + + :: + + ServerAliveInterval 60 + ServerAliveCountMax 5 REST Server diff --git a/internal/backend/sftp/config.go b/internal/backend/sftp/config.go index daefbf441..37d8988d5 100644 --- a/internal/backend/sftp/config.go +++ b/internal/backend/sftp/config.go @@ -17,6 +17,10 @@ type Config struct { Args string `option:"args" help:"specify arguments for ssh"` Connections uint `option:"connections" help:"set a limit for the number of concurrent connections (default: 5)"` + + // SSH options + ServerAliveInterval uint `option:"server-alive-interval" help:"set the ssh ServerAliveInterval option in seconds (default: use ssh config)"` + ServerAliveCountMax uint `option:"server-alive-count-max" help:"set the ssh ServerAliveCountMax option (default: use ssh config)"` } // NewConfig returns a new config with default options applied. diff --git a/internal/backend/sftp/sftp.go b/internal/backend/sftp/sftp.go index 4390e2815..62461ec6e 100644 --- a/internal/backend/sftp/sftp.go +++ b/internal/backend/sftp/sftp.go @@ -257,6 +257,12 @@ func buildSSHCommand(cfg Config) (cmd string, args []string, err error) { if cfg.User != "" { args = append(args, "-l", cfg.User) } + if cfg.ServerAliveInterval > 0 { + args = append(args, "-o", fmt.Sprintf("ServerAliveInterval=%d", cfg.ServerAliveInterval)) + } + if cfg.ServerAliveCountMax > 0 { + args = append(args, "-o", fmt.Sprintf("ServerAliveCountMax=%d", cfg.ServerAliveCountMax)) + } if cfg.Args != "" { a, err := backend.SplitShellStrings(cfg.Args) diff --git a/internal/backend/sftp/sshcmd_test.go b/internal/backend/sftp/sshcmd_test.go index 601c94bd2..dedd024d7 100644 --- a/internal/backend/sftp/sshcmd_test.go +++ b/internal/backend/sftp/sshcmd_test.go @@ -61,6 +61,30 @@ var sshcmdTests = []struct { []string{"::1%lo0", "-p", "22", "-l", "user", "-s", "sftp"}, "", }, + { + Config{User: "user", Host: "host", Path: "dir", ServerAliveInterval: 99}, + "ssh", + []string{"host", "-l", "user", "-o", "ServerAliveInterval=99", "-s", "sftp"}, + "", + }, + { + Config{User: "user", Host: "host", Path: "dir", ServerAliveCountMax: 99}, + "ssh", + []string{"host", "-l", "user", "-o", "ServerAliveCountMax=99", "-s", "sftp"}, + "", + }, + { + Config{User: "user", Host: "host", Path: "dir", ServerAliveInterval: 99, ServerAliveCountMax: 99}, + "ssh", + []string{"host", "-l", "user", "-o", "ServerAliveInterval=99", "-o", "ServerAliveCountMax=99", "-s", "sftp"}, + "", + }, + { + Config{User: "user", Host: "host", Path: "dir", ServerAliveInterval: 99, ServerAliveCountMax: 99, Args: "-i /path/to/id_rsa"}, + "ssh", + []string{"host", "-l", "user", "-o", "ServerAliveInterval=99", "-o", "ServerAliveCountMax=99", "-i", "/path/to/id_rsa", "-s", "sftp"}, + "", + }, } func TestBuildSSHCommand(t *testing.T) { @@ -68,6 +92,9 @@ func TestBuildSSHCommand(t *testing.T) { t.Run("", func(t *testing.T) { cmd, args, err := buildSSHCommand(test.cfg) if test.err != "" { + if err == nil { + t.Fatalf("expected error %v got nil", test.err) + } if err.Error() != test.err { t.Fatalf("expected error %v got %v", test.err, err.Error()) }