欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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語言小游戲程序之彈跳小球的實現(xiàn)代碼

    c語言小游戲程序之彈跳小球的實現(xiàn)代碼

    這篇文章主要介紹了c語言小游戲程序之彈跳小球的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • C語言實現(xiàn)手寫Map(全功能)的示例代碼

    C語言實現(xiàn)手寫Map(全功能)的示例代碼

    這篇文章主要為大家詳細介紹了如何利用C語言實現(xiàn)手寫Map(全功能),文中的示例代碼講解詳細,對我們學習C語言有一定幫助,需要的可以參考一下
    2022-08-08
  • C語言輸入一個數(shù)判斷是否為素數(shù)的多種方法

    C語言輸入一個數(shù)判斷是否為素數(shù)的多種方法

    素數(shù)是只能被1和它自己本身整除,不能被其他自然數(shù)整除的大于1的正整數(shù),下面這篇文章主要給大家介紹了關(guān)于C語言輸入一個數(shù)判斷是否為素數(shù)的多種方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-04-04
  • C++深淺拷貝和string類的兩種寫法詳解

    C++深淺拷貝和string類的兩種寫法詳解

    這篇文章主要為大家詳細介紹了C++深淺拷貝和string類的兩種寫法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • delete[] p->elems和free(p->elems)區(qū)別介紹

    delete[] p->elems和free(p->elems)區(qū)別介紹

    delete[]和free()都是釋放內(nèi)存的函數(shù),但它們具有不同的使用方法和適用情況,這篇文章主要介紹了delete[] p->elems和free(p->elems)有什么區(qū)別,需要的朋友可以參考下
    2023-04-04
  • C語言超詳細分析多進程的概念與使用

    C語言超詳細分析多進程的概念與使用

    在一個項目中并發(fā)執(zhí)行任務(wù)時多數(shù)情況下都會選擇多線程,但有時候也會選擇多進程,例如可以同時運行n個記事本編輯不同文本,由一個命令跳轉(zhuǎn)到另外一個命令,或者使用不同進程進行協(xié)作
    2022-08-08
  • 一些C語言中字符串的算法問題解決實例小結(jié)

    一些C語言中字符串的算法問題解決實例小結(jié)

    這篇文章主要介紹了一些C語言中字符串的算法問題解決實例小結(jié),包括將字符串轉(zhuǎn)化為int類型的數(shù)及旋轉(zhuǎn)字符串等操作,需要的朋友可以參考下
    2016-03-03
  • C語言實現(xiàn)二叉鏈表存儲

    C語言實現(xiàn)二叉鏈表存儲

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)二叉鏈表存儲的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 解決C語言中使用scanf連續(xù)輸入兩個字符類型的問題

    解決C語言中使用scanf連續(xù)輸入兩個字符類型的問題

    這篇文章主要介紹了解決C語言中使用scanf連續(xù)輸入兩個字符類型的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • C++深入探索類和對象之封裝及class與struct的區(qū)別

    C++深入探索類和對象之封裝及class與struct的區(qū)別

    C++?類與對象涉及的知識點非常廣泛,所以我準備寫成幾個特定的部分來作為博文分享,這次的blog將詳細講解類的屬性、行為、訪問權(quán)限,class與struct的區(qū)別以及具體案例,希望能夠?qū)δ銈冇袔椭鉀Q入門小白或者對這方面了解不多的朋友們,那么接下來開始今天的內(nèi)容
    2022-05-05

最新評論