#想知道更多请查看第一版 "Linux C Database backup first Edition"
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 //data table file to be backed up (one row for a database)Ten #defineDb_file "./db_list" One //maximum number of databases that can be backed up A #defineNUM 20 - //The maximum number of characters for a database name - #defineLEN 128 the //to save a database read from Db_file - Char*Db_list[num]; - //the number of databases read from the Db_file file - intRead_num; + //Request Memory Function - voidmalloc_dblist (); + //Releasing memory Functions A voidfree_dblist (); at //Reading database files - voidreaddbfile (); - - intMainintargcChar*argv[]) { - pid_t pid; - inti; in CharBuf[len]; - to //reading database information from a file + readdbfile (); - thePID =fork (); * $ if(PID <0) {Panax Notoginsengfprintf (stderr,"Fork error\n"); -Exit1); the } + A Switch(PID) { the Case-1: +fprintf (stderr,"Fork failed\n"); -Exit1); $ Case 0: $ //A child process makes a backup of the database - for(i =0; i < Read_num; i++) { -memset (BUF,' /', LEN); thesprintf (BUF,"%s%s%s%s%s","Mysqldump-uroot", Db_list[i],">", Db_list[i],". SQL"); - system (BUF);Wuyiprintf"%d,%s\n", I, buf); the } - Break; Wu } - //wait for the end of the child process About if(PID >0) { $ intStat_val; - pid_t child_pid; - -Child_pid = Wait (&stat_val); A + if(!wifexited (Stat_val)) { thefprintf (stdout,"Child terminated abnormaly\n"); - } $Exit1); the the } the the free_dblist (); - inExit0); the the } About the voidmalloc_dblist () the { the inti =0; + //malloc for Db_list - for(i =0; i < NUM; i++) { theDb_list[i] =malloc(LEN);Bayimemset (Db_list[i],' /', LEN); the } the } - voidfree_dblist () - { the inti; the //Free db_list ' s memory the for(i =0; i < NUM; i++) { the Free(Db_list[i]); - } the } the the voidReaddbfile ()94 { theFILE *FP; the thefp = fopen (Db_file,"R");98 if(!FP) { Aboutfprintf (stderr,"%s not found\n", db_file); - }101 Else {102 malloc_dblist ();103 104Read_num =0; the while(FP, fscanf"%127[^\r\n]\n", db_list[read_num]) = =1) {106 puts (Db_list[read_num]);107read_num++;108 }109 the fclose (FP); 111 } the 113}
Linux C Database backup second Edition