This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
frmBackup.cpp
359 lines (287 loc) · 10.6 KB
/
frmBackup.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
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
// RCS-ID: $Id$
// Copyright (C) 2002 - 2009, The pgAdmin Development Team
// This software is released under the Artistic Licence
//
// frmBackup.cpp - Backup database dialogue
//
//////////////////////////////////////////////////////////////////////////
// wxWindows headers
#include <wx/wx.h>
#include <wx/settings.h>
// App headers
#include "pgAdmin3.h"
#include "frm/frmMain.h"
#include "frm/frmBackup.h"
#include "utils/sysLogger.h"
#include "schema/pgSchema.h"
#include "schema/pgTable.h"
// Icons
#include "images/backup.xpm"
#define nbNotebook CTRL_NOTEBOOK("nbNotebook")
#define txtFilename CTRL_TEXT("txtFilename")
#define btnFilename CTRL_BUTTON("btnFilename")
#define rbxFormat CTRL_RADIOBOX("rbxFormat")
#define chkBlobs CTRL_CHECKBOX("chkBlobs")
#define chkOid CTRL_CHECKBOX("chkOid")
#define chkInsert CTRL_CHECKBOX("chkInsert")
#define chkDisableDollar CTRL_CHECKBOX("chkDisableDollar")
#define sbxPlainOptions CTRL_STATICBOX("sbxPlainOptions")
#define chkOnlyData CTRL_CHECKBOX("chkOnlyData")
#define chkOnlySchema CTRL_CHECKBOX("chkOnlySchema")
#define chkNoOwner CTRL_CHECKBOX("chkNoOwner")
#define chkCreateDb CTRL_CHECKBOX("chkCreateDb")
#define chkDropDb CTRL_CHECKBOX("chkDropDb")
#define chkDisableTrigger CTRL_CHECKBOX("chkDisableTrigger")
#define chkVerbose CTRL_CHECKBOX("chkVerbose")
BEGIN_EVENT_TABLE(frmBackup, ExternProcessDialog)
EVT_TEXT(XRCID("txtFilename"), frmBackup::OnChange)
EVT_BUTTON(XRCID("btnFilename"), frmBackup::OnSelectFilename)
EVT_BUTTON(wxID_OK, frmBackup::OnOK)
EVT_RADIOBOX(XRCID("rbxFormat"), frmBackup::OnChangePlain)
EVT_CHECKBOX(XRCID("chkOnlyData"), frmBackup::OnChangePlain)
EVT_CHECKBOX(XRCID("chkOnlySchema"), frmBackup::OnChangePlain)
EVT_CHECKBOX(XRCID("chkNoOwner"), frmBackup::OnChangePlain)
EVT_CLOSE( ExternProcessDialog::OnClose)
END_EVENT_TABLE()
frmBackup::frmBackup(frmMain *form, pgObject *obj) : ExternProcessDialog(form)
{
object=obj;
wxWindowBase::SetFont(settings->GetSystemFont());
LoadResource(form, wxT("frmBackup"));
RestorePosition();
SetTitle(wxString::Format(_("Backup %s %s"), object->GetTranslatedTypeName().c_str(), object->GetFullIdentifier().c_str()));
canBlob = (obj->GetMetaType() == PGM_DATABASE);
chkBlobs->SetValue(canBlob);
chkDisableDollar->Enable(obj->GetConnection()->BackendMinimumVersion(7, 5));
wxString val;
settings->Read(wxT("frmBackup/LastFile"), &val, wxEmptyString);
txtFilename->SetValue(val);
if (!object->GetDatabase()->GetServer()->GetPasswordIsStored())
environment.Add(wxT("PGPASSWORD=") + object->GetServer()->GetPassword());
// Pass the SSL mode via the environment
environment.Add(wxT("PGSSLMODE=") + object->GetServer()->GetConnection()->GetSslModeName());
environment.Add(wxT("PGSSLVERIFY=") + object->GetServer()->GetConnection()->GetSslVerifyModeName());
// Icon
SetIcon(wxIcon(backup_xpm));
// fix translation problem
wxString dollarLabel=wxGetTranslation(_("Disable $$ quoting"));
dollarLabel.Replace(wxT("$$"), wxT("$"));
chkDisableDollar->SetLabel(dollarLabel);
chkDisableDollar->SetSize(chkDisableDollar->GetBestSize());
txtMessages = CTRL_TEXT("txtMessages");
txtMessages->SetMaxLength(0L);
btnOK->Disable();
wxCommandEvent ev;
OnChangePlain(ev);
}
frmBackup::~frmBackup()
{
SavePosition();
}
wxString frmBackup::GetHelpPage() const
{
wxString page;
page = wxT("pg/app-pgdump");
return page;
}
void frmBackup::OnSelectFilename(wxCommandEvent &ev)
{
wxString title, prompt, FilenameOnly;
if (rbxFormat->GetSelection() == 2) // plain
{
title = _("Select output file");
prompt = _("Query files (*.sql)|*.sql|All files (*.*)|*.*");
}
else
{
title = _("Select backup filename");
prompt = _("Backup files (*.backup)|*.backup|All files (*.*)|*.*");
}
wxFileName::SplitPath(txtFilename->GetValue(), NULL, NULL, &FilenameOnly, NULL);
wxFileDialog file(this, title, ::wxPathOnly(txtFilename->GetValue()), FilenameOnly, prompt, wxFD_SAVE);
if (file.ShowModal() == wxID_OK)
{
txtFilename->SetValue(file.GetPath());
OnChange(ev);
}
}
void frmBackup::OnChange(wxCommandEvent &ev)
{
if (!process && !done)
btnOK->Enable(!txtFilename->GetValue().IsEmpty());
}
void frmBackup::OnChangePlain(wxCommandEvent &ev)
{
bool isPlain = (rbxFormat->GetSelection() == 2);
sbxPlainOptions->Enable(isPlain);
chkBlobs->Enable(canBlob && !isPlain);
chkOnlyData->Enable(isPlain && !chkOnlySchema->GetValue());
if (isPlain)
isPlain = !chkOnlyData->GetValue();
chkOnlySchema->Enable(isPlain);
chkNoOwner->Enable(isPlain);
chkDropDb->Enable(isPlain);
chkCreateDb->Enable(isPlain);
chkDisableTrigger->Enable(chkOnlyData->GetValue());
wxCommandEvent nullEvent;
OnChange(nullEvent);
}
wxString frmBackup::GetCmd(int step)
{
wxString cmd = getCmdPart1();
return cmd + getCmdPart2();
}
wxString frmBackup::GetDisplayCmd(int step)
{
wxString cmd = getCmdPart1();
return cmd + getCmdPart2();
}
wxString frmBackup::getCmdPart1()
{
pgServer *server=object->GetDatabase()->GetServer();
wxString cmd;
if (object->GetConnection()->EdbMinimumVersion(8,0))
cmd=edbBackupExecutable;
else if (object->GetConnection()->GetIsGreenplum())
cmd=gpBackupExecutable;
else
cmd=pgBackupExecutable;
if (!server->GetName().IsEmpty())
cmd += wxT(" --host=") + server->GetName();
cmd += wxT(" --port=") + NumToStr((long)server->GetPort())
+ wxT(" --username=") + server->GetUsername();
if (object->GetConnection()->GetIsGreenplum())
cmd += wxT(" --gp-syntax ");
return cmd;
}
wxString frmBackup::getCmdPart2()
{
wxString backupExecutable;
if (object->GetConnection()->EdbMinimumVersion(8,0))
backupExecutable=edbBackupExecutable;
else if (object->GetConnection()->GetIsGreenplum())
backupExecutable=gpBackupExecutable;
else
backupExecutable=pgBackupExecutable;
wxString cmd;
switch (rbxFormat->GetSelection())
{
case 0: // compressed
{
cmd.Append(wxT(" --format=custom"));
if (chkBlobs->GetValue())
cmd.Append(wxT(" --blobs"));
break;
}
case 1: // tar
{
cmd.Append(wxT(" --format=tar"));
if (chkBlobs->GetValue())
cmd.Append(wxT(" --blobs"));
break;
}
case 2:
{
cmd.Append(wxT(" --format=plain"));
if (chkOnlyData->GetValue())
{
cmd.Append(wxT(" --data-only"));
if (chkDisableTrigger->GetValue())
cmd.Append(wxT(" --disable-triggers"));
}
else
{
if (chkOnlySchema->GetValue())
cmd.Append(wxT(" --schema-only"));
if (chkNoOwner->GetValue())
cmd.Append(wxT(" --no-owner"));
if (chkCreateDb->GetValue())
cmd.Append(wxT(" --create"));
if (chkDropDb->GetValue())
cmd.Append(wxT(" --clean"));
}
break;
}
}
if (chkOid->GetValue())
cmd.Append(wxT(" --oids"));
if (chkInsert->GetValue())
cmd.Append(wxT(" --column-inserts"));
if (chkDisableDollar->GetValue())
cmd.Append(wxT(" --disable-dollar-quoting"));
if (settings->GetIgnoreVersion())
cmd.Append(wxT(" --ignore-version"));
if (chkVerbose->GetValue())
cmd.Append(wxT(" --verbose"));
cmd.Append(wxT(" --file=\"") + txtFilename->GetValue() + wxT("\""));
if (object->GetMetaType() == PGM_SCHEMA)
#ifdef WIN32
cmd.Append(wxT(" --schema=\\\"") + ((pgSchema*)object)->GetIdentifier() + wxT("\\\""));
#else
cmd.Append(wxT(" --schema='") + ((pgSchema*)object)->GetQuotedIdentifier() + wxT("'"));
#endif
else if (object->GetMetaType() == PGM_TABLE || object->GetMetaType() == GP_PARTITION)
{
// The syntax changed in 8.2 :-(
if (pgAppMinimumVersion(backupExecutable, 8, 2))
{
#ifdef WIN32
cmd.Append(wxT(" --table=\"\\\"") + ((pgTable*)object)->GetSchema()->GetIdentifier() +
wxT("\\\".\\\"") + ((pgTable*)object)->GetIdentifier() + wxT("\\\"\""));
#else
cmd.Append(wxT(" --table='") + ((pgTable*)object)->GetSchema()->GetQuotedIdentifier() +
wxT(".") + ((pgTable*)object)->GetQuotedIdentifier() + wxT("'"));
#endif
}
else
{
cmd.Append(wxT(" --table=") + ((pgTable*)object)->GetQuotedIdentifier());
cmd.Append(wxT(" --schema=") + ((pgTable*)object)->GetSchema()->GetQuotedIdentifier());
}
}
cmd.Append(wxT(" ") + commandLineCleanOption(object->GetDatabase()->GetQuotedIdentifier()));
return cmd;
}
void frmBackup::Go()
{
txtFilename->SetFocus();
Show(true);
}
void frmBackup::OnOK(wxCommandEvent &ev)
{
if (!done)
{
if (processedFile == txtFilename->GetValue())
{
if (wxMessageBox(_("Are you sure you wish to run a backup to this file again?"), _("Repeat backup?"), wxICON_QUESTION | wxYES_NO) == wxNO)
return;
}
processedFile = txtFilename->GetValue();
}
settings->Write(wxT("frmBackup/LastFile"), txtFilename->GetValue());
ExternProcessDialog::OnOK(ev);
}
backupFactory::backupFactory(menuFactoryList *list, wxMenu *mnu, ctlMenuToolbar *toolbar) : contextActionFactory(list)
{
mnu->Append(id, _("&Backup..."), _("Creates a backup of the current database to a local file"));
}
wxWindow *backupFactory::StartDialog(frmMain *form, pgObject *obj)
{
frmBackup *frm=new frmBackup(form, obj);
frm->Go();
return 0;
}
bool backupFactory::CheckEnable(pgObject *obj)
{
if (!obj)
return false;
if (obj->GetConnection() && obj->GetConnection()->EdbMinimumVersion(8, 0))
return obj->CanBackup() && !edbBackupExecutable.IsEmpty();
else if (obj->GetConnection() && obj->GetConnection()->GetIsGreenplum())
return obj->CanBackup() && !gpBackupExecutable.IsEmpty();
else
return obj->CanBackup() && !pgBackupExecutable.IsEmpty();
}