github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/cmd/sync/sync.go (about)

     1  package sync
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/rclone/rclone/cmd"
     7  	"github.com/rclone/rclone/fs/config/flags"
     8  	"github.com/rclone/rclone/fs/operations"
     9  	"github.com/rclone/rclone/fs/sync"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  var (
    14  	createEmptySrcDirs = false
    15  )
    16  
    17  func init() {
    18  	cmd.Root.AddCommand(commandDefinition)
    19  	cmdFlags := commandDefinition.Flags()
    20  	flags.BoolVarP(cmdFlags, &createEmptySrcDirs, "create-empty-src-dirs", "", createEmptySrcDirs, "Create empty source dirs on destination after sync")
    21  }
    22  
    23  var commandDefinition = &cobra.Command{
    24  	Use:   "sync source:path dest:path",
    25  	Short: `Make source and dest identical, modifying destination only.`,
    26  	Long: `
    27  Sync the source to the destination, changing the destination
    28  only.  Doesn't transfer unchanged files, testing by size and
    29  modification time or MD5SUM.  Destination is updated to match
    30  source, including deleting files if necessary.
    31  
    32  **Important**: Since this can cause data loss, test first with the
    33  ` + "`" + `--dry-run` + "`" + ` flag to see exactly what would be copied and deleted.
    34  
    35  Note that files in the destination won't be deleted if there were any
    36  errors at any point.
    37  
    38  It is always the contents of the directory that is synced, not the
    39  directory so when source:path is a directory, it's the contents of
    40  source:path that are copied, not the directory name and contents.  See
    41  extended explanation in the ` + "`" + `copy` + "`" + ` command above if unsure.
    42  
    43  If dest:path doesn't exist, it is created and the source:path contents
    44  go there.
    45  
    46  **Note**: Use the ` + "`-P`" + `/` + "`--progress`" + ` flag to view real-time transfer statistics
    47  `,
    48  	Run: func(command *cobra.Command, args []string) {
    49  		cmd.CheckArgs(2, 2, command, args)
    50  		fsrc, srcFileName, fdst := cmd.NewFsSrcFileDst(args)
    51  		cmd.Run(true, true, command, func() error {
    52  			if srcFileName == "" {
    53  				return sync.Sync(context.Background(), fdst, fsrc, createEmptySrcDirs)
    54  			}
    55  			return operations.CopyFile(context.Background(), fdst, fsrc, srcFileName, srcFileName)
    56  		})
    57  	},
    58  }