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数据库备份方法
数据库备份,是在数据丢失的情况下,能及时恢复重要数据,防止数据丢失的一种重要手段.一个合理的数据库备份方案,应该能够在数据丢失时,有效地恢复重要数据,同时需要考虑技术实现难度和有效地利用资源. 数据库 ...
随机推荐
- HDU1251 统计难题(Trie)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- Jenkins 十: 访问控制
1. 打开“系统管理” –> “Configure Global Security”. 2. 选中“启用安全”. 3. 找到“安全域”,选中“Jenkins专有数据库”,选中“允许用户注册”. ...
- 关于ETL的几种运行
一:代码部分 1.新建maven项目 2.添加需要的java代码 3.书写mapper类 4.书写runner类 二:运行方式 1.本地运行 2. 3. 三:本地运行方式 1.解压hadoop到本地 ...
- SCOI2013 多项式的运算
---恢复内容开始--- 又是一道裸数据结构题. 之前受序列操作的蛋疼写法影响,只用一个tag,不知道怎么记,之后看了下别人的,终于领悟要用两个tag,一个add,一个mul,维护相当简单,想清楚就行 ...
- jquery ajaxform上传文件返回不提示信息的问题
在使用jquery的ajaxform插件进行ajax提交表单并且上传文件的时候,返回类型datatype :json但是后台通过写出一个json对象后,在执行完以后没有进入success函数,而是直接 ...
- JAVAEE filter总结
1. 为什么需要filter? filter相当于客户端和服务器端之间的一扇门,就像保安一样.作用:比如说设置字符集和权限控制等等. 2. 细节; * . 只能对post请求起作用 * .可以使 ...
- CentOS 安装 Tomcat
1.Tomcat官网获(http://tomcat.apache.org/)取tar.gz文件的下载地址 2.下载: # wget http://apache.fayea.com/tomcat/tom ...
- [PWA] 12. Intro to IndexedDB
Use the library indexedDB-promised. Create a database and stroe: import idb from 'idb'; // Open(db_n ...
- (转)Vim用法小结
这是我转的一些vim基本用法,可能对初用者会有帮助,独乐乐不如众乐乐,是吧! Vim一般的Unix和Linux下均有安装. 三种状态 Command: 任何输入都会作为编辑命令,而不会出现在屏幕上 ...
- ubuntu 查看端口被占用并处理
当启动程序出现端口号被占用的情况,需要查看端口使用情况,使用netstat命令,下面是常用的几个查看端口情况的命令:查看所有的服务端口(ESTABLISHED netstat -a查看所有的服务端口, ...