该版本算是比较成熟的啦,欢迎大伙拿来试用!!!
1.新增数据库连接和备份时间配置文件conf
2.新增日志文件,程序运行的一些异常会记录在log文件下

后续的工作:
1.将代码切割为多个文件,分类存放代码
2.加入自动后台运行的支持
3.加入开机自动运行的支持
完成上面3个之后,我的linux c数据库备份程序就暂时靠一段落了。

使用提醒:
编译:gcc -o main main.c
后台启动:./main &
停止程序:./main stop

#include<sys/types.h>
#include<sys/wait.h>
#include<ctype.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<signal.h>
#include<time.h>
#include<stdio.h> //程序运行的pid信息
#define PID_FILE "./pid.db"
//记录待备份的数据库信息文件
#define DB_FILE "./db_list"
//配置文件信息
#define CONF_FILE "./conf"
//日志文件
#define LOG_FILE "./log"
//最大备份的数据库数量
#define NUM 20
//数据库名字长度的限制
#define LEN 128
//程序轮询时间间隔
#define ALARM_TIME 10 //从文件读取到的数据库信息保存至该数组中
char *db_list[NUM];
//当前待备份的数据库数量
int read_num;
//是否用户终止备份
int isbreak = ; //数据库连接信息
typedef struct db_conf {
char *host;
char *user;
char *pass;
}CONF;
//数据库备份时间
typedef struct bat_t {
int hour;
int min;
}BAT_T; //malloc
void malloc_dblist();
//free
void free_dblist();
//读取待备份的数据库信息
int readDbFile();
//读取配置文件信息(数据库连接信息,备份时间等)
CONF readConfFile();
//读取备份的时间信息
BAT_T readBatTFile();
//记录日志信息
void recordLog(char *);
//信号处理函数
void signHandler(int sig);
//记录程序运行的pid信息
int recordPid(int pid);
//获取程序运行时的pid信息
int readPid(void);
//移除程序运行时的pid信息
void delPid(void); int main(int argc, char *argv[])
{
CONF conf;
BAT_T bt;
pid_t pid, old_pid;
int i, prs;
char buf[LEN];
time_t t;
struct tm *tm_ptr; struct sigaction act, oldact;
sigset_t newmask, suspmask, oldmask; if (argc >= ) {
old_pid = (pid_t)readPid();
//停止掉备份程序
if (strcmp(argv[], "stop") == ) {
kill(old_pid, SIGINT);
return ;
}
else if (strcmp(argv[], "restart") == ) {
kill(old_pid, SIGINT);
sleep();
}
}
old_pid = (pid_t)readPid();
//检测程序是否已经在运行
if (old_pid > ) {
fprintf(stderr, "Progress is running.\n");
return -;
} //记录程序运行的pid信息
prs = recordPid((int)getpid());
if (prs == -) {
fprintf(stderr, "Open pid.db file error.\n");
return -;
}
//读取待备份的数据库
int rs = readDbFile();
if (rs) {
delPid();
return rs;
}
//读取数据配置信息
conf = readConfFile();
//读取备份时间
bt = readBatTFile(); //信号接管
act.sa_handler = signHandler;
sigemptyset(&act.sa_mask);
act.sa_flags = ;
sigaction(SIGALRM, &act, );
sigaction(SIGINT, &act, ); while () {
time(&t);
tm_ptr = localtime(&t); //备份时间内进行备份
if (bt.hour == (int)tm_ptr->tm_hour && bt.min == (int)tm_ptr->tm_min) {
for (i = ; i < read_num; i++) {
memset(buf, '\0', LEN);
//密码为空
if (!strlen(conf.pass)) {
sprintf(buf, "mysqldump -h%s -u%s %s > %s_%02d%02d%02d.sql",
conf.host, conf.user, db_list[i], db_list[i],
tm_ptr->tm_year+, tm_ptr->tm_mon+, tm_ptr->tm_mday);
}
else {
sprintf(buf, "mysqldump -h%s -u%s -p%s %s > %s_%02d%02d%02d.sql",
conf.host, conf.user, conf.pass, db_list[i], db_list[i],
tm_ptr->tm_year+, tm_ptr->tm_mon+, tm_ptr->tm_mday);
}
system(buf);
}
} alarm(ALARM_TIME);
pause(); if (isbreak) {
recordLog("User break progress.");
break;
}
} free_dblist(); delPid(); exit(); } void malloc_dblist()
{
int i = ;
//malloc for db_list
for (i = ; 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 = ; i < NUM; i++) {
free(db_list[i]);
}
} int readDbFile()
{
FILE *fp; fp = fopen(DB_FILE, "r");
if (!fp) {
char buf[];
sprintf(buf, "%s not found\n", DB_FILE);
recordLog(buf);
fprintf(stderr, "%s not found\n", DB_FILE);
return ;
}
else {
malloc_dblist(); read_num = ;
while (fscanf(fp, "%127[^\r\n]\n", db_list[read_num]) == ) {
read_num++;
} fclose(fp); return ;
} } CONF readConfFile()
{
FILE *fp;
CONF conf; if (!(fp = fopen(CONF_FILE, "r"))) {
conf.host = "localhost";
conf.user = "root";
conf.pass = "";
return conf;
} char buf[];
while ((fscanf(fp, "%127[^\r\n]\n", buf)) == ) {
char *tmp1 = strtok(buf, "=");
char *tmp2 = strtok(NULL, "="); if (strstr(tmp1, "HOST")) {
if (tmp2) {
conf.host = strdup(tmp2);
}
else {
conf.host = "localhost";
}
}
else if (strstr(tmp1, "USER")) {
if (tmp2) {
conf.user = strdup(tmp2);
}
else {
conf.host = "root";
}
}
else if (strstr(tmp1, "PASS")) {
if (tmp2) {
conf.pass = strdup(tmp2);
}
else {
conf.pass = "";
}
}
} return conf;
} BAT_T readBatTFile()
{
FILE *fp;
BAT_T bat_time; if (!(fp = fopen(CONF_FILE, "r"))) {
bat_time.hour = ;
bat_time.min = ;
return bat_time;
} char buf[];
while ((fscanf(fp, "%127[^\r\n]\n", buf)) == ) {
if (!strstr(buf, "BAT_TIME"))
continue; //获取到备份数据数据
char *tmp1 = strtok(buf, "=");
char *tmp2 = strtok(NULL, "=");
//对备份时间数据进行分割
char *hour = strtok(tmp2, " ");
char *min = strtok(NULL, " "); if (hour) {
bat_time.hour = atoi(hour);
}
else {
bat_time.hour = ;
} if (min) {
bat_time.min = atoi(min);
}
else {
bat_time.min = ;
}
} return bat_time;
} void recordLog(char *msg)
{
FILE *fp;
fp = fopen(LOG_FILE, "a");
if (fp) {
time_t t;
struct tm *tm_ptr;
time(&t);
tm_ptr = localtime(&t); fprintf(fp, "%d-%d-%d %d:%d:%d: %s\n", (tm_ptr->tm_year+), tm_ptr->tm_mon, tm_ptr->tm_mday,
tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec, msg);
fclose(fp);
}
} void signHandler(int sig)
{
char buf[];
switch (sig) {
case SIGALRM:
//fprintf(stdout, "alarm signal comming:%d.\n", sig);
break;
case SIGINT:
//fprintf(stdout, "sigint signal comming:%d.\n", sig);
isbreak = ;
break;
default:
//fprintf(stdout, "uncatched signal comming:%d.\n", sig);
sprintf(buf, "uncatched signal comming:%d.\n", sig);
recordLog(buf);
}
} int recordPid(int pid)
{
FILE *fp = NULL; if (!(fp = fopen(PID_FILE, "w")))
return -; pid = getpid();
fprintf(fp, "%d", (int)pid);
fclose(fp); return ;
} int readPid(void)
{
FILE *fp = NULL; if (!(fp = fopen(PID_FILE, "r")))
return -; int pid;
if (fscanf(fp, "%d", &pid) != ) {
fclose(fp);
return -;
} fclose(fp); return pid;
} void delPid(void)
{
unlink(PID_FILE);
}

