{"id":2950,"date":"2017-09-04T06:58:46","date_gmt":"2017-09-04T06:58:46","guid":{"rendered":"https:\/\/intelligentbee.com\/blog\/?p=2950"},"modified":"2025-03-06T07:31:57","modified_gmt":"2025-03-06T07:31:57","slug":"mysqldump-through-http-request-golang","status":"publish","type":"post","link":"https:\/\/intelligentbee.com\/blog\/mysqldump-through-http-request-golang\/","title":{"rendered":"Mysqldump Through a HTTP Request with  Golang"},"content":{"rendered":"<p>So, in a previous post I explained how one can backup all databases on a server, each in its own dump file. Let&#8217;s take it to the next level and make a Golang program that will let us run the dump process with a HTTP request.<\/p>\n<p>Assuming you already have Go installed on the backup server, create first a project directory in your home folder for example. Copy the mysql dump script from <a href=\"https:\/\/intelligentbee.com\/blog\/mysqldump-command-useful-usage-examples\/\">here<\/a> and save it as dump.sh in your project folder. Modify <code>ROOTDIR=\"\/backup\/mysql\/\"<\/code> inside dump.sh to reflect current project directory.<\/p>\n<h2>Mysqldump<\/h2>\n<p>We will create a Golang script with two functions. One will launch the backup script when a specific HTTP request is done. The other one will put the HTTP call behind a authentication, so only people with credentials will be able to make the backup request.<\/p>\n<pre class=\"nums:false nums-toggle:false lang:go decode:true \" title=\"http-db-backup.go\">package main\r\n\r\nimport (\r\n\t\"encoding\/base64\"\r\n\t\"fmt\"\r\n\t\"log\"\r\n\t\"net\/http\"\r\n\t\"os\"\r\n\t\"os\/exec\"\r\n\t\"strings\"\r\n)\r\n\r\nvar username = os.Getenv(\"DB_BACKUP_USER\")\r\nvar password = os.Getenv(\"DB_BACKUP_PASSWORD\")\r\n\r\nfunc BasicAuth(w http.ResponseWriter, r *http.Request, user, pass string) bool {\r\n\ts := strings.SplitN(r.Header.Get(\"Authorization\"), \" \", 2)\r\n\tif len(s) != 2 {\r\n\t\treturn false\r\n\t}\r\n\tb, err := base64.StdEncoding.DecodeString(s[1])\r\n\tif err != nil {\r\n\t\treturn false\r\n\t}\r\n\tpair := strings.SplitN(string(b), \":\", 2)\r\n\tif len(pair) != 2 {\r\n\t\treturn false\r\n\t}\r\n\treturn pair[0] == string(user) &amp;&amp; pair[1] == string(pass)\r\n}\r\nfunc handler(w http.ResponseWriter, r *http.Request) {\r\n\tif BasicAuth(w, r, username, password) {\r\n\t\tcmd := exec.Command(\"bash\", \"dump.sh\")\r\n\t\tstdout, err := cmd.Output()\r\n\t\tif err != nil {\r\n\t\t\tlog.Fatal(err)\r\n\t\t}\r\n\t\tfmt.Fprintf(w, string(stdout))\r\n\t\treturn\r\n\t}\r\n\tw.Header().Set(\"WWW-Authenticate\", `Basic realm=\"Protected Page!!! \"`)\r\n\tw.WriteHeader(401)\r\n\tw.Write([]byte(\"401 Unauthorized\\n\"))\r\n}\r\n\r\nfunc main() {\r\n\thttp.HandleFunc(\"\/backup\", handler)\r\n\thttp.ListenAndServe(\":8080\", nil)\r\n}\r\n<\/pre>\n<p>This uses <code>DB_BACKUP_USER<\/code> and <code>DB_BACKUP_PASSWORD<\/code> that you will have to set as environment variables. Just append this to your <code>~\/.bashrc<\/code> file<\/p>\n<pre class=\"nums:false lang:sh decode:true \" title=\"~\/.bashrc\">export DB_BACKUP_USER=\"hello\"\r\nexport DB_BACKUP_PASSWORD=\"password\"<\/pre>\n<p>Now run <code>source ~\/.bashrc<\/code> to load them.<\/p>\n<p>Build the executable with <code>go build http-db-backup.go<\/code> where http-db-backup.go is the name of your Go file. Now you need to run the executable with sudo, but while preserving the environment: <code>sudo -E .\/http-db-backup<\/code><\/p>\n<p>Now if you open your browser and open http:\/\/111.222.333.444:8080\/backup (where 111.222.333.444 is your backup machine IP) the backup process will start, and you will get the output of the dump.sh in your browser when backup finishes.<\/p>\n<p>We can furthermore add another function to list the directory in browser, so you can download the needed backup or backups.<\/p>\n<pre class=\"toolbar:2 nums:false lang:go decode:true \">func lister(w http.ResponseWriter, r *http.Request) {\r\n\tif BasicAuth(w, r, username, password) {\r\n\t\thttp.FileServer(http.Dir(\".\")).ServeHTTP(w, r)\r\n\t\treturn\r\n\t}\r\n\tw.Header().Set(\"WWW-Authenticate\", `Basic realm=\"Protected Page!!! \"`)\r\n\tw.WriteHeader(401)\r\n\tw.Write([]byte(\"401 Unauthorized\\n\"))\r\n}<\/pre>\n<p>All you need to do is to add <code>http.HandleFunc(\"\/\", lister)<\/code> to your <code>main()<\/code> and navigate to http:\/\/111.222.333.444:8080\/ . You will be able to navigate the backup directory to download the dump files.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, in a previous post I explained how one can backup all databases on a server, each in its own [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":2960,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73,78],"tags":[147,176],"yst_prominent_words":[1056,1065,1064,1063,1062,1061,1060,1059,1058,1057,281,1055,1013,970,798,729,629,426,334],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2950"}],"collection":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/comments?post=2950"}],"version-history":[{"count":4,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2950\/revisions"}],"predecessor-version":[{"id":133376,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/2950\/revisions\/133376"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media\/2960"}],"wp:attachment":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media?parent=2950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/categories?post=2950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/tags?post=2950"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=2950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}