2014-04-27 22:00:15 +00:00
package main
import (
2016-04-01 11:50:45 +00:00
"bufio"
2018-05-01 12:40:52 +00:00
"bytes"
2018-04-22 09:57:20 +00:00
"context"
2018-10-03 09:12:51 +00:00
"fmt"
2018-08-11 05:34:37 +00:00
"io"
2018-05-01 12:40:52 +00:00
"io/ioutil"
2014-09-23 20:39:12 +00:00
"os"
2019-04-28 19:59:35 +00:00
"path"
2018-07-18 16:26:24 +00:00
"path/filepath"
2020-10-24 09:35:57 +00:00
"runtime"
2020-03-19 00:44:34 +00:00
"strconv"
2016-04-16 20:04:29 +00:00
"strings"
2022-05-27 17:27:14 +00:00
"sync"
2016-04-16 20:04:29 +00:00
"time"
2016-09-17 10:36:05 +00:00
"github.com/spf13/cobra"
2022-05-27 17:27:14 +00:00
"golang.org/x/sync/errgroup"
2015-04-25 17:20:41 +00:00
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/archiver"
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors"
2022-05-07 18:52:29 +00:00
"github.com/restic/restic/internal/filter"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/fs"
2018-04-22 09:57:20 +00:00
"github.com/restic/restic/internal/repository"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
2018-05-01 12:40:52 +00:00
"github.com/restic/restic/internal/textfile"
2021-09-12 14:15:40 +00:00
"github.com/restic/restic/internal/ui/backup"
2018-04-22 09:57:20 +00:00
"github.com/restic/restic/internal/ui/termstatus"
2016-09-17 10:36:05 +00:00
)
2015-04-25 17:20:41 +00:00
2016-09-17 10:36:05 +00:00
var cmdBackup = & cobra . Command {
2022-07-16 22:03:17 +00:00
Use : "backup [flags] [FILE/DIR] ..." ,
2017-09-11 16:32:44 +00:00
Short : "Create a new backup of files and/or directories" ,
2016-09-17 10:36:05 +00:00
Long : `
The "backup" command creates a new snapshot and saves the files and directories
given as the arguments .
2019-11-05 06:03:38 +00:00
EXIT STATUS
== == == == == =
2020-01-12 15:21:17 +00:00
Exit status is 0 if the command was successful .
Exit status is 1 if there was a fatal error ( no snapshot created ) .
Exit status is 3 if some source data could not be read ( incomplete snapshot created ) .
2016-09-17 10:36:05 +00:00
` ,
2017-08-05 09:54:59 +00:00
PreRun : func ( cmd * cobra . Command , args [ ] string ) {
2018-10-03 12:05:53 +00:00
if backupOptions . Host == "" {
2017-08-05 09:54:59 +00:00
hostname , err := os . Hostname ( )
if err != nil {
debug . Log ( "os.Hostname() returned err: %v" , err )
return
}
2018-10-03 12:05:53 +00:00
backupOptions . Host = hostname
2017-08-05 09:54:59 +00:00
}
} ,
2017-08-06 19:02:16 +00:00
DisableAutoGenTag : true ,
2016-09-17 10:36:05 +00:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-10-02 21:24:37 +00:00
ctx := cmd . Context ( )
2022-05-27 17:27:14 +00:00
var wg sync . WaitGroup
2021-10-31 22:08:13 +00:00
cancelCtx , cancel := context . WithCancel ( ctx )
2022-05-27 17:27:14 +00:00
defer func ( ) {
// shutdown termstatus
cancel ( )
wg . Wait ( )
} ( )
2018-05-02 19:24:18 +00:00
term := termstatus . New ( globalOptions . stdout , globalOptions . stderr , globalOptions . Quiet )
2022-05-27 17:27:14 +00:00
wg . Add ( 1 )
go func ( ) {
defer wg . Done ( )
term . Run ( cancelCtx )
} ( )
2015-04-25 17:20:41 +00:00
2021-10-31 22:08:13 +00:00
return runBackup ( ctx , backupOptions , globalOptions , term , args )
2016-09-17 10:36:05 +00:00
} ,
2015-04-25 17:20:41 +00:00
}
2016-09-17 10:36:05 +00:00
// BackupOptions bundles all options for the backup command.
type BackupOptions struct {
2020-08-25 20:30:57 +00:00
Parent string
Force bool
Excludes [ ] string
InsensitiveExcludes [ ] string
ExcludeFiles [ ] string
InsensitiveExcludeFiles [ ] string
ExcludeOtherFS bool
ExcludeIfPresent [ ] string
ExcludeCaches bool
2020-09-03 01:18:58 +00:00
ExcludeLargerThan string
2020-08-25 20:30:57 +00:00
Stdin bool
StdinFilename string
2020-12-28 15:33:10 +00:00
Tags restic . TagLists
2020-08-25 20:30:57 +00:00
Host string
FilesFrom [ ] string
2020-11-20 11:15:16 +00:00
FilesFromVerbatim [ ] string
FilesFromRaw [ ] string
2020-08-25 20:30:57 +00:00
TimeStamp string
WithAtime bool
IgnoreInode bool
2020-07-08 07:59:00 +00:00
IgnoreCtime bool
2020-10-24 09:35:57 +00:00
UseFsSnapshot bool
backup: add --dry-run/-n flag to show what would happen.
This can be used to check how large a backup is or validate exclusions.
It does not actually write any data to the underlying backend. This is
implemented as a simple overlay backend that accepts writes without
forwarding them, passes through reads, and generally does the minimal
necessary to pretend that progress is actually happening.
Fixes #1542
Example usage:
$ restic -vv --dry-run . | grep add
new /changelog/unreleased/issue-1542, saved in 0.000s (350 B added)
modified /cmd/restic/cmd_backup.go, saved in 0.000s (16.543 KiB added)
modified /cmd/restic/global.go, saved in 0.000s (0 B added)
new /internal/backend/dry/dry_backend_test.go, saved in 0.000s (3.866 KiB added)
new /internal/backend/dry/dry_backend.go, saved in 0.000s (3.744 KiB added)
modified /internal/backend/test/tests.go, saved in 0.000s (0 B added)
modified /internal/repository/repository.go, saved in 0.000s (20.707 KiB added)
modified /internal/ui/backup.go, saved in 0.000s (9.110 KiB added)
modified /internal/ui/jsonstatus/status.go, saved in 0.001s (11.055 KiB added)
modified /restic, saved in 0.131s (25.542 MiB added)
Would add to the repo: 25.892 MiB
2019-06-13 03:39:13 +00:00
DryRun bool
2022-09-24 09:57:16 +00:00
ReadConcurrency uint
2015-04-25 17:20:41 +00:00
}
2016-09-17 10:36:05 +00:00
var backupOptions BackupOptions
2015-01-04 17:23:00 +00:00
2020-07-28 20:57:40 +00:00
// ErrInvalidSourceData is used to report an incomplete backup
2021-03-24 14:42:12 +00:00
var ErrInvalidSourceData = errors . New ( "at least one source file could not be read" )
2020-01-12 15:21:17 +00:00
2016-09-17 10:36:05 +00:00
func init ( ) {
cmdRoot . AddCommand ( cmdBackup )
f := cmdBackup . Flags ( )
2022-05-07 20:23:59 +00:00
f . StringVar ( & backupOptions . Parent , "parent" , "" , "use this parent `snapshot` (default: last snapshot in the repository that has the same target files/directories, and is not newer than the snapshot time)" )
2017-02-13 15:02:47 +00:00
f . BoolVarP ( & backupOptions . Force , "force" , "f" , false , ` force re-reading the target files/directories (overrides the "parent" flag) ` )
2017-07-07 01:19:06 +00:00
f . StringArrayVarP ( & backupOptions . Excludes , "exclude" , "e" , nil , "exclude a `pattern` (can be specified multiple times)" )
2020-04-03 17:44:33 +00:00
f . StringArrayVar ( & backupOptions . InsensitiveExcludes , "iexclude" , nil , "same as --exclude `pattern` but ignores the casing of filenames" )
2017-07-07 01:19:06 +00:00
f . StringArrayVar ( & backupOptions . ExcludeFiles , "exclude-file" , nil , "read exclude patterns from a `file` (can be specified multiple times)" )
2020-08-25 20:30:57 +00:00
f . StringArrayVar ( & backupOptions . InsensitiveExcludeFiles , "iexclude-file" , nil , "same as --exclude-file but ignores casing of `file`names in patterns" )
2020-10-08 15:59:26 +00:00
f . BoolVarP ( & backupOptions . ExcludeOtherFS , "one-file-system" , "x" , false , "exclude other file systems, don't cross filesystem boundaries and subvolumes" )
2020-04-03 17:44:33 +00:00
f . StringArrayVar ( & backupOptions . ExcludeIfPresent , "exclude-if-present" , nil , "takes `filename[:header]`, exclude contents of directories containing filename (except filename itself) if header of that file is as provided (can be specified multiple times)" )
2020-06-21 16:31:59 +00:00
f . BoolVar ( & backupOptions . ExcludeCaches , "exclude-caches" , false , ` excludes cache directories that are marked with a CACHEDIR.TAG file. See https://bford.info/cachedir/ for the Cache Directory Tagging Standard ` )
2020-09-03 01:18:58 +00:00
f . StringVar ( & backupOptions . ExcludeLargerThan , "exclude-larger-than" , "" , "max `size` of the files to be backed up (allowed suffixes: k/K, m/M, g/G, t/T)" )
2016-09-17 10:36:05 +00:00
f . BoolVar ( & backupOptions . Stdin , "stdin" , false , "read backup from stdin" )
2020-04-03 17:44:33 +00:00
f . StringVar ( & backupOptions . StdinFilename , "stdin-filename" , "stdin" , "`filename` to use when reading from stdin" )
2020-04-13 02:29:13 +00:00
f . Var ( & backupOptions . Tags , "tag" , "add `tags` for the new snapshot in the format `tag[,tag,...]` (can be specified multiple times)" )
2022-10-08 09:27:39 +00:00
f . UintVar ( & backupOptions . ReadConcurrency , "read-concurrency" , 0 , "read `n` files concurrently. (default: $RESTIC_READ_CONCURRENCY or 2)" )
2018-11-14 21:58:31 +00:00
f . StringVarP ( & backupOptions . Host , "host" , "H" , "" , "set the `hostname` for the snapshot manually. To prevent an expensive rescan use the \"parent\" flag" )
2018-10-03 12:05:53 +00:00
f . StringVar ( & backupOptions . Host , "hostname" , "" , "set the `hostname` for the snapshot manually" )
2022-09-24 09:57:16 +00:00
err := f . MarkDeprecated ( "hostname" , "use --host" )
2021-01-30 15:48:09 +00:00
if err != nil {
// MarkDeprecated only returns an error when the flag could not be found
panic ( err )
}
2020-11-20 11:15:16 +00:00
f . StringArrayVar ( & backupOptions . FilesFrom , "files-from" , nil , "read the files to backup from `file` (can be combined with file args; can be specified multiple times)" )
f . StringArrayVar ( & backupOptions . FilesFromVerbatim , "files-from-verbatim" , nil , "read the files to backup from `file` (can be combined with file args; can be specified multiple times)" )
f . StringArrayVar ( & backupOptions . FilesFromRaw , "files-from-raw" , nil , "read the files to backup from `file` (can be combined with file args; can be specified multiple times)" )
2020-04-03 17:44:33 +00:00
f . StringVar ( & backupOptions . TimeStamp , "time" , "" , "`time` of the backup (ex. '2012-11-01 22:08:41') (default: now)" )
2017-11-28 20:31:35 +00:00
f . BoolVar ( & backupOptions . WithAtime , "with-atime" , false , "store the atime for all files and directories" )
2019-03-10 20:22:54 +00:00
f . BoolVar ( & backupOptions . IgnoreInode , "ignore-inode" , false , "ignore inode number changes when checking for modified files" )
2020-07-08 07:59:00 +00:00
f . BoolVar ( & backupOptions . IgnoreCtime , "ignore-ctime" , false , "ignore ctime changes when checking for modified files" )
2021-01-02 17:00:06 +00:00
f . BoolVarP ( & backupOptions . DryRun , "dry-run" , "n" , false , "do not upload or write any data, just show what would be done" )
2020-10-24 09:35:57 +00:00
if runtime . GOOS == "windows" {
f . BoolVar ( & backupOptions . UseFsSnapshot , "use-fs-snapshot" , false , "use filesystem snapshot where possible (currently only Windows VSS)" )
}
2020-03-19 00:44:34 +00:00
2022-09-24 09:57:16 +00:00
// parse read concurrency from env, on error the default value will be used
readConcurrency , _ := strconv . ParseUint ( os . Getenv ( "RESTIC_READ_CONCURRENCY" ) , 10 , 32 )
backupOptions . ReadConcurrency = uint ( readConcurrency )
2014-12-07 15:30:52 +00:00
}
2015-07-06 21:02:16 +00:00
// filterExisting returns a slice of all existing items, or an error if no
// items exist at all.
func filterExisting ( items [ ] string ) ( result [ ] string , err error ) {
for _ , item := range items {
Fix 567 (#570)
* Patch for https://github.com/restic/restic/issues/567
Backup also files on windows with longer pathnames than 255 chars (e.g. from node).
as fd0 says "So, as far as I can see, we need to have custom methods for all functions that accept a path, so that on Windows we can substitute the normal (possibly relative) path used within restic by an (absolute) UNC path, and only then call the underlying functions like os.Stat(), os.Lstat(), os.Open() and so on.
I've already thought about adding a generic abstraction for the file system (so we can mock this easier in tests), and this looks like a good opportunity to build it."
* fixed building tests
* Restructured patches
Add Wrapper for filepath.Walk
* using \\?\ requires absolute pathes to be used.
Now all tests run
* used gofmt on the code
* Restructured Code. No patches dir, integrate the file functions into restic/fs/
There is still an issue, because restic.fs.Open has a different api the os.Open, which returns the result of OpenFile, but takes only a string
* Changed the last os.Open() calls to fs.Open() after extending the File interface
* fixed name-clash of restic.fs and fuse.fs detected by travis
* fixed fmt with gofmt
* c&p failure: removed fixpath() call.
* missing include
* fixed includes in linux variant
* Fix for Linux. Fd() is required on File interface
* done gofmt
2016-08-15 19:59:13 +00:00
_ , err := fs . Lstat ( item )
2022-06-13 18:35:37 +00:00
if errors . Is ( err , os . ErrNotExist ) {
2017-09-09 19:12:41 +00:00
Warnf ( "%v does not exist, skipping\n" , item )
2015-07-06 21:02:16 +00:00
continue
}
result = append ( result , item )
}
if len ( result ) == 0 {
2016-09-01 20:17:37 +00:00
return nil , errors . Fatal ( "all target directories/files do not exist" )
2015-07-06 21:02:16 +00:00
}
return
}
2020-11-20 11:15:16 +00:00
// readLines reads all lines from the named file and returns them as a
// string slice.
//
// If filename is empty, readPatternsFromFile returns an empty slice.
// If filename is a dash (-), readPatternsFromFile will read the lines from the
2018-04-22 09:57:20 +00:00
// standard input.
2020-11-20 11:15:16 +00:00
func readLines ( filename string ) ( [ ] string , error ) {
2016-11-03 17:48:07 +00:00
if filename == "" {
return nil , nil
}
2018-05-01 12:40:52 +00:00
var (
data [ ] byte
err error
)
if filename == "-" {
data , err = ioutil . ReadAll ( os . Stdin )
} else {
data , err = textfile . Read ( filename )
}
if err != nil {
return nil , err
2016-11-03 17:48:07 +00:00
}
var lines [ ] string
2018-05-01 12:40:52 +00:00
scanner := bufio . NewScanner ( bytes . NewReader ( data ) )
2016-11-03 17:48:07 +00:00
for scanner . Scan ( ) {
2020-11-20 11:15:16 +00:00
lines = append ( lines , scanner . Text ( ) )
2016-11-03 17:48:07 +00:00
}
if err := scanner . Err ( ) ; err != nil {
return nil , err
}
return lines , nil
}
2020-11-20 11:15:16 +00:00
// readFilenamesFromFileRaw reads a list of filenames from the given file,
// or stdin if filename is "-". Each filename is terminated by a zero byte,
// which is stripped off.
func readFilenamesFromFileRaw ( filename string ) ( names [ ] string , err error ) {
f := os . Stdin
if filename != "-" {
if f , err = os . Open ( filename ) ; err != nil {
return nil , err
}
}
2021-01-30 15:49:15 +00:00
names , err = readFilenamesRaw ( f )
if err != nil {
// ignore subsequent errors
_ = f . Close ( )
return nil , err
}
err = f . Close ( )
if err != nil {
return nil , err
}
return names , nil
2020-11-20 11:15:16 +00:00
}
func readFilenamesRaw ( r io . Reader ) ( names [ ] string , err error ) {
br := bufio . NewReader ( r )
for {
name , err := br . ReadString ( 0 )
switch err {
case nil :
case io . EOF :
if name == "" {
return names , nil
}
return nil , errors . Fatal ( "--files-from-raw: trailing zero byte missing" )
default :
return nil , err
}
name = name [ : len ( name ) - 1 ]
if name == "" {
// The empty filename is never valid. Handle this now to
// prevent downstream code from erroneously backing up
// filepath.Clean("") == ".".
return nil , errors . Fatal ( "--files-from-raw: empty filename in listing" )
}
names = append ( names , name )
}
}
2018-04-22 09:57:20 +00:00
// Check returns an error when an invalid combination of options was set.
func ( opts BackupOptions ) Check ( gopts GlobalOptions , args [ ] string ) error {
2018-11-12 16:54:37 +00:00
if gopts . password == "" {
2020-11-20 11:15:16 +00:00
filesFrom := append ( append ( opts . FilesFrom , opts . FilesFromVerbatim ... ) , opts . FilesFromRaw ... )
for _ , filename := range filesFrom {
2018-11-12 16:54:37 +00:00
if filename == "-" {
return errors . Fatal ( "unable to read password from stdin when data is to be read from stdin, use --password-file or $RESTIC_PASSWORD" )
}
}
2017-02-06 11:43:44 +00:00
}
2018-04-22 09:57:20 +00:00
if opts . Stdin {
2018-11-12 16:54:37 +00:00
if len ( opts . FilesFrom ) > 0 {
2018-04-22 09:57:20 +00:00
return errors . Fatal ( "--stdin and --files-from cannot be used together" )
2015-03-02 13:48:47 +00:00
}
2020-11-20 11:15:16 +00:00
if len ( opts . FilesFromVerbatim ) > 0 {
return errors . Fatal ( "--stdin and --files-from-verbatim cannot be used together" )
}
if len ( opts . FilesFromRaw ) > 0 {
return errors . Fatal ( "--stdin and --files-from-raw cannot be used together" )
}
2015-03-02 13:48:47 +00:00
2018-04-22 09:57:20 +00:00
if len ( args ) > 0 {
return errors . Fatal ( "--stdin was specified and files/dirs were listed as arguments" )
}
2015-07-06 21:02:16 +00:00
}
2018-04-22 09:57:20 +00:00
return nil
}
2017-09-10 13:13:40 +00:00
2018-07-31 15:25:25 +00:00
// collectRejectByNameFuncs returns a list of all functions which may reject data
// from being saved in a snapshot based on path only
func collectRejectByNameFuncs ( opts BackupOptions , repo * repository . Repository , targets [ ] string ) ( fs [ ] RejectByNameFunc , err error ) {
2018-04-22 09:57:20 +00:00
// exclude restic cache
if repo . Cache != nil {
f , err := rejectResticCache ( repo )
if err != nil {
return nil , err
}
fs = append ( fs , f )
2017-09-10 13:13:40 +00:00
}
// add patterns from file
if len ( opts . ExcludeFiles ) > 0 {
2018-07-18 19:39:07 +00:00
excludes , err := readExcludePatternsFromFiles ( opts . ExcludeFiles )
if err != nil {
return nil , err
}
2022-05-07 18:52:29 +00:00
2022-09-09 20:29:05 +00:00
if err := filter . ValidatePatterns ( excludes ) ; err != nil {
return nil , errors . Fatalf ( "--exclude-file: %s" , err )
2022-05-07 18:52:29 +00:00
}
2018-07-18 19:39:07 +00:00
opts . Excludes = append ( opts . Excludes , excludes ... )
2017-09-10 13:13:40 +00:00
}
2020-08-25 20:30:57 +00:00
if len ( opts . InsensitiveExcludeFiles ) > 0 {
excludes , err := readExcludePatternsFromFiles ( opts . InsensitiveExcludeFiles )
if err != nil {
return nil , err
}
2022-05-07 18:52:29 +00:00
2022-09-09 20:29:05 +00:00
if err := filter . ValidatePatterns ( excludes ) ; err != nil {
return nil , errors . Fatalf ( "--iexclude-file: %s" , err )
2022-05-07 18:52:29 +00:00
}
2020-08-25 20:30:57 +00:00
opts . InsensitiveExcludes = append ( opts . InsensitiveExcludes , excludes ... )
}
2018-10-16 20:39:14 +00:00
if len ( opts . InsensitiveExcludes ) > 0 {
2022-09-09 20:29:05 +00:00
if err := filter . ValidatePatterns ( opts . InsensitiveExcludes ) ; err != nil {
return nil , errors . Fatalf ( "--iexclude: %s" , err )
2022-05-07 18:52:29 +00:00
}
2018-10-16 20:39:14 +00:00
fs = append ( fs , rejectByInsensitivePattern ( opts . InsensitiveExcludes ) )
}
2017-09-10 13:13:40 +00:00
if len ( opts . Excludes ) > 0 {
2022-09-09 20:29:05 +00:00
if err := filter . ValidatePatterns ( opts . Excludes ) ; err != nil {
return nil , errors . Fatalf ( "--exclude: %s" , err )
2022-05-07 18:52:29 +00:00
}
2018-04-22 09:57:20 +00:00
fs = append ( fs , rejectByPattern ( opts . Excludes ) )
2017-09-10 13:13:40 +00:00
}
if opts . ExcludeCaches {
opts . ExcludeIfPresent = append ( opts . ExcludeIfPresent , "CACHEDIR.TAG:Signature: 8a477f597d28d172789f06886806bc55" )
}
for _ , spec := range opts . ExcludeIfPresent {
2017-11-27 16:30:53 +00:00
f , err := rejectIfPresent ( spec )
2016-09-18 15:10:33 +00:00
if err != nil {
2018-04-22 09:57:20 +00:00
return nil , err
2016-09-18 15:10:33 +00:00
}
2017-09-10 13:13:40 +00:00
2018-04-22 09:57:20 +00:00
fs = append ( fs , f )
2016-09-18 15:10:33 +00:00
}
2018-04-22 09:57:20 +00:00
return fs , nil
}
2014-04-27 22:00:15 +00:00
2018-07-31 15:25:25 +00:00
// collectRejectFuncs returns a list of all functions which may reject data
// from being saved in a snapshot based on path and file info
func collectRejectFuncs ( opts BackupOptions , repo * repository . Repository , targets [ ] string ) ( fs [ ] RejectFunc , err error ) {
// allowed devices
if opts . ExcludeOtherFS && ! opts . Stdin {
f , err := rejectByDevice ( targets )
if err != nil {
return nil , err
}
fs = append ( fs , f )
}
2020-09-03 01:18:58 +00:00
if len ( opts . ExcludeLargerThan ) != 0 && ! opts . Stdin {
f , err := rejectBySize ( opts . ExcludeLargerThan )
if err != nil {
return nil , err
}
fs = append ( fs , f )
}
2018-07-31 15:25:25 +00:00
return fs , nil
}
2018-04-22 09:57:20 +00:00
// readExcludePatternsFromFiles reads all exclude files and returns the list of
2018-06-22 18:46:04 +00:00
// exclude patterns. For each line, leading and trailing white space is removed
// and comment lines are ignored. For each remaining pattern, environment
// variables are resolved. For adding a literal dollar sign ($), write $$ to
// the file.
2018-07-18 19:39:07 +00:00
func readExcludePatternsFromFiles ( excludeFiles [ ] string ) ( [ ] string , error ) {
2018-06-22 18:46:04 +00:00
getenvOrDollar := func ( s string ) string {
if s == "$" {
return "$"
}
return os . Getenv ( s )
}
2018-04-22 09:57:20 +00:00
var excludes [ ] string
for _ , filename := range excludeFiles {
err := func ( ) ( err error ) {
2018-05-01 12:40:52 +00:00
data , err := textfile . Read ( filename )
2018-04-22 09:57:20 +00:00
if err != nil {
return err
}
2015-06-27 12:40:18 +00:00
2018-05-01 12:40:52 +00:00
scanner := bufio . NewScanner ( bytes . NewReader ( data ) )
2018-04-22 09:57:20 +00:00
for scanner . Scan ( ) {
line := strings . TrimSpace ( scanner . Text ( ) )
// ignore empty lines
if line == "" {
continue
}
// strip comments
if strings . HasPrefix ( line , "#" ) {
continue
}
2018-06-22 18:46:04 +00:00
line = os . Expand ( line , getenvOrDollar )
2018-04-22 09:57:20 +00:00
excludes = append ( excludes , line )
}
return scanner . Err ( )
} ( )
2017-09-11 19:37:10 +00:00
if err != nil {
2018-07-18 19:39:07 +00:00
return nil , err
2017-09-11 19:37:10 +00:00
}
2018-04-22 09:57:20 +00:00
}
2018-07-18 19:39:07 +00:00
return excludes , nil
2018-04-22 09:57:20 +00:00
}
2017-09-11 19:37:10 +00:00
2018-04-22 09:57:20 +00:00
// collectTargets returns a list of target files/dirs from several sources.
func collectTargets ( opts BackupOptions , args [ ] string ) ( targets [ ] string , err error ) {
if opts . Stdin {
return nil , nil
2017-09-11 19:37:10 +00:00
}
2018-11-12 16:54:37 +00:00
for _ , file := range opts . FilesFrom {
2020-11-20 11:15:16 +00:00
fromfile , err := readLines ( file )
2018-07-18 16:26:24 +00:00
if err != nil {
2018-11-12 16:54:37 +00:00
return nil , err
2018-07-18 16:26:24 +00:00
}
2018-11-12 16:54:37 +00:00
// expand wildcards
for _ , line := range fromfile {
2020-11-20 11:15:16 +00:00
line = strings . TrimSpace ( line )
if line == "" || line [ 0 ] == '#' { // '#' marks a comment.
continue
}
2018-11-12 16:54:37 +00:00
var expanded [ ] string
expanded , err := filepath . Glob ( line )
if err != nil {
2022-10-09 09:23:07 +00:00
return nil , fmt . Errorf ( "pattern: %s: %w" , line , err )
2018-11-12 16:54:37 +00:00
}
if len ( expanded ) == 0 {
Warnf ( "pattern %q does not match any files, skipping\n" , line )
}
2020-11-20 11:15:16 +00:00
targets = append ( targets , expanded ... )
}
}
for _ , file := range opts . FilesFromVerbatim {
fromfile , err := readLines ( file )
if err != nil {
return nil , err
}
for _ , line := range fromfile {
if line == "" {
continue
}
targets = append ( targets , line )
}
}
for _ , file := range opts . FilesFromRaw {
fromfile , err := readFilenamesFromFileRaw ( file )
if err != nil {
return nil , err
2018-11-03 13:51:30 +00:00
}
2020-11-20 11:15:16 +00:00
targets = append ( targets , fromfile ... )
2018-07-18 16:26:24 +00:00
}
2020-11-20 11:15:16 +00:00
// Merge args into files-from so we can reuse the normal args checks
// and have the ability to use both files-from and args at the same time.
targets = append ( targets , args ... )
if len ( targets ) == 0 && ! opts . Stdin {
2018-04-22 09:57:20 +00:00
return nil , errors . Fatal ( "nothing to backup, please specify target files/dirs" )
2015-04-26 15:44:38 +00:00
}
2018-04-22 09:57:20 +00:00
targets , err = filterExisting ( targets )
if err != nil {
return nil , err
}
return targets , nil
}
2014-11-30 21:34:21 +00:00
2018-04-22 09:57:20 +00:00
// parent returns the ID of the parent snapshot. If there is none, nil is
// returned.
2022-02-05 21:42:38 +00:00
func findParentSnapshot ( ctx context . Context , repo restic . Repository , opts BackupOptions , targets [ ] string , timeStampLimit time . Time ) ( parentID * restic . ID , err error ) {
2015-04-03 19:18:09 +00:00
// Force using a parent
2016-09-17 10:36:05 +00:00
if ! opts . Force && opts . Parent != "" {
2021-11-06 00:14:24 +00:00
id , err := restic . FindSnapshot ( ctx , repo . Backend ( ) , opts . Parent )
2014-11-30 21:34:21 +00:00
if err != nil {
2018-04-22 09:57:20 +00:00
return nil , errors . Fatalf ( "invalid id %q: %v" , opts . Parent , err )
2014-11-30 21:34:21 +00:00
}
2018-04-22 09:57:20 +00:00
parentID = & id
2014-11-30 21:34:21 +00:00
}
2014-04-27 22:00:15 +00:00
2015-04-03 19:18:09 +00:00
// Find last snapshot to set it as parent, if not already set
2018-04-22 09:57:20 +00:00
if ! opts . Force && parentID == nil {
2021-11-06 00:14:24 +00:00
id , err := restic . FindLatestSnapshot ( ctx , repo . Backend ( ) , repo , targets , [ ] restic . TagList { } , [ ] string { opts . Host } , & timeStampLimit )
2015-07-25 15:05:45 +00:00
if err == nil {
2018-04-22 09:57:20 +00:00
parentID = & id
2016-04-27 16:36:48 +00:00
} else if err != restic . ErrNoSnapshotFound {
2018-04-22 09:57:20 +00:00
return nil , err
2015-04-03 19:18:09 +00:00
}
}
2018-04-22 09:57:20 +00:00
return parentID , nil
}
2021-10-31 22:08:13 +00:00
func runBackup ( ctx context . Context , opts BackupOptions , gopts GlobalOptions , term * termstatus . Terminal , args [ ] string ) error {
2018-04-22 09:57:20 +00:00
err := opts . Check ( gopts , args )
if err != nil {
return err
2015-07-25 16:09:38 +00:00
}
2018-04-22 09:57:20 +00:00
targets , err := collectTargets ( opts , args )
if err != nil {
return err
}
2014-11-16 20:29:11 +00:00
2018-06-01 16:41:44 +00:00
timeStamp := time . Now ( )
if opts . TimeStamp != "" {
2018-11-20 17:01:36 +00:00
timeStamp , err = time . ParseInLocation ( TimeFormat , opts . TimeStamp , time . Local )
2018-06-01 16:41:44 +00:00
if err != nil {
return errors . Fatalf ( "error in time option: %v\n" , err )
}
}
2018-08-11 05:34:37 +00:00
if gopts . verbosity >= 2 && ! gopts . JSON {
2020-05-01 20:38:03 +00:00
Verbosef ( "open repository\n" )
2019-01-10 20:27:56 +00:00
}
2018-08-11 05:34:37 +00:00
2021-10-31 22:08:13 +00:00
repo , err := OpenRepository ( ctx , gopts )
2018-11-10 11:38:29 +00:00
if err != nil {
return err
}
2021-09-12 14:15:40 +00:00
var progressPrinter backup . ProgressPrinter
2018-08-11 05:34:37 +00:00
if gopts . JSON {
2021-09-12 14:15:40 +00:00
progressPrinter = backup . NewJSONProgress ( term , gopts . verbosity )
2018-08-11 05:34:37 +00:00
} else {
2021-09-12 14:15:40 +00:00
progressPrinter = backup . NewTextProgress ( term , gopts . verbosity )
2018-08-11 05:34:37 +00:00
}
2021-09-12 14:15:40 +00:00
progressReporter := backup . NewProgress ( progressPrinter )
2018-04-22 09:57:20 +00:00
backup: add --dry-run/-n flag to show what would happen.
This can be used to check how large a backup is or validate exclusions.
It does not actually write any data to the underlying backend. This is
implemented as a simple overlay backend that accepts writes without
forwarding them, passes through reads, and generally does the minimal
necessary to pretend that progress is actually happening.
Fixes #1542
Example usage:
$ restic -vv --dry-run . | grep add
new /changelog/unreleased/issue-1542, saved in 0.000s (350 B added)
modified /cmd/restic/cmd_backup.go, saved in 0.000s (16.543 KiB added)
modified /cmd/restic/global.go, saved in 0.000s (0 B added)
new /internal/backend/dry/dry_backend_test.go, saved in 0.000s (3.866 KiB added)
new /internal/backend/dry/dry_backend.go, saved in 0.000s (3.744 KiB added)
modified /internal/backend/test/tests.go, saved in 0.000s (0 B added)
modified /internal/repository/repository.go, saved in 0.000s (20.707 KiB added)
modified /internal/ui/backup.go, saved in 0.000s (9.110 KiB added)
modified /internal/ui/jsonstatus/status.go, saved in 0.001s (11.055 KiB added)
modified /restic, saved in 0.131s (25.542 MiB added)
Would add to the repo: 25.892 MiB
2019-06-13 03:39:13 +00:00
if opts . DryRun {
repo . SetDryRun ( )
2021-08-18 11:03:08 +00:00
progressReporter . SetDryRun ( )
backup: add --dry-run/-n flag to show what would happen.
This can be used to check how large a backup is or validate exclusions.
It does not actually write any data to the underlying backend. This is
implemented as a simple overlay backend that accepts writes without
forwarding them, passes through reads, and generally does the minimal
necessary to pretend that progress is actually happening.
Fixes #1542
Example usage:
$ restic -vv --dry-run . | grep add
new /changelog/unreleased/issue-1542, saved in 0.000s (350 B added)
modified /cmd/restic/cmd_backup.go, saved in 0.000s (16.543 KiB added)
modified /cmd/restic/global.go, saved in 0.000s (0 B added)
new /internal/backend/dry/dry_backend_test.go, saved in 0.000s (3.866 KiB added)
new /internal/backend/dry/dry_backend.go, saved in 0.000s (3.744 KiB added)
modified /internal/backend/test/tests.go, saved in 0.000s (0 B added)
modified /internal/repository/repository.go, saved in 0.000s (20.707 KiB added)
modified /internal/ui/backup.go, saved in 0.000s (9.110 KiB added)
modified /internal/ui/jsonstatus/status.go, saved in 0.001s (11.055 KiB added)
modified /restic, saved in 0.131s (25.542 MiB added)
Would add to the repo: 25.892 MiB
2019-06-13 03:39:13 +00:00
}
2018-04-22 09:57:20 +00:00
// use the terminal for stdout/stderr
prevStdout , prevStderr := gopts . stdout , gopts . stderr
defer func ( ) {
gopts . stdout , gopts . stderr = prevStdout , prevStderr
} ( )
2021-01-26 19:52:00 +00:00
gopts . stdout , gopts . stderr = progressPrinter . Stdout ( ) , progressPrinter . Stderr ( )
2018-04-22 09:57:20 +00:00
2021-08-18 11:03:25 +00:00
progressReporter . SetMinUpdatePause ( calculateProgressInterval ( ! gopts . Quiet , gopts . JSON ) )
2014-11-16 20:29:11 +00:00
2021-10-31 22:08:13 +00:00
wg , wgCtx := errgroup . WithContext ( ctx )
2022-05-27 17:27:14 +00:00
cancelCtx , cancel := context . WithCancel ( wgCtx )
defer cancel ( )
wg . Go ( func ( ) error { return progressReporter . Run ( cancelCtx ) } )
2018-04-22 09:57:20 +00:00
2018-08-11 05:34:37 +00:00
if ! gopts . JSON {
2021-01-26 19:52:00 +00:00
progressPrinter . V ( "lock repository" )
2018-08-11 05:34:37 +00:00
}
2021-10-31 22:19:27 +00:00
lock , ctx , err := lockRepo ( ctx , repo )
2018-04-22 09:57:20 +00:00
defer unlockRepo ( lock )
if err != nil {
return err
}
2018-07-31 15:25:25 +00:00
// rejectByNameFuncs collect functions that can reject items from the backup based on path only
rejectByNameFuncs , err := collectRejectByNameFuncs ( opts , repo , targets )
if err != nil {
return err
}
// rejectFuncs collect functions that can reject items from the backup based on path and file info
2018-04-22 09:57:20 +00:00
rejectFuncs , err := collectRejectFuncs ( opts , repo , targets )
if err != nil {
return err
2015-07-19 22:13:39 +00:00
}
2014-11-16 20:29:11 +00:00
2022-02-06 16:43:22 +00:00
var parentSnapshotID * restic . ID
if ! opts . Stdin {
2021-10-31 22:08:13 +00:00
parentSnapshotID , err = findParentSnapshot ( ctx , repo , opts , targets , timeStamp )
2022-02-06 16:43:22 +00:00
if err != nil {
return err
}
2018-04-22 09:57:20 +00:00
2022-02-06 16:43:22 +00:00
if ! gopts . JSON {
if parentSnapshotID != nil {
progressPrinter . P ( "using parent snapshot %v\n" , parentSnapshotID . Str ( ) )
} else {
progressPrinter . P ( "no parent snapshot found, will read all files\n" )
}
2020-11-08 19:15:44 +00:00
}
2018-04-22 09:57:20 +00:00
}
2021-11-05 23:32:46 +00:00
if ! gopts . JSON {
progressPrinter . V ( "load index files" )
}
2021-10-31 22:08:13 +00:00
err = repo . LoadIndex ( ctx )
2021-11-05 23:32:46 +00:00
if err != nil {
return err
}
2018-07-31 15:25:25 +00:00
selectByNameFilter := func ( item string ) bool {
for _ , reject := range rejectByNameFuncs {
if reject ( item ) {
return false
}
}
return true
}
2018-04-22 09:57:20 +00:00
selectFilter := func ( item string , fi os . FileInfo ) bool {
for _ , reject := range rejectFuncs {
if reject ( item , fi ) {
return false
}
}
return true
2014-11-16 20:29:11 +00:00
}
2018-04-22 09:57:20 +00:00
var targetFS fs . FS = fs . Local { }
2020-10-24 09:35:57 +00:00
if runtime . GOOS == "windows" && opts . UseFsSnapshot {
2020-11-04 21:14:18 +00:00
if err = fs . HasSufficientPrivilegesForVSS ( ) ; err != nil {
return err
2020-10-24 09:35:57 +00:00
}
errorHandler := func ( item string , err error ) error {
2022-05-20 22:31:26 +00:00
return progressReporter . Error ( item , err )
2020-10-24 09:35:57 +00:00
}
messageHandler := func ( msg string , args ... interface { } ) {
if ! gopts . JSON {
2021-01-26 19:52:00 +00:00
progressPrinter . P ( msg , args ... )
2020-10-24 09:35:57 +00:00
}
}
localVss := fs . NewLocalVss ( errorHandler , messageHandler )
defer localVss . DeleteSnapshots ( )
targetFS = localVss
}
2018-04-22 09:57:20 +00:00
if opts . Stdin {
2018-08-11 05:34:37 +00:00
if ! gopts . JSON {
2021-01-26 19:52:00 +00:00
progressPrinter . V ( "read data from stdin" )
2018-08-11 05:34:37 +00:00
}
2019-04-28 19:59:35 +00:00
filename := path . Join ( "/" , opts . StdinFilename )
2018-04-22 09:57:20 +00:00
targetFS = & fs . Reader {
ModTime : timeStamp ,
2019-04-28 19:59:35 +00:00
Name : filename ,
2018-04-22 09:57:20 +00:00
Mode : 0644 ,
ReadCloser : os . Stdin ,
}
2019-04-28 19:59:35 +00:00
targets = [ ] string { filename }
2014-11-16 20:29:11 +00:00
}
2018-04-22 09:57:20 +00:00
sc := archiver . NewScanner ( targetFS )
2018-07-31 15:25:25 +00:00
sc . SelectByName = selectByNameFilter
2018-04-22 09:57:20 +00:00
sc . Select = selectFilter
2021-01-26 19:52:00 +00:00
sc . Error = progressReporter . ScannerError
sc . Result = progressReporter . ReportTotal
2014-04-27 22:00:15 +00:00
2018-08-11 05:34:37 +00:00
if ! gopts . JSON {
2021-01-26 19:52:00 +00:00
progressPrinter . V ( "start scan on %v" , targets )
2018-08-11 05:34:37 +00:00
}
2022-05-27 17:27:14 +00:00
wg . Go ( func ( ) error { return sc . Scan ( cancelCtx , targets ) } )
2017-08-01 17:20:09 +00:00
2022-09-24 09:57:16 +00:00
arch := archiver . New ( repo , targetFS , archiver . Options { ReadConcurrency : backupOptions . ReadConcurrency } )
2018-07-31 15:25:25 +00:00
arch . SelectByName = selectByNameFilter
2018-04-22 09:57:20 +00:00
arch . Select = selectFilter
arch . WithAtime = opts . WithAtime
2020-01-12 15:21:17 +00:00
success := true
2022-05-20 22:31:26 +00:00
arch . Error = func ( item string , err error ) error {
2020-01-12 15:21:17 +00:00
success = false
2022-05-20 22:31:26 +00:00
return progressReporter . Error ( item , err )
2020-01-12 15:21:17 +00:00
}
2021-01-26 19:52:00 +00:00
arch . CompleteItem = progressReporter . CompleteItem
arch . StartFile = progressReporter . StartFile
arch . CompleteBlob = progressReporter . CompleteBlob
2020-07-08 07:59:00 +00:00
if opts . IgnoreInode {
// --ignore-inode implies --ignore-ctime: on FUSE, the ctime is not
// reliable either.
arch . ChangeIgnoreFlags |= archiver . ChangeIgnoreCtime | archiver . ChangeIgnoreInode
}
if opts . IgnoreCtime {
arch . ChangeIgnoreFlags |= archiver . ChangeIgnoreCtime
}
2017-08-01 17:20:09 +00:00
2018-04-22 09:57:20 +00:00
if parentSnapshotID == nil {
parentSnapshotID = & restic . ID { }
}
2017-08-01 17:20:09 +00:00
2018-04-22 09:57:20 +00:00
snapshotOpts := archiver . SnapshotOptions {
Excludes : opts . Excludes ,
2020-12-28 15:33:10 +00:00
Tags : opts . Tags . Flatten ( ) ,
2018-04-22 09:57:20 +00:00
Time : timeStamp ,
2018-10-03 12:05:53 +00:00
Hostname : opts . Host ,
2018-04-22 09:57:20 +00:00
ParentSnapshot : * parentSnapshotID ,
}
2017-08-01 17:20:09 +00:00
2018-08-11 05:34:37 +00:00
if ! gopts . JSON {
2021-01-26 19:52:00 +00:00
progressPrinter . V ( "start backup on %v" , targets )
2018-08-11 05:34:37 +00:00
}
2021-10-31 22:08:13 +00:00
_ , id , err := arch . Snapshot ( ctx , targets , snapshotOpts )
2018-04-22 09:57:20 +00:00
// cleanly shutdown all running goroutines
2022-05-27 17:27:14 +00:00
cancel ( )
2018-04-22 09:57:20 +00:00
// let's see if one returned an error
2022-05-27 17:27:14 +00:00
werr := wg . Wait ( )
2021-05-15 21:06:12 +00:00
// return original error
if err != nil {
return errors . Fatalf ( "unable to save snapshot: %v" , err )
}
2020-01-12 13:41:24 +00:00
// Report finished execution
2021-01-26 19:52:00 +00:00
progressReporter . Finish ( id )
backup: add --dry-run/-n flag to show what would happen.
This can be used to check how large a backup is or validate exclusions.
It does not actually write any data to the underlying backend. This is
implemented as a simple overlay backend that accepts writes without
forwarding them, passes through reads, and generally does the minimal
necessary to pretend that progress is actually happening.
Fixes #1542
Example usage:
$ restic -vv --dry-run . | grep add
new /changelog/unreleased/issue-1542, saved in 0.000s (350 B added)
modified /cmd/restic/cmd_backup.go, saved in 0.000s (16.543 KiB added)
modified /cmd/restic/global.go, saved in 0.000s (0 B added)
new /internal/backend/dry/dry_backend_test.go, saved in 0.000s (3.866 KiB added)
new /internal/backend/dry/dry_backend.go, saved in 0.000s (3.744 KiB added)
modified /internal/backend/test/tests.go, saved in 0.000s (0 B added)
modified /internal/repository/repository.go, saved in 0.000s (20.707 KiB added)
modified /internal/ui/backup.go, saved in 0.000s (9.110 KiB added)
modified /internal/ui/jsonstatus/status.go, saved in 0.001s (11.055 KiB added)
modified /restic, saved in 0.131s (25.542 MiB added)
Would add to the repo: 25.892 MiB
2019-06-13 03:39:13 +00:00
if ! gopts . JSON && ! opts . DryRun {
2021-01-26 19:52:00 +00:00
progressPrinter . P ( "snapshot %s saved\n" , id . Str ( ) )
2018-04-22 09:57:20 +00:00
}
2020-01-12 15:21:17 +00:00
if ! success {
2020-07-28 20:57:40 +00:00
return ErrInvalidSourceData
2020-01-12 15:21:17 +00:00
}
2018-04-22 09:57:20 +00:00
2020-01-12 13:41:24 +00:00
// Return error if any
2021-05-15 21:06:12 +00:00
return werr
2017-08-01 17:20:09 +00:00
}