-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackup.php
More file actions
74 lines (57 loc) · 3.11 KB
/
Copy pathbackup.php
File metadata and controls
74 lines (57 loc) · 3.11 KB
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
<?php
/**
* @author Nischay Nahata <nischayn22@gmail.com>
* @license GPL v2 or later
*/
error_reporting(E_ERROR | E_PARSE);
$settings = array();
$settings['dbName'] = 'core';
$settings['dbHost'] = 'localhost';
$settings['dbUser'] = 'root';
$settings['dbPassword'] = '';
// better to provide the absolute path here
$settings['uploadDir'] = 'core/images';
$settings['dailybackupsfolder'] = 'daily_backups';
$settings['weeklybackupsfolder'] = 'weekly_backups';
$settings['monthlybackupsfolder'] = 'monthly_backups';
echo "Hello! Lets begin making backups.\n";
if (!file_exists($settings['dailybackupsfolder'])) {
mkdir( $settings['dailybackupsfolder'], 0777, true);
}
if (!file_exists($settings['weeklybackupsfolder'])) {
mkdir( $settings['weeklybackupsfolder'], 0777, true);
}
if (!file_exists($settings['monthlybackupsfolder'])) {
mkdir( $settings['monthlybackupsfolder'], 0777, true);
}
echo "Date: ". date("Y/m/d") . "\n";
echo "Creating mysql dump\n";
exec( "mysqldump -u ". $settings['dbUser'] ." --password=" . $settings['dbPassword'] . " " . $settings['dbName'] . " | gzip > mysqldump.sql.gz.tmp" );
echo "Creating images backup\n";
exec("tar -cvpzf imagesbackup.tar.gz.tmp " . $settings['uploadDir']);
exec( "cp mysqldump.sql.gz.tmp " . $settings['dailybackupsfolder']. "/dbdump-" . date("Y-m-d") . ".sql.gz" );
exec( "cp imagesbackup.tar.gz.tmp " . $settings['dailybackupsfolder']. "/imagesbackup-" . date("Y-m-d") . ".tar.gz" );
$today = strtotime( 'today', time() );
$week_end = strtotime('next Sunday', time() - 24*60*60); // this will give us sunday of this week
if( $week_end == $today )
{
echo "Today is sunday, copying to weekly backup folder as well\n";
exec( "cp mysqldump.sql.gz.tmp " . $settings['weeklybackupsfolder']. "/dbdump-" . date("Y-m-d") . ".sql.gz" );
exec( "cp imagesbackup.tar.gz.tmp " . $settings['weeklybackupsfolder']. "/imagesbackup-" . date("Y-m-d") . ".tar.gz" );
}
$month_end = (strtotime('next month',strtotime(date('m/01/y'))) - 24*60*60); // this will give us the last day of this month
if( $today == $month_end )
{
echo "Today is last day of month, copying to monthly backup folder as well\n";
exec( "cp mysqldump.sql.gz.tmp " . $settings['monthlybackupsfolder']. "/dbdump-" . date("Y-m-d") . ".sql.gz" );
exec( "cp imagesbackup.tar.gz.tmp " . $settings['monthlybackupsfolder']. "/imagesbackup-" . date("Y-m-d") . ".tar.gz" );
}
echo "Now deleting old and temporary backups \n";
exec("find " .$settings['dailybackupsfolder']. "/dbdump*.gz -maxdepth 1 -type f -mtime +7 -delete");
exec("find " .$settings['weeklybackupsfolder']. "/dbdump*.gz -maxdepth 1 -type f -mtime +32 -delete");
exec("find " .$settings['monthlybackupsfolder']. "/dbdump*.gz -maxdepth 1 -type f -mtime +92 -delete");
exec("find " .$settings['dailybackupsfolder']. "/imagesbackup*.gz -maxdepth 1 -type f -mtime +7 -delete");
exec("find " .$settings['weeklybackupsfolder']. "/imagesbackup*.gz -maxdepth 1 -type f -mtime +32 -delete");
exec("find " .$settings['monthlybackupsfolder']. "/imagesbackup*.gz -maxdepth 1 -type f -mtime +92 -delete");
exec("rm mysqldump.sql.gz.tmp ");
exec("rm imagesbackup.tar.gz.tmp");