mian.c

conf

#数据库服务器地址
HOST=localhost
#数据库账号
USER=root
#数据库密码
PASS=
#备份时间 :小时 分钟
BAT_TIME=

db_list

mkbl
ck_book

linux c数据库备份第四版的更多相关文章

  1. linux c数据库备份第五版

    linux下c实现的数据库备份程序终于迎来第五版啦,这样改程序就暂告一段落啦,有点小激动呢...接下来的一周(可能两周)时间里,我会用一个小型的网络游戏(比拼99乘法)作为我学习linux c的毕业之 ...

  2. linux c数据库备份第三版

    这个版本相对第一版更新了很多,其实我本地定义为第五版的.相对第一版主要更新内容:1.增加了定时器2.用户可以停止调备份程序3.如果备份程序正在运行,那么尝试运行提示已经在运行4.记录程序运行时的pid ...

  3. linux c数据库备份第二版

    #想知道更多请查看第一版"linux c数据库备份第一版" #include<sys/types.h> #include<sys/wait.h> #incl ...

  4. linux c数据库备份第一版

    使用linuxC实现的mysql数据库备份目标:通过alarm信号定时备份数据库备注:目前是第一个版,本身不能定时备份可以结合linux自动化实现定时备份.运行平台:Linux或类unix测试平台:u ...

  5. Linux下数据库备份

    1.登录数据库服务器并切换到数据库用户下 [root@*** ~]# su - oracle 2.测试用户名密码是否能正确连接数据库 [oracle@*** ~]$ sqlplus username/ ...

  6. Linux下数据库备份恢复过程

    1. 远程进入Linux服务器. 2. 一般登录的是root用户, 第一步切换到Oracle用户, 命令: su - oracle 3. 查看服务器上面数据库的监听的状况 lsnrctl 之后输入命令 ...

  7. Linux oracle数据库自动备份自动压缩脚本代码

    Linux oracle数据库备份完成后可以自动压缩脚本代码. 复制代码代码如下: #!/bin/bash #backup.sh #edit: www.jbxue.com ##系统名称 sysname ...

  8. Linux下mysql备份 恢复

    转载自http://blog.chinaunix.net/uid-20577907-id-161611.html 比如我们要备份mysql中已经存在的名为linux的数据库,要用到命令mysqldum ...

  9. SQL Server数据库备份方法

    数据库备份,是在数据丢失的情况下,能及时恢复重要数据,防止数据丢失的一种重要手段.一个合理的数据库备份方案,应该能够在数据丢失时,有效地恢复重要数据,同时需要考虑技术实现难度和有效地利用资源. 数据库 ...

