Subversion Repositories computer_asset_manager_v1

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 rodolico 1
<?php
2
 
3
   global $BACKUP_DATA;
4
   $BACKUP_DATA = array(
5
      'client' => array(
6
                         'table' => 'client',
7
                         'fields' => array(  'name',
8
                                             'notes',
9
                                             'internal_notes',
10
                                             'added_date',
11
                                             'removed_date'
12
                                          ),
13
                         'query' => 'select name,notes,internal_notes,added_date,removed_date from client',
14
                         'insert' => 'insert into client(name,notes,internal_notes,added_date,removed_date) select <name>,<notes>,<internal_notes>,<added_date>,<removed_date>'
15
                  ),
16
      'site' => array(
17
                         'table' => 'site',
18
                         'fields' => array(  'client',
19
                                             'name',
20
                                             'notes',
21
                                             'added_date',
22
                                             'removed_date'
23
                                          ),
24
                         'query' => 'select client.name client, site.name name,site.notes,site.added_date,site.removed_date from site join client using (client_id)',
25
                         'insert' => 'insert into site(client_id,name,notes,added_date,removed_date) select client_id,<name>,<notes>,<added_date>,<removed_date> from client where name = <client>'
26
                  ),
27
      'client_alias' => array(
28
                         'table' => 'client_alias',
29
                         'fields' => array(  'client',
30
                                             'alias',
31
                                             'added_date',
32
                                             'removed_date'
33
                                          ),
34
                         'query' => 'select client.name client, alias,client_alias.added_date,client_alias.removed_date from client_alias join client using (client_id)',
35
                         'insert' => 'insert into client_alias(client_id,alias,added_date,removed_date) select client_id,<alias>,<added_date>,<removed_date> from client where name = <client>'
36
                  ),
37
      'attrib'      => array(
38
                         'table' => 'attrib',
39
                         'fields' => array(  'name',
40
                                             'added_date',
41
                                             'removed_date'
42
                                     ),
43
                         'query' => 'select name,added_date,removed_date from attrib',
44
                         'insert' => 'insert into attrib(name,added_date,removed_date) select <name>,<added_date>,<removed_date>'
45
                  ),
46
      'device_type' => array(
47
                         'table' => 'device_type',
48
                         'fields' => array(  'name',
49
                                             'show_as_system',
50
                                             'added_date',
51
                                             'removed_date'
52
                                          ),
53
                         'query' => 'select name,show_as_system,added_date,removed_date from device_type',
54
                         'insert' => 'insert into device_type(name,show_as_system,added_date,removed_date) select <name>,<show_as_system>,<added_date>,<removed_date>'
55
                  ),
56
      'computers' => array(
57
                         'table' => 'device',
58
                         'fields' => array(  'client',
59
                                             'site',
60
                                             'device_type',
61
                                             'name',
62
                                             'notes',
63
                                             'added_date',
64
                                             'removed_date'
65
                                          ),
66
                         'query' => " select client.name client, site.name site, device_type.name device_type, device.name,device.notes,device.added_date,device.removed_date
67
                                      from device join device_type using (device_type_id) join site using (site_id) join client using (client_id)
68
                                      where device_type.show_as_system = 'Y' and device.part_of is null
69
                                    ",
70
 
71
                         'insert' => 'insert into device(site_id,device_type_id,name,notes,added_date,removed_date) 
72
                                      select min(site.site_id),device_type.device_type_id,<name>,<notes>,<added_date>,<removed_date> 
73
                                      from client join site using (client_id), device_type 
74
                                      where client.name = <client> and site.name=<site> and device_type.name=<device_type>
75
                                      group by device_type.device_type_id'
76
                  ),
77
      'virtual servers' => array(
78
                         'table' => 'device',
79
                         'fields' => array(  'client',
80
                                             'site',
81
                                             'device_type',
82
                                             'part_of',
83
                                             'name',
84
                                             'notes',
85
                                             'added_date',
86
                                             'removed_date'
87
                                          ),
88
                         'query' => " select client.name client, site.name site, device_type.name device_type, parent.name part_of, device.name,device.notes,device.added_date,device.removed_date
89
                                      from device join device_type using (device_type_id) join site using (site_id) join client using (client_id) join device parent on device.part_of = parent.device_id
90
                                      where device_type.show_as_system = 'Y'
91
                                    ",
92
                         'insert' => 'insert into device(site_id,device_type_id,part_of,name,notes,added_date,removed_date)
93
                                      select site.site_id,device_type.device_type_id,parent.device_id,<name>,<notes>,<added_date>,<removed_date> 
94
                                      from client join site using (client_id) join device parent, device_type 
95
                                      where client.name = <client> and site.name=<site> and device_type.name=<device_type> and parent.name = <part_of>'
96
      ),
97
   'device_attribs' => array(
98
                          'table' => 'device_attrib',
99
                          'fields' => array('client',
100
                                             'site',
101
                                             'device',
102
                                             'attrib',
103
                                             'value',
104
                                             'added_date',
105
                                             'removed_date'
106
                                      ),
107
                     'query' => "select client.name client,
108
                                       site.name site,
109
                                       device.name device,
110
                                       attrib.name attrib,
111
                                       device_attrib.value value,
112
                                       device_attrib.added_date,
113
                                       device_attrib.removed_date
114
                                 from device_attrib join device using (device_id) join site using (site_id) join client using (client_id) join attrib using (attrib_id) join device_type using (device_type_id)
115
                                 where device_type.show_as_system = 'Y'",
116
                     'insert' => 'insert into device_attrib(device_id,attrib_id,value,added_date,removed_date)
117
                                      select device.device_id,attrib.attrib_id,<value>,<added_date>,<removed_date> 
118
                                      from client join site using (client_id) join device using (site_id),attrib
119
                                      where client.name = <client> and site.name=<site> and device.name=<device> and attrib.name = <attrib>'
120
       ),
121
      'device_alias' => array(
122
                         'table' => 'device_alias',
123
                         'fields' => array(  'device',
124
                                             'alias',
125
                                             'added_date',
126
                                             'removed_date'
127
                                          ),
128
                         'query' => 'select device.name device, alias,device_alias.added_date,device_alias.removed_date from device_alias join device using (device_id)',
129
                         'insert' => 'insert into device_alias(device_id,alias,added_date,removed_date) 
130
                                      select device_id,<alias>,<added_date>,<removed_date> from device where name = <device>'
131
                  ),
132
      'report' => array(
133
                         'table' => 'report',
134
                         'fields' => array(  'name',
135
                                             'query',
136
                                             'parameters',
137
                                             'screen_report'
138
                                          ),
139
                         'query' => 'select name,query,parameters,screen_report from report',
140
                         'insert' => 'insert into report(name,query,parameters,screen_report) 
141
                                      select <name>,<query>,<parameters>,<screen_report>'
142
                  ),
143
      'login' => array(
144
                         'table' => 'login',
145
                         'fields' => array(  'login_id',
146
                                             'email',
147
                                             'pass',
148
                                             'where_clause',
149
                                             'added_date',
150
                                             'removed_date'
151
                                          ),
152
                         'query' => ' select login_id,email,pass,where_clause,added_date,removed_date from login;',
153
                         'insert' => 'insert into login(login_id,email,pass,where_clause,added_date,removed_date) 
154
                                      select <login_id>,<email>,<pass>,<where_clause>,<added_date>,<removed_date>'
155
                  ),
156
   );
