commit 31310b108ef50a241b7b77d3daebe124442cb78d Author: surtur Date: Wed Aug 17 17:57:17 2022 +0200 initial commit diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..df460e0 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.dotya.ml/wanderer/drone-sqlite-to-s3 + +go 1.19 + +require github.com/mattn/go-sqlite3 v1.14.15 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..252cb10 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= +github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= diff --git a/main.go b/main.go new file mode 100644 index 0000000..23101b4 --- /dev/null +++ b/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "database/sql" + "fmt" + "log" + "os" + "path" + "time" + + _ "github.com/mattn/go-sqlite3" +) + +const ( + droneDir = "/var/lib/drone" + dbFile = "database.sqlite" + logDir = "dronelogs/logs" +) + +func main() { + log.Println("starting...") + + if err := os.MkdirAll(logDir, os.ModePerm); err != nil { + log.Fatal(err) + } + + filename := path.Join(droneDir, dbFile) + + start := time.Now() + + log.Printf("logs dir: %s\n", logDir) + log.Printf("opening db file: %s\n", filename) + + db, err := sql.Open("sqlite3", "file:"+filename+"?mode:ro") + if err != nil { + log.Fatal(err) + } + + defer db.Close() + + rows, err := db.Query("SELECT * FROM logs") + if err != nil { + log.Fatal(err) + } + defer rows.Close() + + var id int + var row string + + for rows.Next() { + + err = rows.Scan(&id, &row) + + if err != nil { + log.Fatal(err) + } + + log.Printf("handling row id: %d\n", id) + + f, err := os.Create(path.Join(logDir, fmt.Sprint(id))) + if err != nil { + log.Fatal(err) + } + defer f.Close() + + log.Println("saving row to a file") + + _, err = f.Write([]byte(row)) + } + + elapsed := time.Since(start) + + log.Printf("took: %s", elapsed) +}