2013-06-27 19:00:01 +00:00
|
|
|
// Sync files and directories to and from local and remote object stores
|
2013-06-27 18:51:03 +00:00
|
|
|
//
|
2012-11-18 17:32:31 +00:00
|
|
|
// Nick Craig-Wood <nick@craig-wood.com>
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2012-11-29 22:13:58 +00:00
|
|
|
"runtime"
|
2012-11-18 17:32:31 +00:00
|
|
|
"runtime/pprof"
|
2012-11-28 11:17:31 +00:00
|
|
|
"strings"
|
2013-01-03 22:50:00 +00:00
|
|
|
"time"
|
2014-03-15 16:06:11 +00:00
|
|
|
|
2015-08-29 17:14:24 +00:00
|
|
|
"github.com/spf13/pflag"
|
2014-03-27 16:55:29 +00:00
|
|
|
|
2014-03-15 16:06:11 +00:00
|
|
|
"github.com/ncw/rclone/fs"
|
2016-01-03 14:12:01 +00:00
|
|
|
_ "github.com/ncw/rclone/fs/all" // import all fs
|
2012-11-18 17:32:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
// Flags
|
2014-03-27 16:55:29 +00:00
|
|
|
cpuprofile = pflag.StringP("cpuprofile", "", "", "Write cpu profile to file")
|
2015-02-27 15:22:26 +00:00
|
|
|
statsInterval = pflag.DurationP("stats", "", time.Minute*1, "Interval to print stats (0 to disable)")
|
2014-04-24 16:59:05 +00:00
|
|
|
version = pflag.BoolP("version", "V", false, "Print the version number")
|
2015-02-27 16:51:17 +00:00
|
|
|
logFile = pflag.StringP("log-file", "", "", "Log everything to this file")
|
2015-08-20 20:07:00 +00:00
|
|
|
retries = pflag.IntP("retries", "", 3, "Retry operations this many times if they fail")
|
2012-11-18 17:32:31 +00:00
|
|
|
)
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Command holds info about the current running command
|
2012-12-03 23:58:17 +00:00
|
|
|
type Command struct {
|
2014-03-15 16:06:11 +00:00
|
|
|
Name string
|
|
|
|
Help string
|
|
|
|
ArgsHelp string
|
2015-08-20 20:07:00 +00:00
|
|
|
Run func(fdst, fsrc fs.Fs) error
|
2014-03-15 16:06:11 +00:00
|
|
|
MinArgs int
|
|
|
|
MaxArgs int
|
|
|
|
NoStats bool
|
2015-08-20 20:07:00 +00:00
|
|
|
Retry bool
|
2012-12-03 23:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkArgs checks there are enough arguments and prints a message if not
|
|
|
|
func (cmd *Command) checkArgs(args []string) {
|
2014-03-15 16:06:11 +00:00
|
|
|
if len(args) < cmd.MinArgs {
|
2012-12-03 23:58:17 +00:00
|
|
|
syntaxError()
|
2014-03-15 16:06:11 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Command %s needs %d arguments mininum\n", cmd.Name, cmd.MinArgs)
|
2012-12-03 23:58:17 +00:00
|
|
|
os.Exit(1)
|
2014-03-15 16:06:11 +00:00
|
|
|
} else if len(args) > cmd.MaxArgs {
|
2012-12-03 23:58:17 +00:00
|
|
|
syntaxError()
|
2014-03-15 16:06:11 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Command %s needs %d arguments maximum\n", cmd.Name, cmd.MaxArgs)
|
2012-12-03 23:58:17 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Commands is a slice of possible Command~s
|
2012-12-03 23:58:17 +00:00
|
|
|
var Commands = []Command{
|
|
|
|
{
|
2014-03-15 16:06:11 +00:00
|
|
|
Name: "copy",
|
2015-02-28 13:08:18 +00:00
|
|
|
ArgsHelp: "source:path dest:path",
|
2014-03-15 16:06:11 +00:00
|
|
|
Help: `
|
2012-12-29 11:35:41 +00:00
|
|
|
Copy the source to the destination. Doesn't transfer
|
2015-05-10 11:17:24 +00:00
|
|
|
unchanged files, testing by size and modification time or
|
|
|
|
MD5SUM. Doesn't delete files from the destination.`,
|
2015-08-20 20:07:00 +00:00
|
|
|
Run: func(fdst, fsrc fs.Fs) error {
|
2015-08-24 20:42:23 +00:00
|
|
|
return fs.CopyDir(fdst, fsrc)
|
2014-03-28 17:56:04 +00:00
|
|
|
},
|
2014-03-15 16:06:11 +00:00
|
|
|
MinArgs: 2,
|
|
|
|
MaxArgs: 2,
|
2015-08-20 20:07:00 +00:00
|
|
|
Retry: true,
|
2012-12-31 16:40:34 +00:00
|
|
|
},
|
|
|
|
{
|
2014-03-15 16:06:11 +00:00
|
|
|
Name: "sync",
|
2015-02-28 13:08:18 +00:00
|
|
|
ArgsHelp: "source:path dest:path",
|
2014-03-15 16:06:11 +00:00
|
|
|
Help: `
|
2015-05-10 11:17:24 +00:00
|
|
|
Sync the source to the destination, changing the destination
|
|
|
|
only. Doesn't transfer unchanged files, testing by size and
|
|
|
|
modification time or MD5SUM. Destination is updated to match
|
|
|
|
source, including deleting files if necessary. Since this can
|
|
|
|
cause data loss, test first with the --dry-run flag.`,
|
2015-08-20 20:07:00 +00:00
|
|
|
Run: func(fdst, fsrc fs.Fs) error {
|
2015-08-24 20:42:23 +00:00
|
|
|
return fs.Sync(fdst, fsrc)
|
|
|
|
},
|
|
|
|
MinArgs: 2,
|
|
|
|
MaxArgs: 2,
|
|
|
|
Retry: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "move",
|
|
|
|
ArgsHelp: "source:path dest:path",
|
|
|
|
Help: `
|
|
|
|
Moves the source to the destination. This is equivalent to a
|
|
|
|
copy followed by a purge, but may use server side operations
|
|
|
|
to speed it up. Since this can cause data loss, test first
|
|
|
|
with the --dry-run flag.`,
|
|
|
|
Run: func(fdst, fsrc fs.Fs) error {
|
|
|
|
return fs.MoveDir(fdst, fsrc)
|
2014-03-28 17:56:04 +00:00
|
|
|
},
|
2014-03-15 16:06:11 +00:00
|
|
|
MinArgs: 2,
|
|
|
|
MaxArgs: 2,
|
2015-08-20 20:07:00 +00:00
|
|
|
Retry: true,
|
2012-12-03 23:58:17 +00:00
|
|
|
},
|
|
|
|
{
|
2014-03-15 16:06:11 +00:00
|
|
|
Name: "ls",
|
2015-10-02 18:48:48 +00:00
|
|
|
ArgsHelp: "remote:path",
|
2014-03-15 16:06:11 +00:00
|
|
|
Help: `
|
2014-07-12 11:09:20 +00:00
|
|
|
List all the objects in the the path with size and path.`,
|
2015-08-20 20:07:00 +00:00
|
|
|
Run: func(fdst, fsrc fs.Fs) error {
|
|
|
|
return fs.List(fdst, os.Stdout)
|
2014-03-28 17:56:04 +00:00
|
|
|
},
|
2014-03-15 16:06:11 +00:00
|
|
|
MinArgs: 1,
|
|
|
|
MaxArgs: 1,
|
2013-01-23 22:43:20 +00:00
|
|
|
},
|
|
|
|
{
|
2014-03-15 16:06:11 +00:00
|
|
|
Name: "lsd",
|
2015-10-02 18:48:48 +00:00
|
|
|
ArgsHelp: "remote:path",
|
2014-03-15 16:06:11 +00:00
|
|
|
Help: `
|
2014-05-05 18:52:52 +00:00
|
|
|
List all directories/containers/buckets in the the path.`,
|
2015-08-20 20:07:00 +00:00
|
|
|
Run: func(fdst, fsrc fs.Fs) error {
|
|
|
|
return fs.ListDir(fdst, os.Stdout)
|
2014-03-28 17:56:04 +00:00
|
|
|
},
|
2014-03-15 16:06:11 +00:00
|
|
|
MinArgs: 1,
|
|
|
|
MaxArgs: 1,
|
2012-12-03 23:58:17 +00:00
|
|
|
},
|
2014-07-12 11:09:20 +00:00
|
|
|
{
|
|
|
|
Name: "lsl",
|
2015-10-02 18:48:48 +00:00
|
|
|
ArgsHelp: "remote:path",
|
2014-07-12 11:09:20 +00:00
|
|
|
Help: `
|
2015-05-10 11:17:24 +00:00
|
|
|
List all the objects in the the path with modification time,
|
|
|
|
size and path.`,
|
2015-08-20 20:07:00 +00:00
|
|
|
Run: func(fdst, fsrc fs.Fs) error {
|
|
|
|
return fs.ListLong(fdst, os.Stdout)
|
2014-07-12 11:09:20 +00:00
|
|
|
},
|
|
|
|
MinArgs: 1,
|
|
|
|
MaxArgs: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "md5sum",
|
2015-10-02 18:48:48 +00:00
|
|
|
ArgsHelp: "remote:path",
|
2014-07-12 11:09:20 +00:00
|
|
|
Help: `
|
2015-02-28 13:08:18 +00:00
|
|
|
Produces an md5sum file for all the objects in the path. This
|
|
|
|
is in the same format as the standard md5sum tool produces.`,
|
2015-08-20 20:07:00 +00:00
|
|
|
Run: func(fdst, fsrc fs.Fs) error {
|
|
|
|
return fs.Md5sum(fdst, os.Stdout)
|
2014-07-12 11:09:20 +00:00
|
|
|
},
|
|
|
|
MinArgs: 1,
|
|
|
|
MaxArgs: 1,
|
|
|
|
},
|
2015-10-02 18:48:48 +00:00
|
|
|
{
|
|
|
|
Name: "size",
|
|
|
|
ArgsHelp: "remote:path",
|
|
|
|
Help: `
|
|
|
|
Returns the total size of objects in remote:path and the number
|
|
|
|
of objects.`,
|
|
|
|
Run: func(fdst, fsrc fs.Fs) error {
|
|
|
|
objects, size, err := fs.Count(fdst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("Total objects: %d\n", objects)
|
|
|
|
fmt.Printf("Total size: %v (%d bytes)\n", fs.SizeSuffix(size), size)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
MinArgs: 1,
|
|
|
|
MaxArgs: 1,
|
|
|
|
},
|
2012-12-03 23:58:17 +00:00
|
|
|
{
|
2014-03-15 16:06:11 +00:00
|
|
|
Name: "mkdir",
|
2015-02-28 13:08:18 +00:00
|
|
|
ArgsHelp: "remote:path",
|
2014-03-15 16:06:11 +00:00
|
|
|
Help: `
|
2012-12-31 17:31:19 +00:00
|
|
|
Make the path if it doesn't already exist`,
|
2015-08-20 20:07:00 +00:00
|
|
|
Run: func(fdst, fsrc fs.Fs) error {
|
|
|
|
return fs.Mkdir(fdst)
|
2014-03-28 17:56:04 +00:00
|
|
|
},
|
2014-03-15 16:06:11 +00:00
|
|
|
MinArgs: 1,
|
|
|
|
MaxArgs: 1,
|
2015-08-20 20:07:00 +00:00
|
|
|
Retry: true,
|
2012-12-03 23:58:17 +00:00
|
|
|
},
|
|
|
|
{
|
2014-03-15 16:06:11 +00:00
|
|
|
Name: "rmdir",
|
2015-02-28 13:08:18 +00:00
|
|
|
ArgsHelp: "remote:path",
|
2014-03-15 16:06:11 +00:00
|
|
|
Help: `
|
2012-12-29 11:35:41 +00:00
|
|
|
Remove the path. Note that you can't remove a path with
|
2012-12-31 17:31:19 +00:00
|
|
|
objects in it, use purge for that.`,
|
2015-08-20 20:07:00 +00:00
|
|
|
Run: func(fdst, fsrc fs.Fs) error {
|
|
|
|
return fs.Rmdir(fdst)
|
2014-03-28 17:56:04 +00:00
|
|
|
},
|
2014-03-15 16:06:11 +00:00
|
|
|
MinArgs: 1,
|
|
|
|
MaxArgs: 1,
|
2015-08-20 20:07:00 +00:00
|
|
|
Retry: true,
|
2012-12-03 23:58:17 +00:00
|
|
|
},
|
2012-12-04 00:09:22 +00:00
|
|
|
{
|
2014-03-15 16:06:11 +00:00
|
|
|
Name: "purge",
|
2015-02-28 13:08:18 +00:00
|
|
|
ArgsHelp: "remote:path",
|
2014-03-15 16:06:11 +00:00
|
|
|
Help: `
|
2012-12-31 17:31:19 +00:00
|
|
|
Remove the path and all of its contents.`,
|
2015-08-20 20:07:00 +00:00
|
|
|
Run: func(fdst, fsrc fs.Fs) error {
|
|
|
|
return fs.Purge(fdst)
|
2014-03-28 17:56:04 +00:00
|
|
|
},
|
2014-03-15 16:06:11 +00:00
|
|
|
MinArgs: 1,
|
|
|
|
MaxArgs: 1,
|
2015-08-20 20:07:00 +00:00
|
|
|
Retry: true,
|
2012-12-04 00:09:22 +00:00
|
|
|
},
|
2012-12-31 17:31:19 +00:00
|
|
|
{
|
2014-03-15 16:06:11 +00:00
|
|
|
Name: "check",
|
2015-02-28 13:08:18 +00:00
|
|
|
ArgsHelp: "source:path dest:path",
|
2014-03-15 16:06:11 +00:00
|
|
|
Help: `
|
2012-12-31 17:31:19 +00:00
|
|
|
Checks the files in the source and destination match. It
|
|
|
|
compares sizes and MD5SUMs and prints a report of files which
|
|
|
|
don't match. It doesn't alter the source or destination.`,
|
2015-08-20 20:07:00 +00:00
|
|
|
Run: func(fdst, fsrc fs.Fs) error {
|
|
|
|
return fs.Check(fdst, fsrc)
|
2014-03-28 17:56:04 +00:00
|
|
|
},
|
2014-03-15 16:06:11 +00:00
|
|
|
MinArgs: 2,
|
|
|
|
MaxArgs: 2,
|
2012-12-31 17:31:19 +00:00
|
|
|
},
|
|
|
|
{
|
2014-03-15 16:06:11 +00:00
|
|
|
Name: "config",
|
|
|
|
Help: `
|
|
|
|
Enter an interactive configuration session.`,
|
2015-08-20 20:07:00 +00:00
|
|
|
Run: func(fdst, fsrc fs.Fs) error {
|
2014-03-28 17:56:04 +00:00
|
|
|
fs.EditConfig()
|
2015-08-20 20:07:00 +00:00
|
|
|
return nil
|
2014-03-28 17:56:04 +00:00
|
|
|
},
|
2014-03-15 16:06:11 +00:00
|
|
|
NoStats: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "help",
|
|
|
|
Help: `
|
2012-12-31 17:31:19 +00:00
|
|
|
This help.`,
|
2014-03-15 16:06:11 +00:00
|
|
|
NoStats: true,
|
2012-12-31 17:31:19 +00:00
|
|
|
},
|
2012-12-03 23:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// syntaxError prints the syntax
|
|
|
|
func syntaxError() {
|
2014-04-24 16:59:05 +00:00
|
|
|
fmt.Fprintf(os.Stderr, `Sync files and directories to and from local and remote object stores - %s.
|
2012-12-03 23:58:17 +00:00
|
|
|
|
|
|
|
Syntax: [options] subcommand <parameters> <parameters...>
|
|
|
|
|
|
|
|
Subcommands:
|
2012-12-29 11:35:41 +00:00
|
|
|
|
2014-07-13 17:58:06 +00:00
|
|
|
`, fs.Version)
|
2012-12-03 23:58:17 +00:00
|
|
|
for i := range Commands {
|
|
|
|
cmd := &Commands[i]
|
2014-03-15 16:06:11 +00:00
|
|
|
fmt.Fprintf(os.Stderr, " %s %s\n", cmd.Name, cmd.ArgsHelp)
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n\n", cmd.Help)
|
2012-12-03 23:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Options:\n")
|
2014-03-27 16:55:29 +00:00
|
|
|
pflag.PrintDefaults()
|
2012-12-03 23:58:17 +00:00
|
|
|
fmt.Fprintf(os.Stderr, `
|
2015-09-11 13:01:35 +00:00
|
|
|
It is only necessary to use a unique prefix of the subcommand, eg 'mo'
|
|
|
|
for 'move'.
|
2012-12-03 23:58:17 +00:00
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit with the message
|
|
|
|
func fatal(message string, args ...interface{}) {
|
|
|
|
syntaxError()
|
|
|
|
fmt.Fprintf(os.Stderr, message, args...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// ParseFlags parses the command line flags
|
2014-03-27 17:26:24 +00:00
|
|
|
func ParseFlags() {
|
2014-03-27 16:55:29 +00:00
|
|
|
pflag.Usage = syntaxError
|
|
|
|
pflag.Parse()
|
2012-11-29 22:13:58 +00:00
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
2014-03-15 16:06:11 +00:00
|
|
|
fs.LoadConfig()
|
2013-06-29 09:43:52 +00:00
|
|
|
|
2012-11-18 17:32:31 +00:00
|
|
|
// Setup profiling if desired
|
|
|
|
if *cpuprofile != "" {
|
|
|
|
f, err := os.Create(*cpuprofile)
|
|
|
|
if err != nil {
|
2013-06-27 19:13:07 +00:00
|
|
|
fs.Stats.Error()
|
2012-11-18 17:32:31 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-07-25 17:19:49 +00:00
|
|
|
err = pprof.StartCPUProfile(f)
|
|
|
|
if err != nil {
|
|
|
|
fs.Stats.Error()
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2012-11-18 17:32:31 +00:00
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
2014-03-27 17:26:24 +00:00
|
|
|
}
|
2012-11-18 17:32:31 +00:00
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// ParseCommand parses the command from the command line
|
2014-03-27 17:26:24 +00:00
|
|
|
func ParseCommand() (*Command, []string) {
|
|
|
|
args := pflag.Args()
|
2012-11-20 22:40:48 +00:00
|
|
|
if len(args) < 1 {
|
|
|
|
fatal("No command supplied\n")
|
|
|
|
}
|
2012-11-18 17:32:31 +00:00
|
|
|
|
2012-12-03 23:58:17 +00:00
|
|
|
cmd := strings.ToLower(args[0])
|
2012-11-20 22:40:48 +00:00
|
|
|
args = args[1:]
|
|
|
|
|
2012-12-03 23:58:17 +00:00
|
|
|
// Find the command doing a prefix match
|
2015-09-11 13:01:35 +00:00
|
|
|
var found = make([]*Command, 0, 1)
|
2014-03-27 17:26:24 +00:00
|
|
|
var command *Command
|
2012-12-03 23:58:17 +00:00
|
|
|
for i := range Commands {
|
2014-03-27 17:26:24 +00:00
|
|
|
trialCommand := &Commands[i]
|
2012-12-03 23:58:17 +00:00
|
|
|
// exact command name found - use that
|
2014-03-27 17:26:24 +00:00
|
|
|
if trialCommand.Name == cmd {
|
|
|
|
command = trialCommand
|
2012-12-03 23:58:17 +00:00
|
|
|
break
|
2014-03-27 17:26:24 +00:00
|
|
|
} else if strings.HasPrefix(trialCommand.Name, cmd) {
|
2015-09-11 13:01:35 +00:00
|
|
|
found = append(found, trialCommand)
|
2012-11-20 22:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-27 17:26:24 +00:00
|
|
|
if command == nil {
|
2015-09-11 13:01:35 +00:00
|
|
|
switch len(found) {
|
|
|
|
case 0:
|
|
|
|
fs.Stats.Error()
|
|
|
|
log.Fatalf("Unknown command %q", cmd)
|
|
|
|
case 1:
|
|
|
|
command = found[0]
|
|
|
|
default:
|
|
|
|
fs.Stats.Error()
|
|
|
|
var names []string
|
|
|
|
for _, cmd := range found {
|
|
|
|
names = append(names, `"`+cmd.Name+`"`)
|
|
|
|
}
|
|
|
|
log.Fatalf("Not unique - matches multiple commands: %s", strings.Join(names, ", "))
|
|
|
|
}
|
2012-12-03 23:58:17 +00:00
|
|
|
}
|
2014-03-27 17:26:24 +00:00
|
|
|
if command.Run == nil {
|
|
|
|
syntaxError()
|
|
|
|
}
|
|
|
|
command.checkArgs(args)
|
|
|
|
return command, args
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// NewFs creates a Fs from a name
|
2014-03-27 17:26:24 +00:00
|
|
|
func NewFs(remote string) fs.Fs {
|
|
|
|
f, err := fs.NewFs(remote)
|
|
|
|
if err != nil {
|
|
|
|
fs.Stats.Error()
|
|
|
|
log.Fatalf("Failed to create file system for %q: %v", remote, err)
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// StartStats prints the stats every statsInterval
|
2014-03-27 17:26:24 +00:00
|
|
|
func StartStats() {
|
2015-02-27 15:22:26 +00:00
|
|
|
if *statsInterval <= 0 {
|
|
|
|
return
|
|
|
|
}
|
2014-03-27 17:26:24 +00:00
|
|
|
go func() {
|
|
|
|
ch := time.Tick(*statsInterval)
|
|
|
|
for {
|
|
|
|
<-ch
|
|
|
|
fs.Stats.Log()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ParseFlags()
|
2014-04-24 16:59:05 +00:00
|
|
|
if *version {
|
2014-07-13 17:58:06 +00:00
|
|
|
fmt.Printf("rclone %s\n", fs.Version)
|
2014-04-24 16:59:05 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2014-03-27 17:26:24 +00:00
|
|
|
command, args := ParseCommand()
|
2012-12-29 11:35:41 +00:00
|
|
|
|
2015-02-27 16:51:17 +00:00
|
|
|
// Log file output
|
|
|
|
if *logFile != "" {
|
|
|
|
f, err := os.OpenFile(*logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to open log file: %v", err)
|
|
|
|
}
|
2015-09-22 06:31:12 +00:00
|
|
|
_, err = f.Seek(0, os.SEEK_END)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to seek log file to end: %v", err)
|
|
|
|
}
|
2015-02-27 16:51:17 +00:00
|
|
|
log.SetOutput(f)
|
|
|
|
redirectStderr(f)
|
|
|
|
}
|
|
|
|
|
2012-12-29 11:35:41 +00:00
|
|
|
// Make source and destination fs
|
2013-06-27 19:13:07 +00:00
|
|
|
var fdst, fsrc fs.Fs
|
2012-12-29 11:35:41 +00:00
|
|
|
if len(args) >= 1 {
|
2014-03-27 17:26:24 +00:00
|
|
|
fdst = NewFs(args[0])
|
2012-12-29 11:35:41 +00:00
|
|
|
}
|
|
|
|
if len(args) >= 2 {
|
2014-03-27 17:26:24 +00:00
|
|
|
fsrc = fdst
|
|
|
|
fdst = NewFs(args[1])
|
2012-12-29 11:35:41 +00:00
|
|
|
}
|
|
|
|
|
2014-03-27 17:26:24 +00:00
|
|
|
fs.CalculateModifyWindow(fdst, fsrc)
|
2013-01-18 23:21:02 +00:00
|
|
|
|
2014-03-27 17:26:24 +00:00
|
|
|
if !command.NoStats {
|
|
|
|
StartStats()
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
2013-01-03 22:50:00 +00:00
|
|
|
|
2012-12-29 11:35:41 +00:00
|
|
|
// Run the actual command
|
2014-03-27 17:26:24 +00:00
|
|
|
if command.Run != nil {
|
2015-08-20 20:07:00 +00:00
|
|
|
var err error
|
|
|
|
for try := 1; try <= *retries; try++ {
|
|
|
|
err = command.Run(fdst, fsrc)
|
|
|
|
if !command.Retry || (err == nil && !fs.Stats.Errored()) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fs.Log(nil, "Attempt %d/%d failed with %d errors and: %v", try, *retries, fs.Stats.GetErrors(), err)
|
|
|
|
} else {
|
|
|
|
fs.Log(nil, "Attempt %d/%d failed with %d errors", try, *retries, fs.Stats.GetErrors())
|
|
|
|
}
|
|
|
|
if try < *retries {
|
|
|
|
fs.Stats.ResetErrors()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to %s: %v", command.Name, err)
|
|
|
|
}
|
2015-08-08 19:10:31 +00:00
|
|
|
if !command.NoStats && (!fs.Config.Quiet || fs.Stats.Errored() || *statsInterval > 0) {
|
2015-02-28 14:39:00 +00:00
|
|
|
fmt.Fprintln(os.Stderr, fs.Stats)
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
|
|
|
if fs.Config.Verbose {
|
2015-02-27 15:04:18 +00:00
|
|
|
fs.Debug(nil, "Go routines at exit %d\n", runtime.NumGoroutine())
|
2014-03-15 16:06:11 +00:00
|
|
|
}
|
2013-06-27 19:13:07 +00:00
|
|
|
if fs.Stats.Errored() {
|
2013-01-03 22:50:00 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
2012-12-31 17:31:19 +00:00
|
|
|
} else {
|
|
|
|
}
|
|
|
|
|
2012-11-18 17:32:31 +00:00
|
|
|
}
|