-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.php
176 lines (149 loc) · 4.16 KB
/
backup.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
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
<?php
/*
* @Name: php Site backup
*
* @Author:
* Basel Juma
* bjuma@shamconsultancy.com
*
* @Copyright 2017 Basel Juma
*
* Description: simple full site daily backup. using this script a daily backup will be taken
* and save for week, each day will be save in a separate folder.
* a backup folder will contain a cope of both database(gzip) and site files(zip)
*
*
* Requirements: PHP5 or above, mysql 5
*/
// Make sure the script can handle large folders/files
ini_set('max_execution_time', 3600);
ini_set('memory_limit','1024M');
/*
*
* Configuration
*
*/
// path to the site to files
$SitePath = "/home/username/public_html/";
// backup folder ... better not to be in public_html
$BackupPath ="/home/username/backup/";
// Database configuration
$DBUSER="zawajzon_edbuser";
$DBPASSWD="zawajzone123$";
$DATABASE="zawajzon_e_db_1";
// folders to exclude
$exclude=[
'/home/username/public_html/xyz',
];
/*
* Step one:
* make sure backup folder exists, if not create one
* then make sure day backup folder exists, if not create
*/
// Check if backup folder exists
if ( ! is_dir($BackupPath) ) {
mkdir($BackupPath, 0777, true); //create folder
}
$day= date("D");
// Make Day folder if not exists
if ( ! is_dir($BackupPath."$day/") ) {
mkdir($BackupPath."$day/" , 0777, true); //create folder
}
// change backup folder to day
$BackupPath .= "$day/";
addMessageLine('start site backup');
// clear log file if exists
$log = $BackupPath . "backupLog.txt";
if(file_exists($log)){
unlink($log);
}
/*
* Step two:
* make site backup by looping all folder files and sub files
*/
// Make site Backup
addMessageLine('start site backup');
Zip($SitePath,$BackupPath."site.zip");
addMessageLine('site backup done');
addMessageLine('backup done');
/*
* Step three:
* make database backup using commandline
*/
// Make db backup
$DBFile = $BackupPath . "db.gz";
addMessageLine('start database backup');
db_backup($DBUSER,$DBPASSWD,$DATABASE,$DBFile);
addMessageLine('database backup done');
/*
* db_backup:
* database backup function
*/
function db_backup($DBUSER,$DBPASSWD,$DATABASE,$DBFilename)
{
$cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best > $DBFilename";
passthru($cmd);
}
/*
* Zip:
* site backup function,zipping
*/
function Zip($source, $destination)
{
global $exclude;
if (!extension_loaded('zip') || !file_exists($source)) {
addMessageLine( "zip extension is a must");
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZipArchive::CREATE|ZipArchive::OVERWRITE)) {
addMessageLine( "Can't open backup file: $destination");
return false;
}
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source) === true)
{
$files = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator(
$source,
RecursiveDirectoryIterator::SKIP_DOTS
),
function ($files, $key, $iterator) use ($exclude) {
return !(in_array($files->getPath(). "/" .$files->getFilename(), $exclude));
}
),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($files as $file)
{
$file = str_replace('\\', '/', $file);
if (is_dir($file) === true)
{
addMessageLine("zipping: $file");
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}
else if (is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
/*
* addMessageLine:
* add message to log file
*/
function addMessageLine($m){
global $BackupPath;
$log = $BackupPath . "backupLog.txt";
$message = $m . " ... " . date("Y-m-d H:i:s") ."\r\n";
echo $message;
file_put_contents($log, $message.PHP_EOL , FILE_APPEND | LOCK_EX);
}
?>