backend/sftp: Add sftp.server-alive-interval and sftp.server-alive-count-max options (#5314)

This commit is contained in:
Merlin authored and GitHub committed 2026-11-27 22:16:04 +01:00
1 parent 885519b1ba
commit 4797768ba9
5 files changed
+64 -5

No files matched your search

+12
View File
@@ -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
+15 -5
View File
@@ -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
+4
View File
@@ -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.
+6
View File
@@ -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)
+27
View File
@@ -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())
}