@@ -9,31 +9,48 @@ import (
99
1010func main () {
1111
12- usage := fmt .Sprintf (`Consul KV and ACL Backup with KV Restore tool.
12+ usage := fmt .Sprintf (`Consul KV and ACL Backup and Sync tool.
1313
1414Version: %s (Commit: %s)
1515
1616Usage:
17- consul-bak [-i IP] [--http-port HTTPPORT] [--rpc-port RPCPORT]
18- [-l] [-t TOKEN] [-a] [-b ACLBACKUPFILE] [-n INPREFIX]...
19- [-x EXPREFIX]... [--restore] [--no-prompt] <filename>
17+ consul-bak (backup|restore|aclbackup)
18+ [--leader-only]
19+ [--rpc-port RPCPORT]
20+ [--http-port HTTPPORT]
21+ [--address IP]
22+ [--include-prefix INPREFIX]...
23+ [--exclude-prefix EXPREFIX]...
24+ [--token TOKEN]
25+ [--no-prompt]
26+ <filename>
27+ consul-bak dumptree
28+ [--leader-only]
29+ [--rpc-port RPCPORT]
30+ [--http-port HTTPPORT]
31+ [--address IP]
32+ <pathname>
33+ consul-bak syncgit
34+ [--leader-only]
35+ [--rpc-port RPCPORT]
36+ [--http-port HTTPPORT]
37+ [--address IP]
38+ <git-url>
2039 consul-bak -h | --help
21- consul-bak --version
40+ consul-bak -v | - -version
2241
2342Options:
24- -h --help Show this screen.
25- --version Show version.
26- -l, --leader-only Create backup only on consul leader.
43+ -h, --help Show this screen.
44+ -v, --version Show version.
45+ --mode=MODE Set mode, can be one of backup,restore,syncgit,dumptree,aclbackup [default: backup]
46+ --leader-only Only run on consul leader.
2747 --rpc-port=RPCPORT RPC port [default: 8400].
2848 --http-port=HTTPPORT HTTP endpoint port [default: 8500].
29- -i, --address=IP The HTTP endpoint of Consul [default: 127.0.0.1].
30- -t, --token=TOKEN An ACL Token with proper permissions in Consul [default: ].
31- -a, --aclbackup Backup ACLs, does nothing in restore mode. ACL restore not available at this time.
32- -b, --aclbackupfile=ACLBACKUPFILE ACL Backup Filename [default: acl.bkp].
33- -x, --exclude-prefix=[EXPREFIX] Repeatable option for keys starting with prefix to exclude from the backup.
34- -n, --include-prefix=[INPREFIX] Repeatable option for keys starting with prefix to include in the backup.
35- -r, --restore Activate restore mode.
36- --no-prompt Don't prompt, force overwrite in restore mode.` , Version , GitCommit )
49+ --address=IP The HTTP endpoint of Consul [default: 127.0.0.1].
50+ --include-prefix=[INPREFIX] Repeatable option for keys starting with prefix to include in the backup.
51+ --exclude-prefix=[EXPREFIX] Repeatable option for keys starting with prefix to exclude from the backup.
52+ --token=TOKEN An ACL Token with proper permissions in Consul [default: ].
53+ --force Don't prompt, force overwrite.` , Version , GitCommit )
3754
3855 arguments , _ := docopt .Parse (usage , nil , true , fmt .Sprintf ("consul-bak %s (%s)" , Version , GitCommit ), false )
3956 SetupLogging ()
@@ -50,7 +67,7 @@ Options:
5067 CheckSocket (rpcEndpoint )
5168
5269 if arguments ["--leader-only" ] == true {
53- logger .Info ("Running in leader only mode, only running backup/restore on Consul leader." )
70+ logger .Info ("Running in leader only mode, only running on Consul leader." )
5471 // if consul client is not available we keep running
5572 if Which ("consul" ) {
5673 var out = ConsulBinaryCall ("info" , rpcOptString )
@@ -66,19 +83,19 @@ Options:
6683 }
6784 }
6885
69- if arguments ["-- restore" ] == true {
86+ if arguments ["restore" ].( bool ) {
7087 logger .Info ("Running in restore mode." )
7188 if (len (arguments ["--exclude-prefix" ].([]string )) > 0 ) || (len (arguments ["--include-prefix" ].([]string )) > 0 ) {
7289 logger .Error ("--exclude-prefix, -x and --include-prefix, -n can be used only for backups" )
7390 os .Exit (1 )
7491 }
75- if arguments ["--no-prompt" ] == false {
92+ if ! arguments ["--force" ].( bool ) {
7693 fmt .Printf ("\n Warning! This will overwrite existing kv. Press [enter] to continue; CTL-C to exit" )
7794 fmt .Scanln ()
7895 }
7996 logger .Infof ("Restoring KV from file: %s" , arguments ["<filename>" ].(string ))
8097 Restore (httpEndpoint , arguments ["--token" ].(string ), arguments ["<filename>" ].(string ))
81- } else {
98+ } else if arguments [ "backup" ].( bool ) {
8299 logger .Info ("Running in backup mode." )
83100 if (len (arguments ["--exclude-prefix" ].([]string )) > 0 ) && (len (arguments ["--include-prefix" ].([]string )) > 0 ) {
84101 logger .Error ("--exclude-prefix and --include-prefix cannot be used together" )
@@ -92,10 +109,15 @@ Options:
92109 }
93110 logger .Infof ("KV store will be backed up to file: %s" , arguments ["<filename>" ].(string ))
94111 Backup (httpEndpoint , arguments ["--token" ].(string ), arguments ["<filename>" ].(string ), arguments ["--exclude-prefix" ].([]string ), arguments ["--include-prefix" ].([]string ))
95- if arguments ["--aclbackup" ] == true {
96-
97- logger .Infof ("ACL Tokens will be backed up to file: %s" , arguments ["--aclbackupfile" ].(string ))
98- BackupACLs (httpEndpoint , arguments ["--token" ].(string ), arguments ["--aclbackupfile" ].(string ))
99- }
112+ } else if arguments ["aclbackup" ].(bool ) {
113+ logger .Infof ("ACL Tokens will be backed up to file: %s" , arguments ["<filename>" ].(string ))
114+ BackupACLs (httpEndpoint , arguments ["--token" ].(string ), arguments ["<filename>" ].(string ))
115+ } else if arguments ["syncgit" ].(bool ) {
116+ logger .Info ("Running in sync mode." )
117+ Sync (httpEndpoint , arguments ["--token" ].(string ), arguments ["<git-url>" ].(string ))
118+ } else if arguments ["dumptree" ].(bool ) {
119+ var path string = arguments ["<pathname>" ].(string )
120+ logger .Infof ("Running in dump mode. Dumping to %s" , path )
121+ Dump (httpEndpoint , arguments ["--token" ].(string ), path )
100122 }
101123}
0 commit comments