后台进程(守护进程)自动备份PostgreSQL数据库

从当前目录中读取ini配置文件的登录数据库必要的参数,登录数据库后获取两次备份的间隔天数,然后启动一个线程隔1分钟检查一下是否需要备份。

之前查资料查了好久,才找到

"pg_dump \"host=%s port=%d user=%s password=%s dbname=%s\" > db_bak\\%s.bak"

这种可以避免输入密码,实现自动备份(不用配置环境变量或改动数据库的登录权限)

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include "inc/libpq-fe.h"
#include <time.h>
 
#pragma comment(linker, "/subsystem:windows /entry:mainCRTStartup")
#pragma comment(lib, "lib/libpq.lib")
 
#define DIRECTORY "db_bak"
#define INIFILE "sysinfo.ini"
#define CONNINFO "host=localhost port=5432 user=postgres password=123456 dbname=DB_SPS"
#define QUERYSQL "select sys_val from tb_sysinfo where sys_key=\'backup_interval_time\'"
#define SPACEUNIT (60*60*24)

// BEGTIME:00 ~ ENDTIME:59
#define BEGTIME 2
#define ENDTIME 3

typedef struct tagArgs
{
	int t_space;
	char db_ip[16];
	char db_name[10];
	char db_user[10];
	char db_passwd[10];
	unsigned short db_port;
}Args;

unsigned int __stdcall ThreadCheckAndSave(void* arglist)
{
	struct tm tlasttime = {0}, tcurtime = {0};
	time_t lasttime = 0, curtime = 0;
	Args args = {0};
	char cmd[256] = {0}, strtime[256] = {0};

	if(arglist == NULL)
	{
		printf("error for begin and space args !\n");
		_endthreadex(1);
	}
	memcpy(&args,arglist,sizeof(args));

	lasttime = time(NULL);
	do{
		curtime = time(NULL);
		if((difftime(curtime,lasttime)/SPACEUNIT) > args.t_space)
		{
			localtime_s(&tcurtime,&curtime);
			if(tcurtime.tm_hour >= BEGTIME && tcurtime.tm_hour <= ENDTIME)
			{
				//备份
				memset(cmd,0,sizeof(cmd));
				memset(strtime,0,sizeof(strtime));
				localtime_s(&tcurtime, &curtime);
				strftime(strtime,sizeof(strtime),"%Y-%m-%d(%H_%M_%S)",&tcurtime);
				CreateDirectoryA(DIRECTORY,NULL);
				sprintf(cmd,"pg_dump \"host=%s port=%d user=%s password=%s dbname=%s\" > db_bak\\%s.bak",
										args.db_ip,
										args.db_port,
										args.db_user,
										args.db_passwd,
										args.db_name,
										strtime);
				system(cmd);
				lasttime = curtime;
			}
			Sleep(60000);
		}
	}while(true);

	_endthreadex(0);
	return 0;
}

int main(int argc, const char* argv[])
{
	PGconn* conn = NULL;
	PGresult* result = NULL;
	char* getstr = NULL;
	
	Args args = {0};
	HANDLE hthread = NULL;
	char inipath[MAX_PATH] = {0};
	char strsql[MAX_PATH] = {0};

	GetCurrentDirectoryA(sizeof(inipath),inipath);
	strcat(inipath,"\\");
	strcat(inipath,INIFILE);
	GetPrivateProfileStringA("db","#ip","127.0.0.1",args.db_ip,sizeof(args.db_ip),inipath);
	GetPrivateProfileStringA("db","#name","",args.db_name,sizeof(args.db_name),inipath);
	GetPrivateProfileStringA("db","#user","",args.db_user,sizeof(args.db_user),inipath);
	GetPrivateProfileStringA("db","#passwd","",args.db_passwd,sizeof(args.db_passwd),inipath);
	args.db_port = GetPrivateProfileIntA("db","#port",0,inipath);
	sprintf(strsql,"host=%s port=%d user=%s password=%s dbname=%s",args.db_ip,args.db_port,args.db_user,args.db_passwd,args.db_name);

	conn = PQconnectdb(strsql);
	if(PQstatus(conn) != CONNECTION_OK)
		goto END;
	result = PQexec(conn,QUERYSQL);
	if(PQresultStatus(result) != PGRES_TUPLES_OK)
		goto END;
	//获取配置参数
	getstr = PQgetvalue(result,0,0);
	if(getstr == NULL)
		goto END;
	args.t_space = atoi(getstr);

	//循环检查备份数据库
	hthread = (HANDLE)_beginthreadex(NULL,0,ThreadCheckAndSave,&args,CREATE_SUSPENDED,NULL);
	SetPriorityClass(GetCurrentProcess(),BELOW_NORMAL_PRIORITY_CLASS);
	//SetThreadPriority(hthread,THREAD_PRIORITY_IDLE);
	ResumeThread(hthread);

END:
	if(result != NULL)
	{
		printf("PQexec : %s\n", PQerrorMessage(conn));
		PQclear(result);
		result = NULL;
	}
	if(conn != NULL)
	{
		printf("PQconn : %s\n", PQerrorMessage(conn));
		PQfinish(conn);
		conn = NULL;
	}
	if(hthread != NULL)
		WaitForSingleObject(hthread,INFINITE);
	else
		return -1;

	return 0;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏张善友的专栏

Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2527
来自专栏一个会写诗的程序员的博客

Spring Reactor 项目核心库Reactor Core

Non-Blocking Reactive Streams Foundation for the JVM both implementing a Reactiv...

2112
来自专栏陈仁松博客

ASP.NET Core 'Microsoft.Win32.Registry' 错误修复

今天在发布Asp.net Core应用到Azure的时候出现错误InvalidOperationException: Cannot find compilati...

4808
来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2506
来自专栏大内老A

The .NET of Tomorrow

Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciti...

30810
来自专栏杨龙飞前端

scrollto 到指定位置

2494
来自专栏魂祭心

原 canvas绘制clock

4024
来自专栏C#

DotNet加密方式解析--非对称加密

    新年新气象,也希望新年可以挣大钱。不管今年年底会不会跟去年一样,满怀抱负却又壮志未酬。(不过没事,我已为各位卜上一卦,卦象显示各位都能挣钱...)...

4808
来自专栏闻道于事

js登录滑动验证,不滑动无法登陆

js的判断这里是根据滑块的位置进行判断,应该是用一个flag判断 <%@ page language="java" contentType="text/html...

6638
来自专栏ASP.NETCore

ASP.NET Core 整合Autofac和Castle实现自动AOP拦截

除了ASP.NETCore自带的IOC容器外,我们还可以使用其他成熟的DI框架,如Autofac,StructureMap等(笔者只用过Unity,Ninjec...

674

扫码关注云+社区