groot-backupscript

Gesponsorde koppelingen

PHP script bestanden

  1. groot-backupscript

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php


# ******************** #
# ***** SETTINGS ***** #

 
// Enter the dir you want to backup, for example: /var/services/web/diryouwantotbackup.
// Documt Root is default to backup the entire web-root directory.

$dir2backup         = $_SERVER['DOCUMENT_ROOT'];

// MySQL login data
$db_name="xxxx";
$db_location="localhost";
$db_user="xxxx";
$db_password="xxxx";


// Where to do you want to send the backup to?
$email            ="ex#[email protected]";
$subject        ="Backup website";

# ***** SETTINGS ***** #
# ******************** #

# ***************************** #
# ***** ZIPPING DIRECTORY ***** #


// Creating random filename for security reasons

$filenamezip = 'BackUp'.rand(1000, 9999).'.zip';

// Creating archive
$zip = new ZipArchive();
$zip->open($filenamezip, ZipArchive::CREATE);

// Setting dir to add to archive
$dirName = $dir2backup;

// Checking dir
if (!is_dir($dirName)) {
    throw new Exception('Directory ' . $dirName . ' does not exist');
}


// Getting realpath
$dirName = realpath($dirName);
if (substr($dirName, -1) != '/') {
    $dirName.= '/';
}




$dirStack = array($dirName);
//Find the index where the last dir starts
$cutFrom = strrpos(substr($dirName, 0, -1), '/')+1;


while (!empty($dirStack)) {
    $currentDir = array_pop($dirStack);
    $filesToAdd = array();

    $dir = dir($currentDir);
    while (false !== ($node = $dir->read())) {
        if (($node == '..') || ($node == '.')) {
            continue;
        }

        if (is_dir($currentDir . $node)) {
            array_push($dirStack, $currentDir . $node . '/');
        }

        if (is_file($currentDir . $node)) {
            $filesToAdd[] = $node;
        }
    }


    $localDir = substr($currentDir, $cutFrom);
    $zip->addEmptyDir($localDir);
  
    foreach ($filesToAdd as $file) {
        $zip->addFile($currentDir . $file, $localDir . $file);
    }
}







# ***** ZIPPING DIRECTORY ***** #
# ***************************** #
 
# ************************* #
# ***** DUMPING MYSQL ***** #


// Connect to mysql database


$connect = mysql_connect($db_location,$db_user,$db_password);
if($connect == TRUE) {
  if(mysql_select_db($db_name) != TRUE) {
    exit("<span style='color: red'>Can't connect to the MySQL database. </body></html>");
  }
}
else{
    exit("<span style='color: red'>Can't connect to the MySQL server.</body></html>");
}

  

// return all available tables
$result_tbl = mysql_query( "SHOW TABLES FROM `$db_name`");

// Creating $output.
$output = '';
while ($row = mysql_fetch_row($result_tbl)) { // De tabellen  aanmaken
    // Table name

    $table = $row[0];
    // Tabel create
    $output .= "\n\n\tCREATE TABLE ".$table." (\n";
    // Select fields
    $result_fld = mysql_query( "SHOW FIELDS FROM ".$table);
    $aant = mysql_num_rows($result_fld);
    // Start counter
    $i = 0;  
    while($row1 = mysql_fetch_row($result_fld)) {
        // Count ++
        $i++;
        // Fieldname - type - auto_increment
        $output .= "\t\t".$row1[0]." ".$row1[1]." ".$row1[5];
        if($aant != $i){
            // If not last, insert a comma.
            $output .= ",\n";
        }

        // If field = primary key
        if($row1[3] == 'PRI'){
            $output .= "\t\tPRIMARY KEY (".$row1[0].")";
            // If not last, insert comma.
            if($aant != $i){
                $output .= ",\n";
            }
        }
    }

    // Close create table
    $output .= "\n\t);\n\n";
    
    // Select data from table
    $insrt_sql = "SELECT * FROM ".$table;
    $insrt_res = mysql_query($insrt_sql);
    while($ins = mysql_fetch_row($insrt_res)){
        // Start INSERT INTO
        $output .= "\tINSERT INTO ".$table." VALUES (";
        for($i=0;$i<count($ins);$i++){
            if(is_numeric($ins[$i])){
                // If nummeric, no quotes
                $output .= $ins[$i];
            }
else{                
                // else insert ''
               $output .= "'".str_replace("'","\'",stripslashes($ins[$i]))."'";

            }

            if($i+1<count($ins)){
                // If not last, insert comma
                $output .= ',';
            }
        }

        // close INSERT INTO
        $output .= ");\n";    
    }
}


 


# ***** DUMPING MYSQL ***** #
# ************************* #

# *************************** #
# ***** FINISHING TOUCH ***** #

// Add MySQL to archive

$zip->addFromString('MySQL DUMP.sql', $output);

// Closing zip file
$zip->close();

# ***** FINISHING TOUCH ***** #
# *************************** #

# *********************************** #
# ***** SENDING BACKUP BY EMAIL ***** #



//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash

$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: $email \r\nReply-To: $email";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks

$attachment = chunk_split(base64_encode(file_get_contents($filenamezip)));
//define the body of the message.
ob_start(); //Turn on output buffering
?>

--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Backup of your website created succesfully!
Created on: <? echo date('c'); ?>


--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Backup of your website created succesfully!</h2>
<p>created on <b><? echo date('c'); ?></b></p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="backup.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = mail($email, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Backup succesfully sent" : "Mail failed";

# ***** SENDING BACKUP BY EMAIL ***** #
# *********************************** #

# ************************************** #
# ***** DELETE ARCHIVE FROM SERVER ***** #


unlink($filenamezip);

# ***** DELETE ARCHIVE FROM SERVER ***** #
# ************************************** #


?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.