Skip to content
This repository was archived by the owner on Jul 22, 2026. It is now read-only.

Commit a6acdde

Browse files
authored
Merge pull request #372 from experdb/develop
Modifying Backrest Backup and Recovery Features
2 parents 529a16f + 925f904 commit a6acdde

38 files changed

Lines changed: 1183 additions & 158 deletions

File tree

eXperDB-Management-Agent/src/main/java/com/k4m/dx/tcontrol/server/DxT049.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,26 @@ public void execute(String strDxExCode, JSONObject jObj) throws Exception {
5757
String hostUserCmd = "echo $HOSTUSER";
5858
String hostUser = util.getPidExec(hostUserCmd);
5959

60-
String backrestConfigPath = pghomePath + "/etc/pgbackrest/pgbackrest.conf";
61-
62-
BufferedReader br2 = new BufferedReader(new FileReader(backrestConfigPath));
63-
StringBuilder content = new StringBuilder();
64-
65-
String backerestConfigContent;
66-
while((backerestConfigContent = br2.readLine()) != null) {
67-
if(backerestConfigContent.contains("log-path")) {
68-
String logPath[] = backerestConfigContent.split("=");
69-
backerestConfigContent = backerestConfigContent.replaceAll("log-path="+logPath[1], "log-path=" + String.valueOf(jObj.get(ClientProtocolID.LOG_PATH)));
70-
}
71-
content.append(backerestConfigContent).append(System.lineSeparator());
72-
}
73-
74-
br2.close();
75-
76-
BufferedWriter bw2 = new BufferedWriter(new FileWriter(new File (backrestConfigPath)));
77-
78-
bw2.write(content.toString());
79-
bw2.close();
60+
// String backrestConfigPath = pghomePath + "/etc/pgbackrest/pgbackrest.conf";
61+
//
62+
// BufferedReader br2 = new BufferedReader(new FileReader(backrestConfigPath));
63+
// StringBuilder content = new StringBuilder();
64+
//
65+
// String backerestConfigContent;
66+
// while((backerestConfigContent = br2.readLine()) != null) {
67+
// if(backerestConfigContent.contains("log-path")) {
68+
// String logPath[] = backerestConfigContent.split("=");
69+
// backerestConfigContent = backerestConfigContent.replaceAll("log-path="+logPath[1], "log-path=" + String.valueOf(jObj.get(ClientProtocolID.LOG_PATH)));
70+
// }
71+
// content.append(backerestConfigContent).append(System.lineSeparator());
72+
// }
73+
//
74+
// br2.close();
75+
//
76+
// BufferedWriter bw2 = new BufferedWriter(new FileWriter(new File (backrestConfigPath)));
77+
//
78+
// bw2.write(content.toString());
79+
// bw2.close();
8080

8181
String configPath = pghomePath + "/etc/pgbackrest/default.conf";
8282
filePath = pghomePath + "/etc/pgbackrest/config/" + String.valueOf(jObj.get(ProtocolID.BCK_FILENM));

eXperDB-Management-Agent/src/main/java/com/k4m/dx/tcontrol/server/DxT053.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,20 @@ public void execute(String strDxExCode, JSONObject jObj) throws Exception {
5252
String pgBlogPathCmd = "echo $PGBLOG";
5353
String pgBlogPath = util.getPidExec(pgBlogPathCmd);
5454

55-
String restore_type = String.valueOf(jObj.get(ProtocolID.RESTORE_FLAG));
55+
String restoreType = String.valueOf(jObj.get(ProtocolID.RESTORE_FLAG));
5656
String exelog = String.valueOf(jObj.get(ProtocolID.EXELOG));
5757

58+
String restoreWrkName = String.valueOf(jObj.get(ProtocolID.WRK_NM));
59+
5860
vo.setEXELOG(exelog);
5961

6062
// 복구 타입에 따른 명령어
61-
if (restore_type.equals("full")) {
62-
restoreCmd = "pgbackrest --stanza=experdb --config-path=$PGHOME/etc/pgbackrest --delta restore > "
63+
if (restoreType.equals("full")) {
64+
restoreCmd = "pgbackrest --stanza=experdb --config=$PGHOME/etc/pgbackrest/config/" + restoreWrkName + ".conf --delta restore > "
6365
+ pgBlogPath + "/" + exelog + ".log";
6466
} else {
6567
String time_restore = String.valueOf(jObj.get(ProtocolID.TIME_RESTORE));
66-
restoreCmd = "pgbackrest --stanza=experdb --config-path=$PGHOME/etc/pgbackrest --type=time \"--target="
68+
restoreCmd = "pgbackrest --stanza=experdb --config=$PGHOME/etc/pgbackrest/config/" + restoreWrkName + ".conf --type=time \"--target="
6769
+ time_restore + "\" --target-action=promote --delta restore > " + pgBlogPath + "/" + exelog + ".log";
6870
}
6971

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package com.k4m.dx.tcontrol.server;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
5+
import java.io.BufferedReader;
6+
import java.io.BufferedWriter;
7+
import java.io.File;
8+
import java.io.FileReader;
9+
import java.io.FileWriter;
10+
import java.net.Socket;
11+
import java.util.ArrayList;
12+
import java.util.HashMap;
13+
import java.util.Iterator;
14+
15+
import org.json.simple.JSONObject;
16+
import org.json.simple.parser.JSONParser;
17+
import org.json.simple.parser.ParseException;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import com.k4m.dx.tcontrol.socket.ProtocolID;
22+
import com.k4m.dx.tcontrol.socket.SocketCtl;
23+
import com.k4m.dx.tcontrol.socket.TranCodeType;
24+
import com.k4m.dx.tcontrol.socket.client.ClientProtocolID;
25+
import com.k4m.dx.tcontrol.util.CommonUtil;
26+
import com.k4m.dx.tcontrol.util.FileUtil;
27+
28+
public class DxT055 extends SocketCtl{
29+
private Logger errLogger = LoggerFactory.getLogger("errorToFile");
30+
private Logger socketLogger = LoggerFactory.getLogger("socketLogger");
31+
32+
public DxT055(Socket socket, BufferedInputStream is, BufferedOutputStream os) {
33+
this.client = socket;
34+
this.is = is;
35+
this.os = os;
36+
}
37+
38+
@SuppressWarnings("unchecked")
39+
public void execute(String strDxExCode, JSONObject jObj) throws Exception {
40+
socketLogger.info("DxT055.execute : " + strDxExCode);
41+
byte[] sendBuff = null;
42+
String strErrCode = "";
43+
String strErrMsg = "";
44+
String strSuccessCode = "0";
45+
String filePath = "";
46+
47+
CommonUtil util = new CommonUtil();
48+
JSONObject outputObj = new JSONObject();
49+
50+
try {
51+
String pghomePathCmd = "echo $PGHOME";
52+
String pghomePath = util.getPidExec(pghomePathCmd);
53+
54+
String backrestLogPathCmd = "echo $PGBLOG";
55+
String backrestLogPath = util.getPidExec(backrestLogPathCmd);
56+
57+
String hostUserCmd = "echo $HOSTUSER";
58+
String hostUser = util.getPidExec(hostUserCmd);
59+
60+
String configPath = pghomePath + "/etc/pgbackrest/default.conf";
61+
filePath = pghomePath + "/etc/pgbackrest/config/" + String.valueOf(jObj.get(ProtocolID.WRK_NM) + ".conf");
62+
63+
BufferedReader br = new BufferedReader(new FileReader(new File(configPath)));
64+
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filePath)));
65+
66+
socketLogger.info("DxT055.execute Restore Type: " + String.valueOf(jObj.get(ClientProtocolID.STORAGE_OPT)));
67+
68+
String fileContent;
69+
while((fileContent = br.readLine()) != null) {
70+
fileContent = fileContent.replaceAll("#log-path=", "log-path=" + backrestLogPath);
71+
fileContent = fileContent.replaceAll("#log-level-console=detail", "log-level-console=detail");
72+
fileContent = fileContent.replaceAll("#log-level-file=detail", "log-level-file=detail");
73+
fileContent = fileContent.replaceAll("#pg1-path=", "pg1-path=" + String.valueOf(jObj.get(ClientProtocolID.PGDATA)));
74+
fileContent = fileContent.replaceAll("#pg1-port=", "pg1-port=" + String.valueOf(jObj.get(ClientProtocolID.DBMS_PORT)));
75+
fileContent = fileContent.replaceAll("#pg1-user=", "pg1-user=" + String.valueOf(jObj.get(ClientProtocolID.SPR_USR_ID)));
76+
77+
78+
79+
if(String.valueOf(jObj.get(ClientProtocolID.STORAGE_OPT)).equals("cloud")) {
80+
ArrayList<String> cloudKeyList = new ArrayList<String>();
81+
ArrayList<String> cloudValueList = new ArrayList<String>();
82+
83+
if(jObj.get(ClientProtocolID.CLOUD_MAP) == null || jObj.get(ClientProtocolID.CLOUD_MAP).equals("")) {
84+
}else {
85+
try {
86+
JSONParser parser = new JSONParser();
87+
JSONObject jsonObject = (JSONObject) parser.parse(String.valueOf(jObj.get(ClientProtocolID.CLOUD_MAP)));
88+
Iterator<String> cloudKeys = jsonObject.keySet().iterator();
89+
90+
while(cloudKeys.hasNext()){
91+
String key = cloudKeys.next().toString();
92+
cloudKeyList.add(key);
93+
cloudValueList.add(String.valueOf(jsonObject.get(key)));
94+
}
95+
96+
} catch (ParseException e1) {
97+
errLogger.info("DxT055 {} : JSON Parser Error (Cloud Opt)");
98+
}
99+
}
100+
101+
if(cloudKeyList.size() != 0) {
102+
for(int i=0; i < cloudKeyList.size(); i++) {
103+
socketLogger.info("DxT055.execute : cloudKeyList " + cloudKeyList.get(i) );
104+
if(cloudKeyList.get(i).equals("s3_bucket")) {
105+
fileContent = fileContent.replaceAll("#repo1-s3-bucket=", "repo1-s3-bucket=" + cloudValueList.get(i));
106+
}else if(cloudKeyList.get(i).equals("s3_region")) {
107+
fileContent = fileContent.replaceAll("#repo1-s3-region=", "repo1-s3-region=" + cloudValueList.get(i));
108+
}else if(cloudKeyList.get(i).equals("s3_key")) {
109+
fileContent = fileContent.replaceAll("#repo1-s3-key=", "repo1-s3-key=" + cloudValueList.get(i));
110+
}else if(cloudKeyList.get(i).equals("s3_endpoint")) {
111+
fileContent = fileContent.replaceAll("#repo1-s3-endpoint=", "repo1-s3-endpoint=" + cloudValueList.get(i));
112+
}else if(cloudKeyList.get(i).equals("s3_path")) {
113+
fileContent = fileContent.replaceAll("#repo1-path=", "repo1-path=" + cloudValueList.get(i));
114+
}else if(cloudKeyList.get(i).equals("s3_key-secret")) {
115+
fileContent = fileContent.replaceAll("#repo1-s3-key-secret=", "repo1-s3-key-secret=" + cloudValueList.get(i));
116+
}else if(cloudKeyList.get(i).equals("cloud_type")) {
117+
fileContent = fileContent.replaceAll("#repo1-type=", "repo1-type=s3");
118+
}
119+
}
120+
}
121+
}else if(String.valueOf(jObj.get(ClientProtocolID.STORAGE_OPT)).toLowerCase().equals("remote")){
122+
fileContent = fileContent.replaceAll("#repo1-host=", "repo1-host=" + String.valueOf(jObj.get(ClientProtocolID.REMOTE_IP)));
123+
fileContent = fileContent.replaceAll("#repo1-host-port=", "repo1-host-port=" + String.valueOf(jObj.get(ClientProtocolID.REMOTE_PORT)));
124+
fileContent = fileContent.replaceAll("#repo1-host-user=", "repo1-host-user=" + String.valueOf(jObj.get(ClientProtocolID.REMOTE_USR)));
125+
}else if(String.valueOf(jObj.get(ClientProtocolID.STORAGE_OPT)).equals("localRemote")){
126+
fileContent = fileContent.replaceAll("#repo1-host-user=", "repo1-host-user=" + String.valueOf(jObj.get(ClientProtocolID.HOSTUSER)));
127+
fileContent = fileContent.replaceAll("#repo1-host-port=", "repo1-host-port=" + String.valueOf(jObj.get(ClientProtocolID.SSH_PORT)));
128+
fileContent = fileContent.replaceAll("#repo1-host=", "repo1-host=" + String.valueOf(jObj.get(ClientProtocolID.DBMS_IP)));
129+
}else if(String.valueOf(jObj.get(ClientProtocolID.STORAGE_OPT)).equals("HAlocal")){
130+
fileContent = fileContent.replaceAll("#repo1-host-user=", "repo1-host-user=" + hostUser);
131+
fileContent = fileContent.replaceAll("#repo1-host=", "repo1-host=" + String.valueOf(jObj.get(ClientProtocolID.DBMS_IP)));
132+
}
133+
134+
fileContent = fileContent.replaceAll("#repo1-path=", "repo1-path=" + String.valueOf(jObj.get(ClientProtocolID.BCK_FILE_PTH)));
135+
136+
bw.write(fileContent + "\r\n");
137+
bw.flush();
138+
}
139+
140+
bw.close();
141+
br.close();
142+
143+
boolean blnIsFile = FileUtil.isFile(filePath);
144+
145+
if(blnIsFile) {
146+
outputObj.put(ProtocolID.DX_EX_CODE, strDxExCode);
147+
outputObj.put(ProtocolID.RESULT_CODE, strSuccessCode);
148+
outputObj.put(ProtocolID.ERR_CODE, strErrCode);
149+
outputObj.put(ProtocolID.ERR_MSG, strErrMsg);
150+
151+
sendBuff = outputObj.toString().getBytes();
152+
send(4, sendBuff);
153+
}else {
154+
errLogger.info("DxT055 {} : file no create");
155+
}
156+
}catch (Exception e) {
157+
errLogger.info("DxT055 {} : " + e.toString());
158+
159+
outputObj.put(ProtocolID.DX_EX_CODE, TranCodeType.DxT055);
160+
outputObj.put(ProtocolID.RESULT_CODE, "1");
161+
outputObj.put(ProtocolID.ERR_CODE, TranCodeType.DxT055);
162+
outputObj.put(ProtocolID.ERR_MSG, "DxT055 Error [" + e.toString() + "]");
163+
HashMap hp = new HashMap();
164+
outputObj.put(ProtocolID.RESULT_DATA, hp);
165+
166+
sendBuff = outputObj.toString().getBytes();
167+
send(4, sendBuff);
168+
} finally {
169+
outputObj = null;
170+
sendBuff = null;
171+
}
172+
}
173+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.k4m.dx.tcontrol.server;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
5+
import java.net.Socket;
6+
import java.util.HashMap;
7+
8+
import org.json.simple.JSONObject;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import com.k4m.dx.tcontrol.socket.ProtocolID;
13+
import com.k4m.dx.tcontrol.socket.SocketCtl;
14+
import com.k4m.dx.tcontrol.socket.TranCodeType;
15+
import com.k4m.dx.tcontrol.util.CommonUtil;
16+
17+
public class DxT056 extends SocketCtl{
18+
private Logger errLogger = LoggerFactory.getLogger("errorToFile");
19+
private Logger socketLogger = LoggerFactory.getLogger("socketLogger");
20+
21+
public DxT056(Socket socket, BufferedInputStream is, BufferedOutputStream os) {
22+
this.client = socket;
23+
this.is = is;
24+
this.os = os;
25+
}
26+
27+
@SuppressWarnings("unchecked")
28+
public void execute(String strDxExCode, JSONObject jObj) throws Exception {
29+
socketLogger.info("DxT056.execute : " + strDxExCode);
30+
byte[] sendBuff = null;
31+
String strErrCode = "";
32+
String strErrMsg = "";
33+
String strSuccessCode = "0";
34+
35+
CommonUtil util = new CommonUtil();
36+
JSONObject outputObj = new JSONObject();
37+
38+
try {
39+
String hostuserCmd = "echo $HOSTUSER";
40+
String hostuser = util.getPidExec(hostuserCmd);
41+
42+
outputObj.put(ProtocolID.DX_EX_CODE, strDxExCode);
43+
outputObj.put(ProtocolID.RESULT_CODE, strSuccessCode);
44+
outputObj.put(ProtocolID.ERR_CODE, strErrCode);
45+
outputObj.put(ProtocolID.ERR_MSG, strErrMsg);
46+
outputObj.put(ProtocolID.RESULT_DATA, hostuser);
47+
48+
sendBuff = outputObj.toString().getBytes();
49+
send(4, sendBuff);
50+
51+
}catch(Exception e){
52+
errLogger.info("DxT056 {} : " + e.toString());
53+
54+
outputObj.put(ProtocolID.DX_EX_CODE, TranCodeType.DxT056);
55+
outputObj.put(ProtocolID.RESULT_CODE, "1");
56+
outputObj.put(ProtocolID.ERR_CODE, TranCodeType.DxT056);
57+
outputObj.put(ProtocolID.ERR_MSG, "DxT056 Error [" + e.toString() + "]");
58+
HashMap hp = new HashMap();
59+
outputObj.put(ProtocolID.RESULT_DATA, hp);
60+
61+
sendBuff = outputObj.toString().getBytes();
62+
send(4, sendBuff);
63+
}finally {
64+
outputObj = null;
65+
sendBuff = null;
66+
}
67+
68+
}
69+
}

eXperDB-Management-Agent/src/main/java/com/k4m/dx/tcontrol/socket/TranCodeType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public class TranCodeType {
6969
public static final String DxT052 = "DxT052";
7070
public static final String DxT053 = "DxT053";
7171
public static final String DxT054 = "DxT054";
72+
public static final String DxT055 = "DxT055";
73+
public static final String DxT056 = "DxT056";
7274

7375
//proxy 추가
7476
public static final String PsP001 = "PsP001"; // proxy 에이전트 setting

eXperDB-Management-Agent/src/main/java/com/k4m/dx/tcontrol/socket/client/ClientAdapter.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,4 +681,22 @@ public JSONObject dxT054(JSONObject jObj) throws Exception{
681681

682682
return parseToJsonObj(recvBuff);
683683
}
684+
685+
public JSONObject dxT055(JSONObject jObj) throws Exception{
686+
687+
byte[] bt = jObj.toString().getBytes();
688+
cc.send(4, bt);
689+
byte[] recvBuff = cc.recv(4, false);
690+
691+
return parseToJsonObj(recvBuff);
692+
}
693+
694+
public JSONObject dxT056(JSONObject jObj) throws Exception{
695+
696+
byte[] bt = jObj.toString().getBytes();
697+
cc.send(4, bt);
698+
byte[] recvBuff = cc.recv(4, false);
699+
700+
return parseToJsonObj(recvBuff);
701+
}
684702
}

eXperDB-Management-Agent/src/main/java/com/k4m/dx/tcontrol/socket/client/ClientProtocolID.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,10 @@ public class ClientProtocolID {
282282
public static final String TARGET_PGDATA= "TARGET_PGDATA";
283283
public static final String TARGET_USER= "TARGET_USER";
284284
public static final String TARGET_PORT= "TARGET_PORT";
285+
public static final String REMOTE_IP= "REMOTE_IP";
286+
public static final String REMOTE_PORT= "REMOTE_PORT";
287+
public static final String REMOTE_USR= "REMOTE_USR";
288+
public static final String HOSTUSER= "HOSTUSER";
289+
public static final String SSH_PORT= "SSH_PORT";
290+
285291
}

eXperDB-Management-Agent/src/main/java/com/k4m/dx/tcontrol/socket/client/ClientTranCodeType.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public class ClientTranCodeType {
6969
public static final String DxT051 = "DxT051";
7070
public static final String DxT052 = "DxT052";
7171
public static final String DxT053 = "DxT053";
72+
public static final String DxT054 = "DxT054";
73+
public static final String DxT055 = "DxT055";
74+
public static final String DxT056 = "DxT056";
7275

7376
public static final String STATUS = "STATUS";
7477
public static final String STOP = "STOP";

eXperDB-Management-Agent/src/main/java/com/k4m/dx/tcontrol/socket/listener/DXTcontrolSocketExecute.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
import com.k4m.dx.tcontrol.server.DxT052;
7777
import com.k4m.dx.tcontrol.server.DxT053;
7878
import com.k4m.dx.tcontrol.server.DxT054;
79+
import com.k4m.dx.tcontrol.server.DxT055;
80+
import com.k4m.dx.tcontrol.server.DxT056;
7981
import com.k4m.dx.tcontrol.socket.ProtocolID;
8082
import com.k4m.dx.tcontrol.socket.SocketCtl;
8183
import com.k4m.dx.tcontrol.socket.TranCodeType;
@@ -411,6 +413,16 @@ public void run() {
411413
DxT054 dxT054 = new DxT054(client, is, os);
412414
dxT054.execute(strDX_EX_CODE, jObj);
413415
break;
416+
417+
case TranCodeType.DxT055 :
418+
DxT055 dxT055 = new DxT055(client, is, os);
419+
dxT055.execute(strDX_EX_CODE, jObj);
420+
break;
421+
422+
case TranCodeType.DxT056 :
423+
DxT056 dxT056 = new DxT056(client, is, os);
424+
dxT056.execute(strDX_EX_CODE, jObj);
425+
break;
414426

415427

416428
}

0 commit comments

Comments
 (0)