最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c實現(xiàn)linux下的數(shù)據(jù)庫備份

 更新時間:2015年07月19日 14:59:16   投稿:hebedich  
本文給大家簡單介紹下c實現(xiàn)linux下的數(shù)據(jù)庫備份的方法和具體的源碼,十分的實用,有需要的小伙伴可以參考下。

Linux下c實現(xiàn)的數(shù)據(jù)庫備份,只要修改數(shù)據(jù)庫列表文件的信息即可。

db_list.txt把后綴去掉即可,一個數(shù)據(jù)庫一行。

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>
 
//待備份的數(shù)據(jù)表文件(一個數(shù)據(jù)庫一行)
#define DB_FILE "./db_list"
//最多可以備份的數(shù)據(jù)庫數(shù)量
#define NUM 20
//一個數(shù)據(jù)庫名字的最長字符數(shù)
#define LEN 128
//保存從DB_FILE中讀取到的數(shù)據(jù)庫
char *db_list[NUM];
//從DB_FILE文件中讀取到的數(shù)據(jù)庫數(shù)量
int read_num;
//請求內(nèi)存函數(shù)
void malloc_dblist();
//釋放內(nèi)存函數(shù)
void free_dblist();
//讀取數(shù)據(jù)庫文件
void readDbFile();
 
int main(int argc, char *argv[]) {
  pid_t pid;
  int i;
  char buf[LEN];
 
  //從文件讀取數(shù)據(jù)庫信息
  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:
      //子進程進行數(shù)據(jù)庫的備份
      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;
  }
  //等待子進程的結(jié)束
  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

admin
book

3.

#include<sys/types.h>
#include<sys/wait.h>
#include<ctype.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
 
//待備份的數(shù)據(jù)表文件(一個數(shù)據(jù)庫一行)
#define DB_FILE "./db_list"
//最多可以備份的數(shù)據(jù)庫數(shù)量
#define NUM 20
//一個數(shù)據(jù)庫名字的最長字符數(shù)
#define LEN 128
//保存從DB_FILE中讀取到的數(shù)據(jù)庫
char *db_list[NUM];
//從DB_FILE文件中讀取到的數(shù)據(jù)庫數(shù)量
int read_num;
//請求內(nèi)存函數(shù)
void malloc_dblist();
//釋放內(nèi)存函數(shù)
void free_dblist();
//讀取數(shù)據(jù)庫文件
void readDbFile();
 
int main(int argc, char *argv[]) {
  pid_t pid;
  int i;
  char buf[LEN];
 
  //從文件讀取數(shù)據(jù)庫信息
  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:
      //子進程進行數(shù)據(jù)庫的備份
      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;
  }
  //等待子進程的結(jié)束
  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); 
  }
   
}

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

相關(guān)文章

  • C/C++?函數(shù)的存儲位置和占用空間詳解

    C/C++?函數(shù)的存儲位置和占用空間詳解

    Lambda函數(shù)的代碼部分在代碼段中,被捕獲的變量存儲在Lambda函數(shù)對象的內(nèi)部,這些變量的存儲位置取決于Lambda函數(shù)對象的存儲位置,這篇文章主要介紹了C/C++函數(shù)的存儲位置和占用空間,需要的朋友可以參考下
    2023-06-06
  • 基于C語言編寫一個簡單的抽卡小游戲

    基于C語言編寫一個簡單的抽卡小游戲

    這篇文章主要為大家介紹了如何利用C語言實現(xiàn)原神抽卡的小游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-04-04
  • C++中char[]能修改char*卻不行

    C++中char[]能修改char*卻不行

    本文主要介紹了C++中char[]能修改char*卻不行,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • C++標準模板庫map的常用操作

    C++標準模板庫map的常用操作

    今天小編就為大家分享一篇關(guān)于C++標準模板庫map的常用操作,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • C++實現(xiàn)簡單班級成績管理系統(tǒng)

    C++實現(xiàn)簡單班級成績管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C++實現(xiàn)簡單班級成績管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C語言中的正則表達式使用示例詳解

    C語言中的正則表達式使用示例詳解

    正則表達式是使用單個字符串來描述、匹配一系列符合某個句法規(guī)則的字符串。本文通過示例代碼給大家介紹了C語言中的正則表達式使用,感興趣的朋友跟隨小編一起看看吧
    2019-07-07
  • C語言實現(xiàn)停車場項目

    C語言實現(xiàn)停車場項目

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)停車場項目,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C++通過共享內(nèi)存ShellCode實現(xiàn)跨進程傳輸

    C++通過共享內(nèi)存ShellCode實現(xiàn)跨進程傳輸

    在計算機安全領(lǐng)域,ShellCode是一段用于利用系統(tǒng)漏洞或執(zhí)行特定任務(wù)的機器碼,本文主要為大家介紹了C++如何通過共享內(nèi)存ShellCode實現(xiàn)跨進程傳輸,需要的可以參考下
    2023-12-12
  • C++ 自由存儲區(qū)是否等價于堆你知道嗎

    C++ 自由存儲區(qū)是否等價于堆你知道嗎

    自由存儲是C++中通過new與delete動態(tài)分配和釋放對象的抽象概念,而堆(heap)是C語言和操作系統(tǒng)的術(shù)語,是操作系統(tǒng)維護的一塊動態(tài)分配內(nèi)存
    2021-08-08
  • C語言system 自動關(guān)機函數(shù)代碼

    C語言system 自動關(guān)機函數(shù)代碼

    這篇文章主要介紹了C語言system 自動關(guān)機函數(shù)代碼,需要的朋友可以參考下
    2016-04-04

最新評論

永定县| 郯城县| 靖西县| 海林市| 赫章县| 娄烦县| 锡林郭勒盟| 莱州市| 军事| 永嘉县| 永和县| 承德市| 江源县| 南和县| 兴隆县| 灵石县| 五寨县| 独山县| 张家界市| 如东县| 横山县| 阿拉善盟| 土默特左旗| 顺义区| 全南县| 鄂托克前旗| 罗平县| 奈曼旗| 泽普县| 类乌齐县| 通河县| 大足县| 乳源| 五寨县| 罗源县| 衡南县| 梅河口市| 田阳县| 习水县| 宜良县| 河东区|