-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb-tool.cpp
370 lines (274 loc) · 8.58 KB
/
db-tool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Stream ve string kutuphaneleri
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
// Veritabani islemleri icin kutuphane
#include <pqxx/pqxx>
// Boost kutuphaneleri
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/filesystem.hpp>
#include <boost/tokenizer.hpp>
// Standart kutuphane
#include <stdlib.h>
// Islem yapilacak veritabanlarinin bilgisini tutacak sinif
#include "Database_Info.hpp"
using namespace std;
using namespace pqxx;
using namespace boost::property_tree;
void parse_json(const char*);
void backup(string, connection*);
void write(result&, string);
void add_to_archive(char*);
void restore(string, connection*);
void create_table(string, vector<string>, vector<string>, connection*);
void insert_row(string, vector<string>, vector<string>, connection*);
unique_ptr<connection> connect(Database_Info&);
void disconnect(connection*);
// Veritabani bilgilerinin bulundugu nesneleri tutan vektor
vector<Database_Info> databases;
int main(int argc, char* argv[]){
// arguman sayisi 3 ve islem argumani backup ise yedekleme
// arguman sayisi 4 ve islem argumani restore ise restore islemi yapilir
if(argc == 3 && string(argv[1]) == "-backup"){
// Yedeklenecek tablolarin bulunacagi dizini olustur
boost::filesystem::path p{".//backups"};
boost::filesystem::create_directory(p);
// Yedekleneme yapilacak veritabani bilgilerini JSON'dan parse et
parse_json(argv[2]);
// Tablolari yedekle
for(auto& database : databases){
unique_ptr<connection> con = connect(database);
for(auto& table: database.tables){
backup(table, con.get());
}
disconnect(con.get());
}
// Yedeklenen dosyalari arsive ekle
add_to_archive(argv[2]);
}else if(argc == 3 && string(argv[1]) == "-restore"){
stringstream command;
command << "tar -xzf " << argv[2];
// Archive dosyasini cikart
system(command.str().c_str());
// Restore edilecek veritabani bilgilerini JSON'dan parse et
parse_json(string(".//backups//example.json").c_str());
// Tablolari restore et
for(auto& database : databases){
unique_ptr<connection> con = connect(database);
for(auto& table: database.tables){
restore(table, con.get());
}
disconnect(con.get());
}
}else{
cout << "Usage: <operation(backup/restore)> <json-path/archive-path>" << endl;
return 1;
}
return 0;
}
// JSON da belirtilen veritabanları ve tabloları cek
void parse_json(const char* path){
// Koku olustur ve JSON dosyasini oku
ptree root;
read_json(path, root);
// JSON ı parse et
for(auto& database : root){
string dbname = database.second.get<string>("dbname");
string user = database.second.get<string>("user");
string password = database.second.get<string>("password");
string hostaddr = database.second.get<string>("hostaddr");
string port = database.second.get<string>("port");
Database_Info info(dbname, user, password, hostaddr, port);
// Yedeklenecek tablolarin bilgisini vektore ekle
for(auto& table : database.second.get_child("tables")){
info.add_table(table.second.get_value<string>());
}
databases.push_back(info);
}
}
// Veritabani baglantisini kur
unique_ptr<connection> connect(Database_Info &database){
stringstream connection_string;
// Baglanti stringini olustur
connection_string << "dbname=" << database.dbname << " user=" << database.user << " password=" << database.password << " hostaddr=" << database.hostaddr << " port=" << database.port;
// Veritabanina baglantiyi kur
unique_ptr<connection> con(new connection(connection_string.str()));
// Veritabanina baglanti kurulamadiysa sonlandir
if(!(con->is_open())){
cerr << "Connection didn't established" << endl;
exit(1);
}
return move(con);
}
// Veritabani baglantisini sonlandir
void disconnect(connection* con){
con->disconnect();
}
// Arguman olarak alinan veritabani ve tablolarini yedekle
void backup(string table, connection* con){
try{
// Sorguyu olustur
stringstream temp;
temp << "SELECT * FROM " << table << ";";
string query = temp.str();
temp.str("");
// Nontransaction nesnesini olustur
nontransaction ntransact(*con);
// Sorguyu icra et
result res(ntransact.exec(query));
// Dosyaya yaz
write(res, table);
}catch(const exception &e){
cerr << e.what() << endl;
exit(1);
}
}
// Tablolari dosyaya yaz
void write(result &res, string table){
try{
stringstream path;
path << "backups/" << table;
ofstream write;
write.open(path.str());
// Sutun tiplerini dosyaya ekle
for(int i = 0; i < res.columns(); i++){
write << res.column_type(i) << "\t";
}
write << "\n";
// Sutun basliklarini dosyaya ekle
for(int i = 0; i < res.columns(); i++){
write << res.column_name(i) << "\t";
}
write << "\n";
for(int iter = 0; iter != res.size(); iter++){
// Satir verilerini dosyaya ekle
for(int i = 0; i < res.columns(); i++){
write << res[iter][i] << "\t";
}
write << "\n";
}
// Dosyayi kapat
write.close();
}catch(const exception &e){
cerr << e.what() << endl;
exit(1);
}
}
// Yedeklenen tablolari arsivle
void add_to_archive(char* path){
// JSON dosyasini dizine tasi
stringstream command;
command << "cp " << path << " .//backups//" << path;
system(command.str().c_str());
// Dizindeki dosyalari tar a ekle
string com1("tar -czf backups.tar.gz .//backups");
system(com1.c_str());
// Dizini sil
string com2("rm -rf .//backups");
system(com2.c_str());
}
// Parse edilen tablolari restore et
void restore(string table, connection* con){
string line;
vector<string> oids;
vector<string> column_headers;
boost::char_separator<char> separator{"\t"};
typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
// Dosyayi oku ve verileri al
stringstream path;
path << ".//backups//" << table;
ifstream read(path.str());
if(read.is_open()){
// oid leri dosyadan oku
getline(read, line);
// Her bir sutun icin oid degerlerini tokenize et
// oid degerleri restore isleminde sutun tipini belirlemek icin kullaniliyor
tokenizer tokenize_oids{line, separator};
for(tokenizer::iterator iter = tokenize_oids.begin(); iter != tokenize_oids.end(); iter++){
oids.push_back(*iter);
}
// Sutun basliklarini dosyadan oku
getline(read, line);
// Her bir sutun icin sutun basliklarini tokenize et
tokenizer tokenize_column_headers{line, separator};
for(tokenizer::iterator iter = tokenize_column_headers.begin();
iter != tokenize_column_headers.end(); iter++){
column_headers.push_back(*iter);
}
// oid ve sutun baslik bilgileri alinan tabloyu olustur
create_table(table, oids, column_headers, con);
// Satirlari tabloya ekle
while(getline(read, line)){
vector<string> row;
tokenizer tokenize_row{line, separator};
for(tokenizer::iterator iter = tokenize_row.begin();
iter != tokenize_row.end();iter++){
row.push_back(*iter);
}
insert_row(table, column_headers, row, con);
}
read.close();
}
}
void create_table(string table, vector<string> oids,
vector<string> column_headers, connection* con){
try{
stringstream query;
query << "CREATE TABLE " << table << " (";
for(int i = 0; i < oids.size(); i++){
// Sutun tipini ogrenmek icin sorguyu olustur
stringstream temp;
temp << "SELECT typname FROM pg_type where oid=" << oids.at(i) << ";";
// non-transaction nesnesini olustur
work ntransact(*con);
// Tipi al
result res(ntransact.exec(temp.str()));
string type = res.begin()[0].as<string>();
query << column_headers.at(i) << " " << type;
// Sorgunun sonu degilse virgul, sorgu sonu ise noktali virgul koy
if(i != (oids.size()- 1)){
query << ", ";
}else{
query << ");";
}
}
work transact(*con);
transact.exec(query);
transact.commit();
}catch(const exception &e){
cerr << e.what();
exit(1);
}
}
void insert_row(string table, vector<string> column_headers,
vector<string> row, connection* con){
try{
stringstream query;
query << "INSERT INTO " << table << " (";
for(int i = 0; i < column_headers.size(); i++){
query << column_headers.at(i);
if(i != column_headers.size() - 1){
query << ",";
}else{
query << ") ";
}
}
query << "VALUES (";
for(int i = 0; i < row.size(); i++){
query << "'" << row.at(i) << "'";
if(i != row.size() - 1){
query << ", ";
}else{
query << ");";
}
}
work transact(*con);
transact.exec(query);
transact.commit();
}catch(const exception &e){
cerr << e.what();
exit(1);
}
}