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