2018-06-22 19:07:23 +00:00
|
|
|
/*
|
2019-07-31 17:52:23 +00:00
|
|
|
Copyright 2018, 2019 the Velero contributors.
|
2018-06-22 19:07:23 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2018-02-28 01:35:35 +00:00
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BackupCommand returns a Command for running a restic backup.
|
2018-06-15 03:24:01 +00:00
|
|
|
func BackupCommand(repoIdentifier, passwordFile, path string, tags map[string]string) *Command {
|
2019-01-10 19:48:37 +00:00
|
|
|
// --host flag is provided with a generic value because restic uses the host
|
2018-07-02 17:52:32 +00:00
|
|
|
// to find a parent snapshot, and by default it will be the name of the daemonset pod
|
|
|
|
// where the `restic backup` command is run. If this pod is recreated, we want to continue
|
|
|
|
// taking incremental backups rather than triggering a full one due to a new pod name.
|
|
|
|
|
2018-02-28 01:35:35 +00:00
|
|
|
return &Command{
|
2018-06-15 03:24:01 +00:00
|
|
|
Command: "backup",
|
|
|
|
RepoIdentifier: repoIdentifier,
|
|
|
|
PasswordFile: passwordFile,
|
|
|
|
Dir: path,
|
|
|
|
Args: []string{"."},
|
2019-09-10 18:28:19 +00:00
|
|
|
ExtraFlags: append(backupTagFlags(tags), "--host=velero", "--json"),
|
2018-02-28 01:35:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func backupTagFlags(tags map[string]string) []string {
|
|
|
|
var flags []string
|
|
|
|
for k, v := range tags {
|
|
|
|
flags = append(flags, fmt.Sprintf("--tag=%s=%s", k, v))
|
|
|
|
}
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreCommand returns a Command for running a restic restore.
|
2018-06-15 03:24:01 +00:00
|
|
|
func RestoreCommand(repoIdentifier, passwordFile, snapshotID, target string) *Command {
|
2018-02-28 01:35:35 +00:00
|
|
|
return &Command{
|
2018-06-15 03:24:01 +00:00
|
|
|
Command: "restore",
|
|
|
|
RepoIdentifier: repoIdentifier,
|
|
|
|
PasswordFile: passwordFile,
|
|
|
|
Dir: target,
|
|
|
|
Args: []string{snapshotID},
|
|
|
|
ExtraFlags: []string{"--target=."},
|
2018-02-28 01:35:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSnapshotCommand returns a Command for running a restic (get) snapshots.
|
2018-06-15 03:24:01 +00:00
|
|
|
func GetSnapshotCommand(repoIdentifier, passwordFile string, tags map[string]string) *Command {
|
2018-02-28 01:35:35 +00:00
|
|
|
return &Command{
|
2018-06-15 03:24:01 +00:00
|
|
|
Command: "snapshots",
|
|
|
|
RepoIdentifier: repoIdentifier,
|
|
|
|
PasswordFile: passwordFile,
|
2022-04-11 12:49:20 +00:00
|
|
|
// "--last" is replaced by "--latest=1" in restic v0.12.1
|
|
|
|
ExtraFlags: []string{"--json", "--latest=1", getSnapshotTagFlag(tags)},
|
2018-02-28 01:35:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSnapshotTagFlag(tags map[string]string) string {
|
|
|
|
var tagFilters []string
|
|
|
|
for k, v := range tags {
|
|
|
|
tagFilters = append(tagFilters, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("--tag=%s", strings.Join(tagFilters, ","))
|
|
|
|
}
|
|
|
|
|
2018-06-15 03:24:01 +00:00
|
|
|
func InitCommand(repoIdentifier string) *Command {
|
2018-02-28 01:35:35 +00:00
|
|
|
return &Command{
|
2018-06-15 03:24:01 +00:00
|
|
|
Command: "init",
|
|
|
|
RepoIdentifier: repoIdentifier,
|
2018-02-28 01:35:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 18:00:54 +00:00
|
|
|
func SnapshotsCommand(repoIdentifier string) *Command {
|
2019-01-24 21:47:31 +00:00
|
|
|
return &Command{
|
2019-04-29 18:00:54 +00:00
|
|
|
Command: "snapshots",
|
2019-01-24 21:47:31 +00:00
|
|
|
RepoIdentifier: repoIdentifier,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 03:24:01 +00:00
|
|
|
func PruneCommand(repoIdentifier string) *Command {
|
2018-02-28 01:35:35 +00:00
|
|
|
return &Command{
|
2018-06-15 03:24:01 +00:00
|
|
|
Command: "prune",
|
|
|
|
RepoIdentifier: repoIdentifier,
|
2018-02-28 01:35:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 03:24:01 +00:00
|
|
|
func ForgetCommand(repoIdentifier, snapshotID string) *Command {
|
2018-02-28 01:35:35 +00:00
|
|
|
return &Command{
|
2018-06-15 03:24:01 +00:00
|
|
|
Command: "forget",
|
|
|
|
RepoIdentifier: repoIdentifier,
|
|
|
|
Args: []string{snapshotID},
|
2018-02-28 01:35:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-31 17:52:23 +00:00
|
|
|
|
|
|
|
func UnlockCommand(repoIdentifier string) *Command {
|
|
|
|
return &Command{
|
|
|
|
Command: "unlock",
|
|
|
|
RepoIdentifier: repoIdentifier,
|
|
|
|
}
|
|
|
|
}
|
2019-09-10 21:50:57 +00:00
|
|
|
|
|
|
|
func StatsCommand(repoIdentifier, passwordFile, snapshotID string) *Command {
|
|
|
|
return &Command{
|
|
|
|
Command: "stats",
|
|
|
|
RepoIdentifier: repoIdentifier,
|
|
|
|
PasswordFile: passwordFile,
|
|
|
|
Args: []string{snapshotID},
|
|
|
|
ExtraFlags: []string{"--json"},
|
|
|
|
}
|
|
|
|
}
|