linux c数据库备份第二版

#想知道更多请查看第一版"linux c数据库备份第一版"

  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 //待备份的数据表文件(一个数据库一行)
 10 #define DB_FILE "./db_list"
 11 //最多可以备份的数据库数量
 12 #define NUM 20
 13 //一个数据库名字的最长字符数
 14 #define LEN 128
 15 //保存从DB_FILE中读取到的数据库
 16 char *db_list[NUM];
 17 //从DB_FILE文件中读取到的数据库数量
 18 int read_num;
 19 //请求内存函数
 20 void malloc_dblist();
 21 //释放内存函数
 22 void free_dblist();
 23 //读取数据库文件
 24 void readDbFile();
 25
 26 int main(int argc, char *argv[]) {
 27     pid_t pid;
 28     int i;
 29     char buf[LEN];
 30
 31     //从文件读取数据库信息
 32     readDbFile();
 33
 34     pid = fork();
 35
 36     if (pid < 0) {
 37         fprintf(stderr, "fork error\n");
 38         exit(1);
 39     }
 40
 41     switch (pid) {
 42         case -1:
 43             fprintf(stderr, "fork failed\n");
 44             exit(1);
 45         case 0:
 46             //子进程进行数据库的备份
 47             for (i = 0; i < read_num; i++) {
 48                 memset(buf, ‘\0‘, LEN);
 49                 sprintf(buf, "%s%s%s%s%s", "mysqldump -uroot ", db_list[i], " > ", db_list[i], ".sql");
 50                 system(buf);
 51                 printf("%d,%s\n", i, buf);
 52             }
 53             break;
 54     }
 55     //等待子进程的结束
 56     if (pid > 0) {
 57         int stat_val;
 58         pid_t child_pid;
 59
 60         child_pid = wait(&stat_val);
 61
 62         if (!WIFEXITED(stat_val)) {
 63             fprintf(stdout, "Child terminated abnormaly\n");
 64         }
 65         exit(1);
 66
 67     }
 68
 69     free_dblist();
 70
 71     exit(0);
 72
 73 }
 74
 75 void malloc_dblist()
 76 {
 77     int i = 0;
 78     //malloc for db_list
 79     for (i = 0; i < NUM; i++) {
 80         db_list[i] = malloc(LEN);
 81         memset(db_list[i], ‘\0‘, LEN);
 82     }
 83 }
 84 void free_dblist()
 85 {
 86     int i;
 87     //free db_list‘s memory
 88     for (i = 0; i < NUM; i++) {
 89         free(db_list[i]);
 90     }
 91 }
 92
 93 void readDbFile()
 94 {
 95     FILE *fp;
 96
 97     fp = fopen(DB_FILE, "r");
 98     if (!fp) {
 99         fprintf(stderr, "%s not found\n", DB_FILE);
100     }
101     else {
102         malloc_dblist();
103
104         read_num = 0;
105         while (fscanf(fp, "%127[^\r\n]\n", db_list[read_num]) == 1) {
106             puts(db_list[read_num]);
107             read_num++;
108         }
109
110         fclose(fp);
111     }
112
113 }
时间: 2024-08-08 12:23:25

linux c数据库备份第二版的相关文章

linux c数据库备份第五版

linux下c实现的数据库备份程序终于迎来第五版啦,这样改程序就暂告一段落啦,有点小激动呢...接下来的一周(可能两周)时间里,我会用一个小型的网络游戏(比拼99乘法)作为我学习linux c的毕业之作...后面接着就是选条路走了“linux网络大数据开发”或者“基于linux的微型系统”了...欢迎各位linux c爱好者私信交流... 在发布上个版本的时候说在这个版会加入开启自动和自动后台运行的,后面有网友提出建议和自己考虑之后就觉得没有必要做这个两件事情啦... 该版本和上一个版本的的功能

linux c数据库备份第一版

