Issues (994)

src/DB/backup-db.php (8 issues)

1
<?php
2
3
$DBH = new PDO("mysql:host=$db_hostname;dbname=$db_database; charset=utf8", $db_username, $db_password);
4
5
//put table names you want backed up in this array.
6
//leave empty to do all
7
$tables = [];
8
9
backup_tables($DBH, $tables);
10
11
function backup_tables($DBH, $tables)
12
{
13
  $DBH->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL);
14
15
  //Script Variables
16
  $compression = false;
17
  $BACKUP_PATH = '';
18
  $nowtimename = time();
19
20
  //create/open files
21
  if ($compression) {
0 ignored issues
show
The condition $compression is always false.
Loading history...
22
    $zp = gzopen($BACKUP_PATH . $nowtimename . '.sql.gz', 'a9');
23
  } else {
24
    $handle = fopen($BACKUP_PATH . $nowtimename . '.sql', 'a+');
25
  }
26
27
  //array of all database field types which just take numbers
28
  $numtypes = ['tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double', 'decimal', 'real'];
29
30
  //get all of the tables
31
  if (empty($tables)) {
32
    $pstm1 = $DBH->query('SHOW TABLES');
33
    while ($row = $pstm1->fetch(PDO::FETCH_NUM)) {
34
      $tables[] = $row[0];
35
    }
36
  } else {
37
    $tables = is_array($tables) ? $tables : explode(',', $tables);
38
  }
39
40
  //cycle through the table(s)
41
42
  foreach ($tables as $table) {
43
    $result = $DBH->query("SELECT * FROM $table");
44
    $num_fields = $result->columnCount();
45
    $num_rows = $result->rowCount();
46
47
    $return = '';
48
    //uncomment below if you want 'DROP TABLE IF EXISTS' displayed
49
    //$return.= 'DROP TABLE IF EXISTS `'.$table.'`;';
50
51
    //table structure
52
    $pstm2 = $DBH->query("SHOW CREATE TABLE $table");
53
    $row2 = $pstm2->fetch(PDO::FETCH_NUM);
54
    $ifnotexists = str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', $row2[1]);
55
    $return .= "\n\n" . $ifnotexists . ";\n\n";
56
57
    if ($compression) {
58
      gzwrite($zp, $return);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $zp does not seem to be defined for all execution paths leading up to this point.
Loading history...
59
    } else {
60
      fwrite($handle, $return);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $handle does not seem to be defined for all execution paths leading up to this point.
Loading history...
61
    }
62
    $return = '';
63
64
    //insert values
65
    if ($num_rows) {
66
      $return = 'INSERT INTO `' . "$table" . '` (';
67
      $pstm3 = $DBH->query("SHOW COLUMNS FROM $table");
68
      $count = 0;
69
      $type = [];
70
71
      while ($rows = $pstm3->fetch(PDO::FETCH_NUM)) {
72
        if (stripos($rows[1], '(')) {
73
          $type[$table][] = stristr($rows[1], '(', true);
74
        } else {
75
          $type[$table][] = $rows[1];
76
        }
77
78
        $return .= '`' . $rows[0] . '`';
79
        ++$count;
80
        if ($count < ($pstm3->rowCount())) {
81
          $return .= ', ';
82
        }
83
      }
84
85
      $return .= ')' . ' VALUES';
86
87
      if ($compression) {
88
        gzwrite($zp, $return);
89
      } else {
90
        fwrite($handle, $return);
91
      }
92
      $return = '';
93
    }
94
    $count = 0;
95
    while ($row = $result->fetch(PDO::FETCH_NUM)) {
96
      $return = "\n\t(";
97
98
      for ($j = 0; $j < $num_fields; ++$j) {
99
        //$row[$j] = preg_replace("\n","\\n",$row[$j]);
100
101
        if (isset($row[$j])) {
102
          //if number, take away "". else leave as string
103
          if ((in_array($type[$table][$j], $numtypes)) && (!empty($row[$j]))) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.
Loading history...
104
            $return .= $row[$j];
105
          } else {
106
            $return .= $DBH->quote($row[$j]);
107
          }
108
        } else {
109
          $return .= 'NULL';
110
        }
111
        if ($j < ($num_fields - 1)) {
112
          $return .= ',';
113
        }
114
      }
115
      ++$count;
116
      if ($count < ($result->rowCount())) {
117
        $return .= '),';
118
      } else {
119
        $return .= ');';
120
      }
121
      if ($compression) {
122
        gzwrite($zp, $return);
123
      } else {
124
        fwrite($handle, $return);
125
      }
126
      $return = '';
127
    }
128
    $return = "\n\n-- ------------------------------------------------ \n\n";
129
    if ($compression) {
130
      gzwrite($zp, $return);
131
    } else {
132
      fwrite($handle, $return);
133
    }
134
    $return = '';
0 ignored issues
show
The assignment to $return is dead and can be removed.
Loading history...
135
  }
136
137
  $error1 = $pstm2->errorInfo();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pstm2 seems to be defined by a foreach iteration on line 42. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
138
  $error2 = $pstm3->errorInfo();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pstm3 does not seem to be defined for all execution paths leading up to this point.
Loading history...
139
  $error3 = $result->errorInfo();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result seems to be defined by a foreach iteration on line 42. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
140
  echo $error1[2];
141
  echo $error2[2];
142
  echo $error3[2];
143
144
  if ($compression) {
145
    gzclose($zp);
146
  } else {
147
    fclose($handle);
148
  }
149
}
150