/usr/share/gocode/src/github.com/influxdata/influxdb/cmd/influx_inspect/export/export.go is in golang-github-influxdb-influxdb-dev 1.1.1+dfsg1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | package export
import (
"compress/gzip"
"flag"
"fmt"
"io"
"log"
"math"
"os"
"path"
"path/filepath"
"sort"
"strings"
"sync"
"time"
"github.com/influxdata/influxdb/influxql"
"github.com/influxdata/influxdb/models"
"github.com/influxdata/influxdb/pkg/escape"
"github.com/influxdata/influxdb/tsdb/engine/tsm1"
)
// Command represents the program execution for "influx_inspect export".
type Command struct {
// Standard input/output, overridden for testing.
Stderr io.Writer
Stdout io.Writer
dataDir string
walDir string
out string
database string
retentionPolicy string
startTime int64
endTime int64
compress bool
manifest map[string]struct{}
tsmFiles map[string][]string
walFiles map[string][]string
}
// NewCommand returns a new instance of Command.
func NewCommand() *Command {
return &Command{
Stderr: os.Stderr,
Stdout: os.Stdout,
manifest: make(map[string]struct{}),
tsmFiles: make(map[string][]string),
walFiles: make(map[string][]string),
}
}
// Run executes the command.
func (cmd *Command) Run(args ...string) error {
var start, end string
fs := flag.NewFlagSet("export", flag.ExitOnError)
fs.StringVar(&cmd.dataDir, "datadir", os.Getenv("HOME")+"/.influxdb/data", "Data storage path")
fs.StringVar(&cmd.walDir, "waldir", os.Getenv("HOME")+"/.influxdb/wal", "WAL storage path")
fs.StringVar(&cmd.out, "out", os.Getenv("HOME")+"/.influxdb/export", "Destination file to export to")
fs.StringVar(&cmd.database, "database", "", "Optional: the database to export")
fs.StringVar(&cmd.retentionPolicy, "retention", "", "Optional: the retention policy to export (requires -database)")
fs.StringVar(&start, "start", "", "Optional: the start time to export")
fs.StringVar(&end, "end", "", "Optional: the end time to export")
fs.BoolVar(&cmd.compress, "compress", false, "Compress the output")
fs.SetOutput(cmd.Stdout)
fs.Usage = func() {
fmt.Fprintf(cmd.Stdout, "Exports TSM files into InfluxDB line protocol format.\n\n")
fmt.Fprintf(cmd.Stdout, "Usage: %s export [flags]\n\n", filepath.Base(os.Args[0]))
fs.PrintDefaults()
}
if err := fs.Parse(args); err != nil {
return err
}
// set defaults
if start != "" {
s, err := time.Parse(time.RFC3339, start)
if err != nil {
return err
}
cmd.startTime = s.UnixNano()
} else {
cmd.startTime = math.MinInt64
}
if end != "" {
e, err := time.Parse(time.RFC3339, end)
if err != nil {
return err
}
cmd.endTime = e.UnixNano()
} else {
// set end time to max if it is not set.
cmd.endTime = math.MaxInt64
}
if err := cmd.validate(); err != nil {
return err
}
return cmd.export()
}
func (cmd *Command) validate() error {
// validate args
if cmd.retentionPolicy != "" && cmd.database == "" {
return fmt.Errorf("must specify a db")
}
if cmd.startTime != 0 && cmd.endTime != 0 && cmd.endTime < cmd.startTime {
return fmt.Errorf("end time before start time")
}
return nil
}
func (cmd *Command) export() error {
if err := cmd.walkTSMFiles(); err != nil {
return err
}
if err := cmd.walkWALFiles(); err != nil {
return err
}
return cmd.writeFiles()
}
func (cmd *Command) walkTSMFiles() error {
err := filepath.Walk(cmd.dataDir, func(dir string, f os.FileInfo, err error) error {
if err != nil {
return err
}
// check to see if this is a tsm file
ext := fmt.Sprintf(".%s", tsm1.TSMFileExtension)
if filepath.Ext(dir) != ext {
return nil
}
relPath, _ := filepath.Rel(cmd.dataDir, dir)
dirs := strings.Split(relPath, string(byte(os.PathSeparator)))
if len(dirs) < 2 {
return fmt.Errorf("invalid directory structure for %s", dir)
}
if dirs[0] == cmd.database || cmd.database == "" {
if dirs[1] == cmd.retentionPolicy || cmd.retentionPolicy == "" {
key := filepath.Join(dirs[0], dirs[1])
files := cmd.tsmFiles[key]
if files == nil {
files = []string{}
}
cmd.manifest[key] = struct{}{}
cmd.tsmFiles[key] = append(files, dir)
}
}
return nil
})
if err != nil {
return err
}
return nil
}
func (cmd *Command) walkWALFiles() error {
err := filepath.Walk(cmd.walDir, func(dir string, f os.FileInfo, err error) error {
if err != nil {
return err
}
// check to see if this is a wal file
prefix := tsm1.WALFilePrefix
ext := fmt.Sprintf(".%s", tsm1.WALFileExtension)
_, fileName := path.Split(dir)
if filepath.Ext(dir) != ext || !strings.HasPrefix(fileName, prefix) {
return nil
}
relPath, _ := filepath.Rel(cmd.walDir, dir)
dirs := strings.Split(relPath, string(byte(os.PathSeparator)))
if len(dirs) < 2 {
return fmt.Errorf("invalid directory structure for %s", dir)
}
if dirs[0] == cmd.database || cmd.database == "" {
if dirs[1] == cmd.retentionPolicy || cmd.retentionPolicy == "" {
key := filepath.Join(dirs[0], dirs[1])
files := cmd.walFiles[key]
if files == nil {
files = []string{}
}
cmd.manifest[key] = struct{}{}
cmd.walFiles[key] = append(files, dir)
}
}
return nil
})
if err != nil {
return err
}
return nil
}
func (cmd *Command) writeFiles() error {
// open our output file and create an output buffer
var w io.WriteCloser
w, err := os.Create(cmd.out)
if err != nil {
return err
}
defer w.Close()
if cmd.compress {
w = gzip.NewWriter(w)
defer w.Close()
}
s, e := time.Unix(0, cmd.startTime).Format(time.RFC3339), time.Unix(0, cmd.endTime).Format(time.RFC3339)
fmt.Fprintf(w, "# INFLUXDB EXPORT: %s - %s\n", s, e)
// Write out all the DDL
fmt.Fprintln(w, "# DDL")
for key := range cmd.manifest {
keys := strings.Split(key, string(byte(os.PathSeparator)))
db, rp := influxql.QuoteIdent(keys[0]), influxql.QuoteIdent(keys[1])
fmt.Fprintf(w, "CREATE DATABASE %s WITH NAME %s\n", db, rp)
}
fmt.Fprintln(w, "# DML")
for key := range cmd.manifest {
keys := strings.Split(key, string(byte(os.PathSeparator)))
fmt.Fprintf(w, "# CONTEXT-DATABASE:%s\n", keys[0])
fmt.Fprintf(w, "# CONTEXT-RETENTION-POLICY:%s\n", keys[1])
if files, ok := cmd.tsmFiles[key]; ok {
fmt.Printf("writing out tsm file data for %s...", key)
if err := cmd.writeTsmFiles(w, files); err != nil {
return err
}
fmt.Println("complete.")
}
if _, ok := cmd.walFiles[key]; ok {
fmt.Printf("writing out wal file data for %s...", key)
if err := cmd.writeWALFiles(w, cmd.walFiles[key], key); err != nil {
return err
}
fmt.Println("complete.")
}
}
return nil
}
func (cmd *Command) writeTsmFiles(w io.WriteCloser, files []string) error {
fmt.Fprintln(w, "# writing tsm data")
// we need to make sure we write the same order that the files were written
sort.Strings(files)
// use a function here to close the files in the defers and not let them accumulate in the loop
write := func(f string) error {
file, err := os.OpenFile(f, os.O_RDONLY, 0600)
if err != nil {
return fmt.Errorf("%v", err)
}
defer file.Close()
reader, err := tsm1.NewTSMReader(file)
if err != nil {
log.Printf("unable to read %s, skipping\n", f)
return nil
}
defer reader.Close()
if sgStart, sgEnd := reader.TimeRange(); sgStart > cmd.endTime || sgEnd < cmd.startTime {
return nil
}
for i := 0; i < reader.KeyCount(); i++ {
var pairs string
key, typ := reader.KeyAt(i)
values, _ := reader.ReadAll(string(key))
measurement, field := tsm1.SeriesAndFieldFromCompositeKey(key)
// measurements are stored escaped, field names are not
field = escape.String(field)
for _, value := range values {
if (value.UnixNano() < cmd.startTime) || (value.UnixNano() > cmd.endTime) {
continue
}
switch typ {
case tsm1.BlockFloat64:
pairs = field + "=" + fmt.Sprintf("%v", value.Value())
case tsm1.BlockInteger:
pairs = field + "=" + fmt.Sprintf("%vi", value.Value())
case tsm1.BlockBoolean:
pairs = field + "=" + fmt.Sprintf("%v", value.Value())
case tsm1.BlockString:
pairs = field + "=" + fmt.Sprintf("%q", models.EscapeStringField(fmt.Sprintf("%s", value.Value())))
default:
pairs = field + "=" + fmt.Sprintf("%v", value.Value())
}
fmt.Fprintln(w, string(measurement), pairs, value.UnixNano())
}
}
return nil
}
for _, f := range files {
if err := write(f); err != nil {
return err
}
}
return nil
}
func (cmd *Command) writeWALFiles(w io.WriteCloser, files []string, key string) error {
fmt.Fprintln(w, "# writing wal data")
// we need to make sure we write the same order that the wal received the data
sort.Strings(files)
var once sync.Once
warn := func() {
msg := fmt.Sprintf(`WARNING: detected deletes in wal file.
Some series for %q may be brought back by replaying this data.
To resolve, you can either let the shard snapshot prior to exporting the data
or manually editing the exported file.
`, key)
fmt.Fprintln(cmd.Stderr, msg)
}
// use a function here to close the files in the defers and not let them accumulate in the loop
write := func(f string) error {
file, err := os.OpenFile(f, os.O_RDONLY, 0600)
if err != nil {
return fmt.Errorf("%v", err)
}
defer file.Close()
reader := tsm1.NewWALSegmentReader(file)
defer reader.Close()
for reader.Next() {
entry, err := reader.Read()
if err != nil {
n := reader.Count()
fmt.Fprintf(os.Stderr, "file %s corrupt at position %d", file.Name(), n)
break
}
switch t := entry.(type) {
case *tsm1.DeleteWALEntry:
once.Do(warn)
continue
case *tsm1.DeleteRangeWALEntry:
once.Do(warn)
continue
case *tsm1.WriteWALEntry:
var pairs string
for key, values := range t.Values {
measurement, field := tsm1.SeriesAndFieldFromCompositeKey([]byte(key))
// measurements are stored escaped, field names are not
field = escape.String(field)
for _, value := range values {
if (value.UnixNano() < cmd.startTime) || (value.UnixNano() > cmd.endTime) {
continue
}
switch value.Value().(type) {
case float64:
pairs = field + "=" + fmt.Sprintf("%v", value.Value())
case int64:
pairs = field + "=" + fmt.Sprintf("%vi", value.Value())
case bool:
pairs = field + "=" + fmt.Sprintf("%v", value.Value())
case string:
pairs = field + "=" + fmt.Sprintf("%q", models.EscapeStringField(fmt.Sprintf("%s", value.Value())))
default:
pairs = field + "=" + fmt.Sprintf("%v", value.Value())
}
fmt.Fprintln(w, string(measurement), pairs, value.UnixNano())
}
}
}
}
return nil
}
for _, f := range files {
if err := write(f); err != nil {
return err
}
}
return nil
}
|