-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_script.php
66 lines (55 loc) · 1.8 KB
/
backup_script.php
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
<?php
//assume running from command line
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once "config.php";
global $backups;
global $backupDestination;
// Goes through each configured backup location, copying the entire directory structure to the destination.
// For Windows systems, origin drive letters will be replaced with just the letter, ie: C:\ => C\
foreach($backups as $bl) {
$fullBackupDestination = $backupDestination;
// This script replaces drive letters with just a letter, for Windows systems
preg_match("/^[a-zA-Z]:\\.*/", $bl, $matches);
if($matches) {
$drive = substr($matches[0], 0, 1);
$blStripped = preg_replace('/^[a-zA-Z]:/', '', $bl);
$fullBackupDestination .= '\\' . $drive . $blStripped;
} else {
$fullBackupDestination .= $bl;
}
if(is_dir($bl)) {
_ensure_directory($fullBackupDestination);
_recursive_copy_directory($bl, $fullBackupDestination);
} else {
$destinationDirectory = dirname($fullBackupDestination);
_ensure_directory($destinationDirectory);
//ensure_directory($destinationDirectory);
copy($bl, $fullBackupDestination);
}
echo "\nBacked up: " . $fullBackupDestination;
}
echo "\n\nDone!\n";
function _ensure_directory($dir) {
if (!file_exists($dir)) {
echo "\nCreating directory " + $dir;
mkdir($dir, 0777, true);
}
}
function _recursive_copy_directory($src,$dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
_recursive_copy_directory($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
?>