linux c数据库备份第四版
该版本算是比较成熟的啦,欢迎大伙拿来试用!!!
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数据库备份第四版的更多相关文章
- linux c数据库备份第五版
linux下c实现的数据库备份程序终于迎来第五版啦,这样改程序就暂告一段落啦,有点小激动呢...接下来的一周(可能两周)时间里,我会用一个小型的网络游戏(比拼99乘法)作为我学习linux c的毕业之 ...
- linux c数据库备份第三版
这个版本相对第一版更新了很多,其实我本地定义为第五版的.相对第一版主要更新内容:1.增加了定时器2.用户可以停止调备份程序3.如果备份程序正在运行,那么尝试运行提示已经在运行4.记录程序运行时的pid ...
- linux c数据库备份第二版
#想知道更多请查看第一版"linux c数据库备份第一版" #include<sys/types.h> #include<sys/wait.h> #incl ...
- linux c数据库备份第一版
使用linuxC实现的mysql数据库备份目标:通过alarm信号定时备份数据库备注:目前是第一个版,本身不能定时备份可以结合linux自动化实现定时备份.运行平台:Linux或类unix测试平台:u ...
- Linux下数据库备份
1.登录数据库服务器并切换到数据库用户下 [root@*** ~]# su - oracle 2.测试用户名密码是否能正确连接数据库 [oracle@*** ~]$ sqlplus username/ ...
- Linux下数据库备份恢复过程
1. 远程进入Linux服务器. 2. 一般登录的是root用户, 第一步切换到Oracle用户, 命令: su - oracle 3. 查看服务器上面数据库的监听的状况 lsnrctl 之后输入命令 ...
- Linux oracle数据库自动备份自动压缩脚本代码
Linux oracle数据库备份完成后可以自动压缩脚本代码. 复制代码代码如下: #!/bin/bash #backup.sh #edit: www.jbxue.com ##系统名称 sysname ...
- Linux下mysql备份 恢复
转载自http://blog.chinaunix.net/uid-20577907-id-161611.html 比如我们要备份mysql中已经存在的名为linux的数据库,要用到命令mysqldum ...
- SQL Server数据库备份方法
数据库备份,是在数据丢失的情况下,能及时恢复重要数据,防止数据丢失的一种重要手段.一个合理的数据库备份方案,应该能够在数据丢失时,有效地恢复重要数据,同时需要考虑技术实现难度和有效地利用资源. 数据库 ...
随机推荐
- nyist 58 最小步数 BFS
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 1,0 ...
- mac上charels抓包工具使用技巧
有这俩技巧就足够了 http://www.jianshu.com/p/18449f5f9d1c http://blog.csdn.net/u010187139/article/details/5198 ...
- Linux散列表(一)——操作函数
散列表(又名哈希表)仅仅需要一个包含单一指针的链表头.它是双向链表的变体.它不同于双链表——表头和结点使用相同的结构体——散列表对表头和结点有不同的定义.如下: struct hlist_head { ...
- ios解析XML和json数据
解析的基本概念所谓“解析”:从事先规定好的格式串中提取数据解析的前提:提前约定好格式.数据提供方按照格式提供数据.数据获取方按照格式获取数据iOS开发常见的解析:XML解析.JSON解析 一.XML数 ...
- Struts2接收参数的几种方式
一.用Action属性 在action里定义要接收的参数,并提供相应的set和get方法. 如: public class LoginAction extends ActionSupport { pr ...
- adb出现adb server is out of date时的解决的方法
出错的原因是adb的port被其它程序的进程占据了,所以要做的就是找到并kill该进程.步骤:. 1.在cmd中运行adb nodaemon server,查看adb的port号是多少,普通情况下是5 ...
- Sea.js提供简单、极致的模块化开发体验
为什么使用 Sea.js ? Sea.js 追求简单.自然的代码书写和组织方式,具有以下核心特性: 简单友好的模块定义规范:Sea.js 遵循 CMD 规范,可以像 Node.js 一般书写模块代码. ...
- sdaf
- 实例化讲解 RunLoop
实例化讲解RunLoop 之前看过很多有关RunLoop的文章,其中要么是主要介绍RunLoop的基本概念,要么是主要讲解RunLoop的底层原理,很少用真正的实例来讲解RunLoop的,这其中有大部 ...
- 在cmd窗口下运行Java程序时无法找到主类的解决办法
我是Java的初学者,昨天在cmd窗口下运行一段Java程序时总是有问题,可以编译但无法执行. 也就是javac时正确,一旦java时就不对了,提示找不到或无法加载主类,经百度谷歌再加上自己的摸索终于 ...