Linux c Database Backup Version 1 and Database Backup Version 1

Source: Internet
Author: User
Tags printable characters

Linux c Database Backup Version 1 and Database Backup Version 1

Mysql Database Backup Using linuxC
Objective: To regularly back up the database through the alarm signal
Note: It is currently the first version. You can use linux automation to implement regular backup if you cannot perform regular backup.
Running platform: Linux or unix-like
Test Platform: ubuntu server 14.04x64
File Information:
Main. c: database backup program
Db_list: information about the database to be backed up, one file per row.
Disadvantages:
The file reading method is not in place. The fgetc character is used to read the relevant database information, and then the fgets function is used for processing, I encountered a lot of troubles, such as reading the carriage return character \ r, reading the newline character \ n, and reading invisible characters. Therefore, I switched to fgetc to achieve this. At present, the test is still quite good, and you are welcome to propose better solutions. (Note: The fscanf function has been used last night to solve this problem and will be updated later)

Compile and run:
First, write the name of the database to be backed up into a database row in the db_list file, as shown in figure
Admin
Book
Then compile and run
$ Gcc-o mian main. c
$./Main

The content of the main. c file is as follows:

#include <sys / types.h>
#include <sys / wait.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

// Data table file to be backed up (one row per database)
#define DB_FILE "./db_list"
// The maximum number of databases that can be backed up
#define NUM 20
// The longest number of characters in a database name
#define LEN 128
// Save the database read from DB_FILE
char * db_list [NUM];
// Number of databases read from DB_FILE file
int read_num;
// Request memory function
void malloc_dblist ();
// Free memory function
void free_dblist ();
// Read database file
void readDbFile ();

int main (int argc, char * argv []) {
pid_t pid;
int i;
char buf [LEN];

// Read database information from file
readDbFile ();
The
pid = fork ();

if (pid <0) {
fprintf (stderr, "fork error \ n");
exit (1);
}
The
switch (pid) {
case -1:
fprintf (stderr, "fork failed \ n");
exit (1);
case 0:
// The child process backs up the database
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;
}
// Wait for the end of the child process
if (pid> 0) {
int stat_val;
pid_t child_pid;
The
child_pid = wait (& stat_val);
The
if (! WIFEXITED (stat_val)) {
fprintf (stdout, "Child terminated abnormaly \ n");
}
exit (1);
The
}
The
free_dblist ();
The
exit (0);
The
}

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;
int i, j, c, isnewline;
char * rs;
char tmpbuf [LEN];
The
fp = fopen (DB_FILE, "r");
if (! fp) {
fprintf (stderr, "% s not found \ n", DB_FILE);
}
else {
malloc_dblist ();
The
read_num = 1;
j = 0;
isnewline = 0;
while (! feof (fp)) {
c = fgetc (fp);
// Null character or string end character
if (c == '' || c == '\ 0') {
continue;
}
// Enter character
if (c == '\ r') {
continue;
}
// Newline character
if (c == '\ n') {
j = 0;
isnewline = 1;
continue;
}
// Non-printable characters
if (! isprint (c)) {
continue;
}
if (isnewline) {
read_num ++;
isnewline = 0;
}
db_list [read_num-1] [j] = c;
j ++;
}
The
fclose (fp);
}
The
} 


The following is the database information file db_list:

adminbook

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.