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

首頁 > 編程 > C > 正文

c實現linux下的數據庫備份

2020-01-26 15:02:00
字體:
來源:轉載
供稿:網友

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

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

1. main.c  

#include<sys/types.h>#include<sys/wait.h>#include<ctype.h>#include<unistd.h>#include<string.h>#include<stdlib.h>#include<stdio.h> //待備份的數據表文件(一個數據庫一行)#define DB_FILE "./db_list"http://最多可以備份的數據庫數量#define NUM 20//一個數據庫名字的最長字符數#define LEN 128//保存從DB_FILE中讀取到的數據庫char *db_list[NUM];//從DB_FILE文件中讀取到的數據庫數量int read_num;//請求內存函數void malloc_dblist();//釋放內存函數void free_dblist();//讀取數據庫文件void readDbFile(); int main(int argc, char *argv[]) {  pid_t pid;  int i;  char buf[LEN];   //從文件讀取數據庫信息  readDbFile();     pid = fork();   if (pid < 0) {    fprintf(stderr, "fork error/n");    exit(1);  }     switch (pid) {    case -1:      fprintf(stderr, "fork failed/n");      exit(1);    case 0:      //子進程進行數據庫的備份      for (i = 0; i < read_num; i++) {        memset(buf, '/0', LEN);        sprintf(buf, "%s%s%s%s%s", "mysqldump -uroot ", db_list[i], " > ", db_list[i], ".sql");        system(buf);        printf("%d,%s/n", i, buf);      }      break;  }  //等待子進程的結束  if (pid > 0) {    int stat_val;    pid_t child_pid;         child_pid = wait(&stat_val);         if (!WIFEXITED(stat_val)) {      fprintf(stdout, "Child terminated abnormaly/n");    }    exit(1);       }     free_dblist();     exit(0);   } void malloc_dblist(){  int i = 0;  //malloc for db_list  for (i = 0; i < NUM; i++) {    db_list[i] = malloc(LEN);    memset(db_list[i], '/0', LEN);  }}void free_dblist(){  int i;  //free db_list's memory  for (i = 0; i < NUM; i++) {    free(db_list[i]);  }} void readDbFile(){  FILE *fp;     fp = fopen(DB_FILE, "r");  if (!fp) {    fprintf(stderr, "%s not found/n", DB_FILE);  }  else {    malloc_dblist();         read_num = 0;    while (fscanf(fp, "%127[^/r/n]/n", db_list[read_num]) == 1) {      puts(db_list[read_num]);      read_num++;    }         fclose(fp);   }   }

2. db_list.txt

adminbook

3.

#include<sys/types.h>#include<sys/wait.h>#include<ctype.h>#include<unistd.h>#include<string.h>#include<stdlib.h>#include<stdio.h> //待備份的數據表文件(一個數據庫一行)#define DB_FILE "./db_list"http://最多可以備份的數據庫數量#define NUM 20//一個數據庫名字的最長字符數#define LEN 128//保存從DB_FILE中讀取到的數據庫char *db_list[NUM];//從DB_FILE文件中讀取到的數據庫數量int read_num;//請求內存函數void malloc_dblist();//釋放內存函數void free_dblist();//讀取數據庫文件void readDbFile(); int main(int argc, char *argv[]) {  pid_t pid;  int i;  char buf[LEN];   //從文件讀取數據庫信息  readDbFile();     pid = fork();   if (pid < 0) {    fprintf(stderr, "fork error/n");    exit(1);  }     switch (pid) {    case -1:      fprintf(stderr, "fork failed/n");      exit(1);    case 0:      //子進程進行數據庫的備份      for (i = 0; i < read_num; i++) {        memset(buf, '/0', LEN);        sprintf(buf, "%s%s%s%s%s", "mysqldump -uroot ", db_list[i], " > ", db_list[i], ".sql");        system(buf);        printf("%d,%s/n", i, buf);      }      break;  }  //等待子進程的結束  if (pid > 0) {    int stat_val;    pid_t child_pid;         child_pid = wait(&stat_val);         if (!WIFEXITED(stat_val)) {      fprintf(stdout, "Child terminated abnormaly/n");    }    exit(1);       }     free_dblist();     exit(0);   } void malloc_dblist(){  int i = 0;  //malloc for db_list  for (i = 0; i < NUM; i++) {    db_list[i] = malloc(LEN);    memset(db_list[i], '/0', LEN);  }}void free_dblist(){  int i;  //free db_list's memory  for (i = 0; i < NUM; i++) {    free(db_list[i]);  }} void readDbFile(){  FILE *fp;     fp = fopen(DB_FILE, "r");  if (!fp) {    fprintf(stderr, "%s not found/n", DB_FILE);  }  else {    malloc_dblist();         read_num = 0;    while (fscanf(fp, "%127[^/r/n]/n", db_list[read_num]) == 1) {      puts(db_list[read_num]);      read_num++;    }         fclose(fp);   }   }

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 弋阳县| 景宁| 金平| 巴中市| 许昌县| 温宿县| 湖南省| 福建省| 阳高县| 高邮市| 昌图县| 天台县| 江阴市| 三河市| 彰武县| 娄烦县| 竹溪县| 新密市| 临安市| 定西市| 射阳县| 康保县| 常宁市| 邯郸市| 天台县| 乐陵市| 稻城县| 上饶市| 兴山县| 高安市| 南城县| 汽车| 芒康县| 澄迈县| 宁阳县| 恭城| 桃园市| 集贤县| 星子县| 潞西市| 新竹市|