国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

c實現linux下的數據庫備份

2020-05-23 14:18:04
字體:
來源:轉載
供稿:網友

本文給大家簡單介紹下c實現linux下的數據庫備份的方法和具體的源碼,十分的實用,有需要的小伙伴可以參考下。

Linux下c實現的數據庫備份,只要修改數據庫列表文件的信息即可。

db_list.txt把后綴去掉即可,一個數據庫一行。

1. main.c

 

 
  1. #include<sys/types.h> 
  2. #include<sys/wait.h> 
  3. #include<ctype.h> 
  4. #include<unistd.h> 
  5. #include<string.h> 
  6. #include<stdlib.h> 
  7. #include<stdio.h> 
  8.  
  9. //待備份的數據表文件(一個數據庫一行) 
  10. #define DB_FILE "./db_list" 
  11. //最多可以備份的數據庫數量 
  12. #define NUM 20 
  13. //一個數據庫名字的最長字符數 
  14. #define LEN 128 
  15. //保存從DB_FILE中讀取到的數據庫 
  16. char *db_list[NUM]; 
  17. //從DB_FILE文件中讀取到的數據庫數量 
  18. int read_num; 
  19. //請求內存函數 
  20. void malloc_dblist(); 
  21. //釋放內存函數 
  22. void free_dblist(); 
  23. //讀取數據庫文件 
  24. void readDbFile(); 
  25.  
  26. int main(int argc, char *argv[]) { 
  27. pid_t pid; 
  28. int i; 
  29. char buf[LEN]; 
  30.  
  31. //從文件讀取數據庫信息 
  32. readDbFile(); 
  33.  
  34. pid = fork(); 
  35.  
  36. if (pid < 0) { 
  37. fprintf(stderr, "fork error/n"); 
  38. exit(1); 
  39.  
  40. switch (pid) { 
  41. case -1: 
  42. fprintf(stderr, "fork failed/n"); 
  43. exit(1); 
  44. case 0: 
  45. //子進程進行數據庫的備份 
  46. for (i = 0; i < read_num; i++) { 
  47. memset(buf, '/0', LEN); 
  48. sprintf(buf, "%s%s%s%s%s""mysqldump -uroot ", db_list[i], " > ", db_list[i], ".sql"); 
  49. system(buf); 
  50. printf("%d,%s/n", i, buf); 
  51. break
  52. //等待子進程的結束 
  53. if (pid > 0) { 
  54. int stat_val; 
  55. pid_t child_pid; 
  56.  
  57. child_pid = wait(&stat_val); 
  58.  
  59. if (!WIFEXITED(stat_val)) { 
  60. fprintf(stdout, "Child terminated abnormaly/n"); 
  61. exit(1); 
  62.  
  63.  
  64. free_dblist(); 
  65.  
  66. exit(0); 
  67.  
  68.  
  69. void malloc_dblist() 
  70. int i = 0; 
  71. //malloc for db_list 
  72. for (i = 0; i < NUM; i++) { 
  73. db_list[i] = malloc(LEN); 
  74. memset(db_list[i], '/0', LEN); 
  75. void free_dblist() 
  76. int i; 
  77. //free db_list's memory 
  78. for (i = 0; i < NUM; i++) { 
  79. free(db_list[i]); 
  80.  
  81. void readDbFile() 
  82. FILE *fp; 
  83.  
  84. fp = fopen(DB_FILE, "r"); 
  85. if (!fp) { 
  86. fprintf(stderr, "%s not found/n", DB_FILE); 
  87. else { 
  88. malloc_dblist(); 
  89.  
  90. read_num = 0; 
  91. while (fscanf(fp, "%127[^/r/n]/n", db_list[read_num]) == 1) { 
  92. puts(db_list[read_num]); 
  93. read_num++; 
  94.  
  95. fclose(fp);  
  96.  

2. db_list.txt

 

 
  1. admin 
  2. book 

3.

 

 
  1. #include<sys/types.h> 
  2. #include<sys/wait.h> 
  3. #include<ctype.h> 
  4. #include<unistd.h> 
  5. #include<string.h> 
  6. #include<stdlib.h> 
  7. #include<stdio.h> 
  8.  
  9. //待備份的數據表文件(一個數據庫一行) 
  10. #define DB_FILE "./db_list" 
  11. //最多可以備份的數據庫數量 
  12. #define NUM 20 
  13. //一個數據庫名字的最長字符數 
  14. #define LEN 128 
  15. //保存從DB_FILE中讀取到的數據庫 
  16. char *db_list[NUM]; 
  17. //從DB_FILE文件中讀取到的數據庫數量 
  18. int read_num; 
  19. //請求內存函數 
  20. void malloc_dblist(); 
  21. //釋放內存函數 
  22. void free_dblist(); 
  23. //讀取數據庫文件 
  24. void readDbFile(); 
  25.  
  26. int main(int argc, char *argv[]) { 
  27. pid_t pid; 
  28. int i; 
  29. char buf[LEN]; 
  30.  
  31. //從文件讀取數據庫信息 
  32. readDbFile(); 
  33.  
  34. pid = fork(); 
  35.  
  36. if (pid < 0) { 
  37. fprintf(stderr, "fork error/n"); 
  38. exit(1); 
  39.  
  40. switch (pid) { 
  41. case -1: 
  42. fprintf(stderr, "fork failed/n"); 
  43. exit(1); 
  44. case 0: 
  45. //子進程進行數據庫的備份 
  46. for (i = 0; i < read_num; i++) { 
  47. memset(buf, '/0', LEN); 
  48. sprintf(buf, "%s%s%s%s%s""mysqldump -uroot ", db_list[i], " > ", db_list[i], ".sql"); 
  49. system(buf); 
  50. printf("%d,%s/n", i, buf); 
  51. break
  52. //等待子進程的結束 
  53. if (pid > 0) { 
  54. int stat_val; 
  55. pid_t child_pid; 
  56.  
  57. child_pid = wait(&stat_val); 
  58.  
  59. if (!WIFEXITED(stat_val)) { 
  60. fprintf(stdout, "Child terminated abnormaly/n"); 
  61. exit(1); 
  62.  
  63.  
  64. free_dblist(); 
  65.  
  66. exit(0); 
  67.  
  68.  
  69. void malloc_dblist() 
  70. int i = 0; 
  71. //malloc for db_list 
  72. for (i = 0; i < NUM; i++) { 
  73. db_list[i] = malloc(LEN); 
  74. memset(db_list[i], '/0', LEN); 
  75. void free_dblist() 
  76. int i; 
  77. //free db_list's memory 
  78. for (i = 0; i < NUM; i++) { 
  79. free(db_list[i]); 
  80.  
  81. void readDbFile() 
  82. FILE *fp; 
  83.  
  84. fp = fopen(DB_FILE, "r"); 
  85. if (!fp) { 
  86. fprintf(stderr, "%s not found/n", DB_FILE); 
  87. else { 
  88. malloc_dblist(); 
  89.  
  90. read_num = 0; 
  91. while (fscanf(fp, "%127[^/r/n]/n", db_list[read_num]) == 1) { 
  92. puts(db_list[read_num]); 
  93. read_num++; 
  94.  
  95. fclose(fp);  
  96.  

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 馆陶县| 井冈山市| 长葛市| 张家港市| 衡阳县| 确山县| 来安县| 岗巴县| 宁乡县| 特克斯县| 千阳县| 嫩江县| 连平县| 西藏| 安义县| 邯郸市| 厦门市| 蒙自县| 台中县| 江山市| 河曲县| 大兴区| 澜沧| 台山市| 图木舒克市| 邵东县| 太仓市| 乌兰察布市| 北票市| 桂林市| 习水县| 环江| 峨边| 屯留县| 聊城市| 宿松县| 江口县| 北宁市| 叙永县| 岱山县| 威海市|