rclone/rclone.go

404 lines
9.1 KiB
Go
Raw Normal View History

2013-06-28 05:00:01 +10:00
// Sync files and directories to and from local and remote object stores
2013-06-28 04:51:03 +10:00
//
// Nick Craig-Wood <nick@craig-wood.com>
package main
import (
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
"strings"
"time"
2014-03-28 03:55:29 +11:00
"github.com/ogier/pflag"
"github.com/ncw/rclone/fs"
2013-06-28 05:13:07 +10:00
// Active file systems
_ "github.com/ncw/rclone/drive"
_ "github.com/ncw/rclone/dropbox"
_ "github.com/ncw/rclone/googlecloudstorage"
_ "github.com/ncw/rclone/local"
_ "github.com/ncw/rclone/s3"
_ "github.com/ncw/rclone/swift"
)
// Globals
var (
// Flags
2014-03-28 03:55:29 +11:00
cpuprofile = pflag.StringP("cpuprofile", "", "", "Write cpu profile to file")
statsInterval = pflag.DurationP("stats", "", time.Minute*1, "Interval to print stats (0 to disable)")
2014-04-25 02:59:05 +10:00
version = pflag.BoolP("version", "V", false, "Print the version number")
logFile = pflag.StringP("log-file", "", "", "Log everything to this file")
2015-08-21 06:07:00 +10:00
retries = pflag.IntP("retries", "", 3, "Retry operations this many times if they fail")
)
2012-12-04 10:58:17 +11:00
type Command struct {
Name string
Help string
ArgsHelp string
2015-08-21 06:07:00 +10:00
Run func(fdst, fsrc fs.Fs) error
MinArgs int
MaxArgs int
NoStats bool
2015-08-21 06:07:00 +10:00
Retry bool
2012-12-04 10:58:17 +11:00
}
// checkArgs checks there are enough arguments and prints a message if not
func (cmd *Command) checkArgs(args []string) {
if len(args) < cmd.MinArgs {
2012-12-04 10:58:17 +11:00
syntaxError()
fmt.Fprintf(os.Stderr, "Command %s needs %d arguments mininum\n", cmd.Name, cmd.MinArgs)
2012-12-04 10:58:17 +11:00
os.Exit(1)
} else if len(args) > cmd.MaxArgs {
2012-12-04 10:58:17 +11:00
syntaxError()
fmt.Fprintf(os.Stderr, "Command %s needs %d arguments maximum\n", cmd.Name, cmd.MaxArgs)
2012-12-04 10:58:17 +11:00
os.Exit(1)
}
}
var Commands = []Command{
{
Name: "copy",
2015-03-01 00:08:18 +11:00
ArgsHelp: "source:path dest:path",
Help: `
Copy the source to the destination. Doesn't transfer
unchanged files, testing by size and modification time or
MD5SUM. Doesn't delete files from the destination.`,
2015-08-21 06:07:00 +10:00
Run: func(fdst, fsrc fs.Fs) error {
return fs.CopyDir(fdst, fsrc)
},
MinArgs: 2,
MaxArgs: 2,
2015-08-21 06:07:00 +10:00
Retry: true,
},
{
Name: "sync",
2015-03-01 00:08:18 +11:00
ArgsHelp: "source:path dest:path",
Help: `
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-21 06:07:00 +10:00
Run: func(fdst, fsrc fs.Fs) error {
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)
},
MinArgs: 2,
MaxArgs: 2,
2015-08-21 06:07:00 +10:00
Retry: true,
2012-12-04 10:58:17 +11:00
},
{
Name: "ls",
2015-03-01 00:08:18 +11:00
ArgsHelp: "[remote:path]",
Help: `
List all the objects in the the path with size and path.`,
2015-08-21 06:07:00 +10:00
Run: func(fdst, fsrc fs.Fs) error {
return fs.List(fdst, os.Stdout)
},
MinArgs: 1,
MaxArgs: 1,
2013-01-24 09:43:20 +11:00
},
{
Name: "lsd",
2015-03-01 00:08:18 +11:00
ArgsHelp: "[remote:path]",
Help: `
List all directories/containers/buckets in the the path.`,
2015-08-21 06:07:00 +10:00
Run: func(fdst, fsrc fs.Fs) error {
return fs.ListDir(fdst, os.Stdout)
},
MinArgs: 1,
MaxArgs: 1,
2012-12-04 10:58:17 +11:00
},
{
Name: "lsl",
2015-03-01 00:08:18 +11:00
ArgsHelp: "[remote:path]",
Help: `
List all the objects in the the path with modification time,
size and path.`,
2015-08-21 06:07:00 +10:00
Run: func(fdst, fsrc fs.Fs) error {
return fs.ListLong(fdst, os.Stdout)
},
MinArgs: 1,
MaxArgs: 1,
},
{
Name: "md5sum",
2015-03-01 00:08:18 +11:00
ArgsHelp: "[remote:path]",
Help: `
2015-03-01 00:08:18 +11: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-21 06:07:00 +10:00
Run: func(fdst, fsrc fs.Fs) error {
return fs.Md5sum(fdst, os.Stdout)
},
MinArgs: 1,
MaxArgs: 1,
},
2012-12-04 10:58:17 +11:00
{
Name: "mkdir",
2015-03-01 00:08:18 +11:00
ArgsHelp: "remote:path",
Help: `
2013-01-01 04:31:19 +11:00
Make the path if it doesn't already exist`,
2015-08-21 06:07:00 +10:00
Run: func(fdst, fsrc fs.Fs) error {
return fs.Mkdir(fdst)
},
MinArgs: 1,
MaxArgs: 1,
2015-08-21 06:07:00 +10:00
Retry: true,
2012-12-04 10:58:17 +11:00
},
{
Name: "rmdir",
2015-03-01 00:08:18 +11:00
ArgsHelp: "remote:path",
Help: `
Remove the path. Note that you can't remove a path with
2013-01-01 04:31:19 +11:00
objects in it, use purge for that.`,
2015-08-21 06:07:00 +10:00
Run: func(fdst, fsrc fs.Fs) error {
return fs.Rmdir(fdst)
},
MinArgs: 1,
MaxArgs: 1,
2015-08-21 06:07:00 +10:00
Retry: true,
2012-12-04 10:58:17 +11:00
},
{
Name: "purge",
2015-03-01 00:08:18 +11:00
ArgsHelp: "remote:path",
Help: `
2013-01-01 04:31:19 +11:00
Remove the path and all of its contents.`,
2015-08-21 06:07:00 +10:00
Run: func(fdst, fsrc fs.Fs) error {
return fs.Purge(fdst)
},
MinArgs: 1,
MaxArgs: 1,
2015-08-21 06:07:00 +10:00
Retry: true,
},
2013-01-01 04:31:19 +11:00
{
Name: "check",
2015-03-01 00:08:18 +11:00
ArgsHelp: "source:path dest:path",
Help: `
2013-01-01 04:31:19 +11: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-21 06:07:00 +10:00
Run: func(fdst, fsrc fs.Fs) error {
return fs.Check(fdst, fsrc)
},
MinArgs: 2,
MaxArgs: 2,
2013-01-01 04:31:19 +11:00
},
{
Name: "config",
Help: `
Enter an interactive configuration session.`,
2015-08-21 06:07:00 +10:00
Run: func(fdst, fsrc fs.Fs) error {
fs.EditConfig()
2015-08-21 06:07:00 +10:00
return nil
},
NoStats: true,
},
{
Name: "help",
Help: `
2013-01-01 04:31:19 +11:00
This help.`,
NoStats: true,
2013-01-01 04:31:19 +11:00
},
2012-12-04 10:58:17 +11:00
}
// syntaxError prints the syntax
func syntaxError() {
2014-04-25 02:59:05 +10:00
fmt.Fprintf(os.Stderr, `Sync files and directories to and from local and remote object stores - %s.
2012-12-04 10:58:17 +11:00
Syntax: [options] subcommand <parameters> <parameters...>
Subcommands:
`, fs.Version)
2012-12-04 10:58:17 +11:00
for i := range Commands {
cmd := &Commands[i]
fmt.Fprintf(os.Stderr, " %s %s\n", cmd.Name, cmd.ArgsHelp)
fmt.Fprintf(os.Stderr, "%s\n\n", cmd.Help)
2012-12-04 10:58:17 +11:00
}
fmt.Fprintf(os.Stderr, "Options:\n")
2014-03-28 03:55:29 +11:00
pflag.PrintDefaults()
2012-12-04 10:58:17 +11:00
fmt.Fprintf(os.Stderr, `
It is only necessary to use a unique prefix of the subcommand, eg 'up'
for 'upload'.
2012-12-04 10:58:17 +11:00
`)
}
// Exit with the message
func fatal(message string, args ...interface{}) {
syntaxError()
fmt.Fprintf(os.Stderr, message, args...)
os.Exit(1)
}
// Parse the command line flags
func ParseFlags() {
2014-03-28 03:55:29 +11:00
pflag.Usage = syntaxError
pflag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU())
fs.LoadConfig()
2013-06-29 19:43:52 +10:00
// Setup profiling if desired
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
2013-06-28 05:13:07 +10:00
fs.Stats.Error()
log.Fatal(err)
}
2014-07-26 03:19:49 +10:00
err = pprof.StartCPUProfile(f)
if err != nil {
fs.Stats.Error()
log.Fatal(err)
}
defer pprof.StopCPUProfile()
}
}
// Parse the command from the command line
func ParseCommand() (*Command, []string) {
args := pflag.Args()
if len(args) < 1 {
fatal("No command supplied\n")
}
2012-12-04 10:58:17 +11:00
cmd := strings.ToLower(args[0])
args = args[1:]
2012-12-04 10:58:17 +11:00
// Find the command doing a prefix match
var command *Command
2012-12-04 10:58:17 +11:00
for i := range Commands {
trialCommand := &Commands[i]
2012-12-04 10:58:17 +11:00
// exact command name found - use that
if trialCommand.Name == cmd {
command = trialCommand
2012-12-04 10:58:17 +11:00
break
} else if strings.HasPrefix(trialCommand.Name, cmd) {
if command != nil {
2013-06-28 05:13:07 +10:00
fs.Stats.Error()
2012-12-04 10:58:17 +11:00
log.Fatalf("Not unique - matches multiple commands %q", cmd)
}
command = trialCommand
}
}
if command == nil {
2013-06-28 05:13:07 +10:00
fs.Stats.Error()
2012-12-04 10:58:17 +11:00
log.Fatalf("Unknown command %q", cmd)
}
if command.Run == nil {
syntaxError()
}
command.checkArgs(args)
return command, args
}
// Create a Fs from a name
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
}
// Print the stats every statsInterval
func StartStats() {
if *statsInterval <= 0 {
return
}
go func() {
ch := time.Tick(*statsInterval)
for {
<-ch
fs.Stats.Log()
}
}()
}
func main() {
ParseFlags()
2014-04-25 02:59:05 +10:00
if *version {
fmt.Printf("rclone %s\n", fs.Version)
2014-04-25 02:59:05 +10:00
os.Exit(0)
}
command, args := ParseCommand()
// 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)
}
f.Seek(0, os.SEEK_END)
log.SetOutput(f)
redirectStderr(f)
}
// Make source and destination fs
2013-06-28 05:13:07 +10:00
var fdst, fsrc fs.Fs
if len(args) >= 1 {
fdst = NewFs(args[0])
}
if len(args) >= 2 {
fsrc = fdst
fdst = NewFs(args[1])
}
fs.CalculateModifyWindow(fdst, fsrc)
if !command.NoStats {
StartStats()
}
// Run the actual command
if command.Run != nil {
2015-08-21 06:07:00 +10: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)
}
if !command.NoStats && (!fs.Config.Quiet || fs.Stats.Errored() || *statsInterval > 0) {
fmt.Fprintln(os.Stderr, fs.Stats)
}
if fs.Config.Verbose {
2015-02-28 02:04:18 +11:00
fs.Debug(nil, "Go routines at exit %d\n", runtime.NumGoroutine())
}
2013-06-28 05:13:07 +10:00
if fs.Stats.Errored() {
os.Exit(1)
}
os.Exit(0)
2013-01-01 04:31:19 +11:00
} else {
}
}