diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..330dfd8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2017 Muhammad Inam + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..90a1084 --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +# Acquia Site factory local import + +A small utility to import databases for ACSF for local setup. + +## About +Download all the backups from a particular day. You can either download them one by one or rsync all of the once filtering based the date. Acquia adds a suffix to backup like 2017-03-21. + +Create a mapping file, lets call is config.json - with name and id tuples. The name is the name of local DB and ID is the ACSF DB name. You can see the this ID is part of backup you have downloaded from the cloud. + +``` + { + "name": "sitefactory_site1", + "id": "testsub01ldb100001" + }, + { + "name": "sitefactory_site2", + "id": "testsub01ldb100002" + } +``` + +## Usage +``` + -config-file string + Config file path. (default ./config.json) + -mysql-pass string + MySQL password. (default "drupal") + -mysql-user string + MySQL user name. (default "drupal") + -source-dir string + B dump source directory. (default "current folder") +``` + +## Import Databases +Import all databases. You will have to create target databases manually. +``` +./acsfdbimport -source-dir=some/dir -config-file=some/file/config.json +``` +## TODO +* Add better error handling and messages. +* Better command line options. + +## Contribution +* File and issue, feature request. +* Send a PR. diff --git a/main.go b/main.go new file mode 100644 index 0000000..f13780a --- /dev/null +++ b/main.go @@ -0,0 +1,145 @@ +package main + +import ( + "encoding/json" + "errors" + "flag" + "fmt" + "io/ioutil" + "log" + "os" + "os/exec" + "path/filepath" + "strings" +) + +// CommandLineOptions Command line options. +type commandLineOptions struct { + configFile string + sourceDir string + mysqlUser string + mysqlPass string +} + +// Database enrty to import. +type db struct { + ID string `json:"id"` + Name string `json:"name"` +} + +func main() { + args := parseCommandLineArgs() + + dump, err := getDumpFiles(args.sourceDir) + if err != nil { + fmt.Printf("Could not find db dump files (i.e *.sql.gz) in " + args.sourceDir + ".") + os.Exit(1) + } + + db, err := getConfig(args.configFile) + if err != nil { + fmt.Printf("Config file error: %v\n", err) + os.Exit(1) + } + + importDB(dump, db, args) +} + +// Import databases. +func importDB(dump []string, db []db, args *commandLineOptions) { + for _, d := range db { + f, err := findDBFile(d.ID, dump) + if err != nil { + fmt.Println("No file with ID " + d.ID + " found.") + } else { + fmt.Println("Importing " + f + " into " + d.Name) + // TODO: probably better to use StdoutPipe(). + cmd := "zcat " + f + " | mysql -u" + args.mysqlUser + " -p" + args.mysqlPass + " " + d.Name + + _, err := exec.Command("bash", "-c", cmd).Output() + if err != nil { + fmt.Println("Failed to import DB.") + } else { + fmt.Println("DB imported successfully.") + } + } + } +} + +// Find database dump file. +func findDBFile(id string, dbDump []string) (string, error) { + for _, s := range dbDump { + if strings.Contains(s, id) { + return s, nil + } + } + + err := errors.New("database dump file not found") + + return "", err +} + +// Get database dump files - *.sql.gz. +func getDumpFiles(dir string) ([]string, error) { + files, err := ioutil.ReadDir(dir) + if err != nil { + return nil, err + } + res := []string{} + for _, f := range files { + if !f.IsDir() && strings.HasSuffix(f.Name(), ".sql.gz") { + res = append(res, filepath.Join(dir, f.Name())) + } + } + return res, nil +} + +// Get config file. +func getConfig(fileName string) ([]db, error) { + var db []db + + file, err := ioutil.ReadFile(fileName) + if err != nil { + return db, err + } + + json.Unmarshal(file, &db) + + return db, nil +} + +// Parse command line arguments. +func parseCommandLineArgs() *commandLineOptions { + args := &commandLineOptions{} + dir, _ := os.Getwd() + + defaultConfigFile := dir + "/config.json" + configFile := flag.String("config-file", defaultConfigFile, "Config file path.") + sourceDir := flag.String("source-dir", dir, "DB dump source directory.") + mysqlUser := flag.String("mysql-user", "drupal", "MySQL user name.") + mysqlPass := flag.String("mysql-pass", "drupal", "MySQL password.") + + flag.Parse() + + args.configFile = *configFile + args.sourceDir = *sourceDir + args.mysqlUser = *mysqlUser + args.mysqlPass = *mysqlPass + + // validate files. + if _, err := os.Stat(args.configFile); os.IsNotExist(err) { + log.Fatal("Config file " + args.configFile + " does not exist") + } + + fi, err := os.Stat(args.sourceDir) + if err != nil { + fmt.Printf("Source directory " + args.sourceDir + " does not exist") + os.Exit(1) + } + if !fi.IsDir() { + fmt.Printf("Make sure " + args.sourceDir + " is a directory") + os.Exit(1) + } + + return args +}