随机推荐

  1. POJ 3050 穷举

    题意:给定一个5*5的地图,每个格子上有一个数字.从一个格子出发(上下左右4个方向),走5步将数字连起来可以构造出一个6位数.问该地图可以构造出多少个不同的6位数. 分析:可以对每个格子做深度优先遍历 ...

  2. OpenStack 应用调试

  3. Eclipse : 找不到或无法加载主类

    问题: 找不到或无法加载主类 解决思路: Step1: 去网上百度了下,基本上都说是环境变量的问题. 原因:环境变量设置有问题. 解决方法:重设环境变量 结果:当然问题没解决. Step2:再次百度, ...

  4. Best Reward HDU 3613(回文子串Manacher)

    题目大意:有一个串(全部由小写字母组成),现在要把它分成两部分,如果分开后的部分是回文串就计算出来它的价值总和,如果不是回文的那么价值就是0,最多能得到的最大价值.   分析:首先的明白这个最大价值有 ...

  5. 【bzoj2434】[Noi2011]阿狸的打字机

    题目描述 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的:l 输入小写字 ...

  6. SetupFactory +添加frm2.0工具

    网盘链接: http://pan.baidu.com/s/1c1DFRJM 带单独的添加frm2.0工具 原setupfactory文件下载地址:http://www.pc0359.cn/downin ...

  7. [Javascrip] Logging Timing Data to the Console

    Learn to use console.time with console.timeEnd to get accurate timings of operations in javascript. ...

  8. JSON 遍历转为Model Bean

    @RequestMapping(value = "/batchAddPageIndexBrand") @ResponseBody public HashMap<String, ...

  9. AS 2.0新功能 Instant Run

    Instant Run上手 作为一个Android开发者,很多的时候我们需要花大量的时间在bulid,运行到真机(虚拟机)上,对于ios上的Playground羡慕不已,这种情况将在Android S ...

  10. android省电开发之cpu降频

    众所周知,在android系统的耗电量排行里,cpu的耗电占了比较大的一部分比例,也就是说,cpu的使用率和使用频率将直接或间接的影响电量的分配和使用,但很遗憾,android-sdk中没有为andr ...