157
 
158
   function backup() {
159
      //$tmpfname = tempnam("/tmp", "BDB");
160
      $tmpfname = '/tmp/' . date('YmdHiss') . 'camp.sql';
161
      $handle = fopen($tmpfname, "w");
162
      global $BACKUP_DATA;
163
      $returnValues = array();
164
      $tableNames = array();
165
      foreach ($BACKUP_DATA as $job => $info) {
166
         $tableNames[$info['table']] = 1;
167
      }
168
      foreach ($tableNames as $tableName => $trash) {
169
         //fwrite($handle, "select '$tableName', count(*) from $tableName;\n");
170
         fwrite($handle, "delete from $tableName;\n"); // delete everything in the table
171
         fwrite($handle, "ALTER table $tableName auto_increment=1;\n"); // and set any auto_increments to start at 1
172
      }
173
      foreach ($BACKUP_DATA as $job => $info) {
174
         fwrite($handle, "/* $job */\n");
175
         $table = $info['table'];
176
         $resource = mysql_query( $info['query'] );
177
         $fieldNames = implode($info['fields'], ',');
178
         while ($thisRow = mysql_fetch_assoc($resource)) {
179
            $insertQuery = $info['insert'];
180
            foreach ($info['fields'] as $thisField) {
181
               $insertQuery = search_replace_string($insertQuery, '<' . $thisField . '>', makeSafeSQLValue($thisRow[$thisField]) );
182
            }
183
            fwrite($handle, "$insertQuery;\n");
184
         }
185
      }
186
      fclose($handle);
187
      system  ( "gzip $tmpfname" );
188
      $tmpfname .= '.gz';
189
      header('Content-type: application/gzip');
190
      // It will be called downloaded.pdf
191
      header('Content-Disposition: attachment; filename="' . basename($tmpfname) . '"');
192
      readfile($tmpfname);
193
      unlink($tmpfname);
194
   }
195
 
196
 
197
   // initialize any module definitions also
198
   $InstalledModules = array();
199
   // get module information
200
   $data = queryDatabaseExtended( "select theValue from _system where removed_date is null and group_name = 'Modules'");
201
   if ($data) {
202
      foreach ($data['data'] as $row) {
203
         $InstalledModules[] = $row['theValue'];
204
      }
205
      // note, we are only going to include the database.php. All other stuff is left to the individual modules
206
      // $_SESSION['file system root'] is set in login.php, and is the path to the root of this application, so all else is relative
207
      foreach ($InstalledModules as $directory) {
208
         include_once( $_SESSION['file system root'] . "/$directory/backupDB.php");
209
 
210
      }
211
   }
212
 
213
 
214
 
215
?>