使用linuxC实现的mysql数据库备份目标:通过alarm信号定时备份数据库备注:目前是第一个版,本身不能定时备份可以结合linux自动化实现定时备份.运行平台:Linux或类unix测试平台:ubuntu server 14.04 x64文件信息:main.c:数据库备份程序db_list:待备份的数据库信息,一行一个文件.不足:文件的读取方式感觉还不到位,使用的是fgetc一个个字符读取然后过滤和组合来读取相关的数据库信息:开始使用的是fgets函数处理的时候,遇到了很多的麻烦,比如读取

linux c数据库备份第三版

这个版本相对第一版更新了很多,其实我本地定义为第五版的.相对第一版主要更新内容:1.增加了定时器2.用户可以停止调备份程序3.如果备份程序正在运行,那么尝试运行提示已经在运行4.记录程序运行时的pid信息5.支持** start;** restart;** stop等命令还有其他细节的更新.不足:restart的支持还不是很完美,因为没有考虑到服务器繁忙等情况. 运行示例:编译:gcc -o main main.c运行:./main重启./main restart关闭./main stop #i

Linux文件系统管理命令(第二版)

Linux文件系统管理命令 常用命令 1.df命令 查看分区使用情况 常用选项 -h 比较人性化 -m 以兆字节显示分区使用情况 显示信息: Mounted on:挂载点 Filesystem:对应的具体硬件名 2.du命令 查看文件/目录大小,默认以K为单位 常用选项: -h[humanity] du -h /home/xiaofang -s[统计statistics] du -sh /etc #查看目录大小,常用 3.检测修复文件系统fsck[file system check].e2fsc

Linux用户管理命令(第二版)

添加用户 1.useradd -设置选项 用户名 [-D 查看缺省参数 ] 选项: u: UID [必须是系统中没有的] g:缺省所属用户组GID[最好有] G: 指定用户所属多个组[可以指定这个用户所属的部门等] d: 宿主目录[可以任意指定] s: 命令解释器Shell [必须是包含在/etc/shells文件中的] c:描述信息[可以用于描述该用户是谁,有什么权限,最好要有] e: 指定用户失效时间 E.g. useradd -u 6666 -g root -G sys,apache -d

Linux用户管理案例(第二版)

批量添加用户 1.按照/etc/passwd文件格式编写用户信息文件users.info xiaofang01::1001:503::/home/xiaofang01:/bin/bash  #注意不能有空行,不然会报错 2.newusers < users.info #newusers命令 导入用户信息文件 3.pwunconv #取消shadow password功能 4.以[用户名:密码]的格式编写密码文件passwd.info 5.chpasswd < passwd.info #导入密码

Linux特殊权限分析(第二版)

SetUID[权限值=4] 问题:为什么普通用户可以修改自己的密码? ll $(which passwd) 1.SetUID:当一个可执行程序/命令具有SetUID 权限,用户执行这个程序时,将以这个程序的所有者的身份执行. 2.加SetUID权限: chmod u+s [filename] 或 chmod 4755 [filename] #SetUID权限值=4 E.g. chmod u+s $(which touch) #可以看到newfile2的所有者并不是guest,而是root! 3.

linux下c实现的数据库备份(第四版)

该版本算是比较成熟的啦,欢迎大伙拿来试用!!!1.新增数据库连接和备份时间配置文件conf2.新增日志文件,程序运行的一些异常会记录在log文件下 后续的工作:1.将代码切割为多个文件,分类存放代码2.加入自动后台运行的支持3.加入开机自动运行的支持完成上面3个之后,我的linux c数据库备份程序就暂时靠一段落了. 使用提醒:编译:gcc -o main main.c后台启动:./main &停止程序:./main stop #include<sys/types.h> #includ

Linux下mysql备份 恢复

转载自http://blog.chinaunix.net/uid-20577907-id-161611.html 比如我们要备份mysql中已经存在的名为linux的数据库,要用到命令mysqldump 命令格式如下: [[email protected] root]# mysqldump -u root -p linux > /root/linux.sql Enter password:在这里输入数据库的密码 通过上面的命令,我们要明白两件事,首先备份数据库是要以数据库管理员的身份备份:其次: