MySQL BLOB export/backup for Linux


SUBMITTED BY: Guest

DATE: Jan. 20, 2013, 4:23 a.m.

FORMAT: C++

SIZE: 3.3 kB

HITS: 1446

  1. #include <mysql.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. #include <sstream>
  8. #include <sys/stat.h>
  9. int main(int argc, char* argv[]) {
  10. // Check if the database name was passed on the cmd-line and if not output usage instructions
  11. // and terminate.
  12. if(argv[1] == NULL) {
  13. std::cout << "Usage :\n";
  14. std::cout << "./docbackup <database_name>\n\n";
  15. std::cout << "(Where '<database_name>' is the name of the MySQL database to connect to.)\n";
  16. return 1;
  17. }
  18. MYSQL *conn;
  19. MYSQL_RES *res;
  20. MYSQL_ROW row;
  21. // Usernames and password changed to protect the guilty
  22. // If you need somewhere to start to make this more useful, I'm sure you can
  23. // work out how to pass the server, username and password on the cmd-line rather than
  24. // being evil and hardcoding it. :)
  25. const char *server = "localhost";
  26. const char *user = "someuser";
  27. const char *password = "somepass";
  28. char *database = argv[1];
  29. std::ofstream myfile;
  30. std::ofstream logfile;
  31. conn = mysql_init(NULL);
  32. // Connect to the database
  33. if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
  34. fprintf(stderr, "%s\n", mysql_error(conn));
  35. return 1;
  36. }
  37. // Send SQL query
  38. if(mysql_query(conn, "select uploads.content,uploads.name,LENGTH(uploads.content),clients.legalname,clients.tradingname from uploads LEFT JOIN clients ON uploads.client_id = clients.client_id where 1")) {
  39. fprintf(stderr, "%s\n", mysql_error(conn));
  40. return 1;
  41. }
  42. res = mysql_use_result(conn);
  43. // Create a directory for this database to export files into
  44. mkdir(argv[1],S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  45. std::stringstream f;
  46. f << argv[1] << "/" << "files.csv";
  47. std::string logfilename = f.str();
  48. // Output results
  49. logfile.open(logfilename.c_str(), std::ios::out);
  50. printf("files:\n");
  51. while((row = mysql_fetch_row(res)) != NULL) {
  52. std::string lname;
  53. std::string tname;
  54. if(row[3] != NULL)
  55. lname = row[3];
  56. if(row[4] != NULL)
  57. tname = row[4];
  58. std::stringstream s;
  59. s << "\"" << row[1] << "\"," << "\"" << lname << "\",\"" << tname << "\"" << std::endl;
  60. std::string log = s.str();
  61. std::stringstream of;
  62. of << argv[1] << "/" << row[1];
  63. std::string outfilename = of.str();
  64. myfile.open(outfilename.c_str(), std::ios::binary | std::ios::out );
  65. int size = strtol(row[2],NULL,0);
  66. myfile.write(row[0], size);
  67. myfile.close();
  68. printf("%s (%d)\n", row[1], size);
  69. logfile.write(log.c_str(), log.size());
  70. }
  71. logfile.close();
  72. // close connection
  73. mysql_free_result(res);
  74. mysql_close(conn);
  75. return 1;
  76. }

comments powered by Disqus