Skip to content

Instantly share code, notes, and snippets.

@0x46616c6b
Last active September 19, 2018 08:29
Show Gist options
  • Save 0x46616c6b/112e7a664ef86c8e3d3fa1934e462f79 to your computer and use it in GitHub Desktop.
Save 0x46616c6b/112e7a664ef86c8e3d3fa1934e462f79 to your computer and use it in GitHub Desktop.
Etherpad Migration from Redis to MySQL
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"strings"
"github.com/go-redis/redis"
_ "github.com/go-sql-driver/mysql"
"gopkg.in/cheggaaa/pb.v1"
)
var (
redisHost string
redisPort string
mysqlUser string
mysqlPass string
mysqlDb string
mysqlHost string
mysqlPort string
dryRun bool
)
func main() {
flag.StringVar(&redisHost, "redis-host", "localhost", "redis host")
flag.StringVar(&redisPort, "redis-port", "6379", "redis port")
flag.StringVar(&mysqlUser, "mysql-user", "etherpad", "mysql user")
flag.StringVar(&mysqlPass, "mysql-pass", "etherpad", "mysql password")
flag.StringVar(&mysqlDb, "mysql-db", "etherpad", "mysql database")
flag.StringVar(&mysqlHost, "mysql-host", "127.0.0.1", "mysql host")
flag.StringVar(&mysqlPort, "mysql-port", "3306", "mysql port")
flag.BoolVar(&dryRun, "dry-run", false, "dry-run")
flag.Parse()
db := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", redisHost, redisPort),
DB: 0,
})
defer db.Close()
my, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", mysqlUser, mysqlPass, mysqlHost, mysqlPort, mysqlDb))
if err != nil {
panic(err)
}
defer my.Close()
val, err := db.DbSize().Result()
if err != nil {
panic(err)
}
fmt.Printf("Total keys to process: %d\n", val)
bar := pb.StartNew(int(val))
bar.ShowTimeLeft = true
bar.ShowSpeed = true
var c uint64
var keys []string
for ok := true; ok; {
keys, c, err = db.Scan(c, "", 0).Result()
if err != nil {
panic(err)
}
for _, key := range keys {
bar.Increment()
// Ignore keys prefixed with ueberDB (not needed in MySQL)
if strings.HasPrefix(key, "ueberDB") {
continue
}
// Ignore keys prefixed with mylist (not needed in MySQL)
if strings.HasPrefix(key, "mylist") {
continue
}
val, err := db.Get(key).Result()
if err != nil {
log.Printf("Error: %s is not valid key for scalar value. Message: %s\n", key, err)
continue
}
if !dryRun {
stmt, err := my.Prepare("INSERT INTO store VALUES (?,?)")
if err != nil {
panic(err)
}
_, err = stmt.Exec(key, val)
if err != nil {
panic(err)
}
err = stmt.Close()
if err != nil {
panic(err)
}
}
}
if c == 0 {
ok = false
}
}
bar.Finish()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment