@@ -87,11 +87,14 @@ func init() {
87
87
log .Fatalf ("Cannot create related models: %s" , err )
88
88
}
89
89
90
- Db .AutoMigrate (& Challenge {}, & Transaction {}, & Port {}, & User {}, & Tag {}, & Notification {}, & Hint {}, & DynamicFlag {}, & OTP {})
90
+ err := Db .AutoMigrate (& Challenge {}, & Transaction {}, & Port {}, & User {}, & Tag {}, & Notification {}, & Hint {}, & DynamicFlag {}, & OTP {})
91
+ if err != nil {
92
+ log .Fatalf ("failed to migrate database with error: %s" , err )
93
+ }
94
+
91
95
users , err := QueryUserEntries ("email" , core .DEFAULT_USER_EMAIL )
92
96
if err != nil {
93
- log .Errorf ("Error while checking dummy user entry." )
94
- os .Exit (1 )
97
+ log .Fatalf ("Error while checking dummy user entry." )
95
98
}
96
99
97
100
if len (users ) == 0 {
@@ -116,9 +119,8 @@ func init() {
116
119
}
117
120
118
121
func BackupAndReset () {
119
- beastRemoteDir := filepath .Join (BEAST_GLOBAL_DIR , core .BEAST_REMOTES_DIR )
120
- beastStagingDir := filepath .Join (BEAST_GLOBAL_DIR , core .BEAST_STAGING_DIR )
121
122
LoadDbConfig ()
123
+
122
124
err := BackupDatabase ()
123
125
if err != nil {
124
126
log .Errorf ("Error while backing up database: %s" , err )
@@ -129,12 +131,33 @@ func BackupAndReset() {
129
131
log .Errorf ("Error while resetting up database: %s" , err )
130
132
return
131
133
}
132
- err = os .Rename (beastRemoteDir , beastRemoteDir + time .Now ().Format ("20060102150405" )+ ".bak" )
134
+
135
+ backupPath := filepath .Join (core .BEAST_GLOBAL_DIR , "backup" , core .BEAST_REMOTES_DIR )
136
+ err = utils .CreateIfNotExistDir (backupPath )
137
+ if err != nil {
138
+ log .Errorf ("Error while creating backup directory: %s" , err )
139
+ return
140
+ }
141
+
142
+ backupPath = filepath .Join (backupPath , core .BEAST_REMOTES_DIR + time .Now ().Format ("20060102150405" )+ ".bak" )
143
+ oldPath := filepath .Join (core .BEAST_GLOBAL_DIR , core .BEAST_REMOTES_DIR )
144
+ err = os .Rename (oldPath , backupPath )
133
145
if err != nil {
134
146
log .Errorf ("Error while backing up remote dir: %s" , err )
135
147
return
136
148
}
137
- err = os .Rename (beastStagingDir , beastStagingDir + time .Now ().Format ("20060102150405" )+ ".bak" )
149
+
150
+ backupPath = filepath .Join (core .BEAST_GLOBAL_DIR , "backup" , core .BEAST_STAGING_DIR )
151
+
152
+ err = utils .CreateIfNotExistDir (backupPath )
153
+ if err != nil {
154
+ log .Errorf ("Error while creating backup directory: %s" , err )
155
+ return
156
+ }
157
+
158
+ oldPath = filepath .Join (core .BEAST_GLOBAL_DIR , core .BEAST_STAGING_DIR )
159
+ backupPath = filepath .Join (backupPath , core .BEAST_STAGING_DIR + time .Now ().Format ("20060102150405" )+ ".bak" )
160
+ err = os .Rename (oldPath , backupPath )
138
161
if err != nil {
139
162
log .Errorf ("Error while backing up staging dir: %s" , err )
140
163
return
@@ -145,8 +168,16 @@ func BackupDatabase() error {
145
168
if dbConfig == (Config {}) {
146
169
LoadDbConfig ()
147
170
}
171
+
172
+ backupPath := filepath .Join (core .BEAST_GLOBAL_DIR , "backup" , "db" )
173
+ err := utils .CreateIfNotExistDir (backupPath )
174
+ if err != nil {
175
+ log .Errorf ("Error while creating backup directory: %s" , err )
176
+ return err
177
+ }
178
+
148
179
backupFile := fmt .Sprintf ("%s_%s.bak" , dbConfig .PsqlConf .Dbname , time .Now ().Format ("20060102150405" ))
149
- cmd := exec .Command ("pg_dump" , "-U" , dbConfig .PsqlConf .User , "-h" , dbConfig .PsqlConf .Host , "-p" , dbConfig .PsqlConf .Port , "-F" , "c" , "-f" , filepath .Join (core . BEAST_GLOBAL_DIR , backupFile ), dbConfig .PsqlConf .Dbname )
180
+ cmd := exec .Command ("pg_dump" , "-U" , dbConfig .PsqlConf .User , "-h" , dbConfig .PsqlConf .Host , "-p" , dbConfig .PsqlConf .Port , "-F" , "c" , "-f" , filepath .Join (backupPath , backupFile ), dbConfig .PsqlConf .Dbname )
150
181
cmd .Env = append (os .Environ (), fmt .Sprintf ("PGPASSWORD=%s" , dbConfig .PsqlConf .Password ))
151
182
output , err := cmd .CombinedOutput ()
152
183
if err != nil {
@@ -163,11 +194,11 @@ func ResetDatabase() error {
163
194
}
164
195
err := TerminateDatabaseConnections ()
165
196
if err != nil {
166
- log .Errorf ("Unable to terminate connections " , err )
197
+ log .Errorf ("Unable to terminate connections %s " , err )
167
198
return err
168
199
}
169
200
170
- dropCmd := exec .Command ("psql " , "-U" , dbConfig .PsqlConf .User , "-h" , dbConfig .PsqlConf .Host , "-p" , dbConfig .PsqlConf .Port , "-c " , "DROP DATABASE IF EXISTS " + dbConfig .PsqlConf .Dbname )
201
+ dropCmd := exec .Command ("dropdb " , "-U" , dbConfig .PsqlConf .User , "-h" , dbConfig .PsqlConf .Host , "-p" , dbConfig .PsqlConf .Port , "--force " , dbConfig .PsqlConf .Dbname )
171
202
dropCmd .Env = append (os .Environ (), fmt .Sprintf ("PGPASSWORD=%s" , dbConfig .PsqlConf .Password ))
172
203
173
204
output , err := dropCmd .CombinedOutput ()
@@ -176,7 +207,7 @@ func ResetDatabase() error {
176
207
return err
177
208
}
178
209
179
- createCmd := exec .Command ("psql" , "-U" , dbConfig .PsqlConf .User , "-h" , dbConfig .PsqlConf .Host , "-p" , dbConfig .PsqlConf .Port , "-c" , "CREATE DATABASE " + dbConfig .PsqlConf .Dbname )
210
+ createCmd := exec .Command ("psql" , "-U" , dbConfig .PsqlConf .User , "-h" , dbConfig .PsqlConf .Host , "-p" , dbConfig .PsqlConf .Port , "-d" , "postgres" , "- c" , "CREATE DATABASE " + dbConfig .PsqlConf .Dbname + ";" )
180
211
createCmd .Env = append (os .Environ (), fmt .Sprintf ("PGPASSWORD=%s" , dbConfig .PsqlConf .Password ))
181
212
182
213
output , err = createCmd .CombinedOutput ()
@@ -218,10 +249,10 @@ func RestoreDatabase(backupFile string) error {
218
249
219
250
err := TerminateDatabaseConnections ()
220
251
if err != nil {
221
- log .Error ("Unable to terminate connections " , err )
252
+ log .Errorf ("Unable to terminate connections: %s " , err )
222
253
return err
223
254
}
224
-
255
+
225
256
err = utils .ValidateFileExists (backupFile )
226
257
if err != nil {
227
258
return fmt .Errorf ("backup file does not exist: %s" , backupFile )
@@ -249,21 +280,3 @@ func RestoreDatabase(backupFile string) error {
249
280
log .Println ("Database restored successfully from:" , backupFile )
250
281
return nil
251
282
}
252
-
253
- // func BackupDatabase() {
254
- // beastDb := filepath.Join(BEAST_GLOBAL_DIR, BEAST_DATABASE)
255
- // beastRemoteDir := filepath.Join(BEAST_GLOBAL_DIR, core.BEAST_REMOTES_DIR)
256
- // beastStagingDir := filepath.Join(BEAST_GLOBAL_DIR, core.BEAST_STAGING_DIR)
257
- // err := utils.CopyFile(beastDb, beastDb+time.Now().Format("20060102150405")+".bak")
258
- // if err != nil {
259
- // log.Errorf("Error while backing up database: %s", err)
260
- // }
261
- // err = utils.CopyDirectory(beastRemoteDir, beastRemoteDir+time.Now().Format("20060102150405")+".bak")
262
- // if err != nil {
263
- // log.Errorf("Error while backing up remote dir: %s", err)
264
- // }
265
- // err = utils.CopyDirectory(beastStagingDir, beastStagingDir+time.Now().Format("20060102150405")+".bak")
266
- // if err != nil {
267
- // log.Errorf("Error while backing up staging dir: %s", err)
268
- // }
269
- // }
0 commit comments