Location: PHPKode > scripts > Backup Sites and DBs > dump.php
<?php	
	define('DUMPS_PATH', 'PATH TO DUMP FOLDER');
	define('ROOT_PATH',  'PATH TO ROOT SITES FOLDER');
	$folders = array(
			'SITE FOLDER 1',
			'SITE FOLDER 2'
	);	
	define('DB_HOST', 'HOSTING DB HOST');
	define('DB_USER', 'HOSTING DB USER');
	define('DB_PASS', 'HOSTING DB PASSWORD');
	
	$big_dump_file = DUMPS_PATH . date('Y-m-d') . '.all.tgz';
	$dump = DUMPS_PATH . date('Y-m-d') . '/';
	if (!file_exists($big_dump_file)) {
		if (!is_dir($dump)) {
			mkdir($dump);
		}
		$conn = @mysql_connect(DB_HOST, DB_USER, DB_PASS)
        or die( mysql_errno(). ' : ' . mysql_error());
		$result = mysql_list_dbs($conn);
		$dbs = array();
		while($row = mysql_fetch_row( $result )) {
			if ($row[0] == 'information_schema') continue;
			$dbs[] = $row[0];
		}

		foreach ($dbs AS $db) {
			$db_file  = $dump . $db . '.sql';
			if (!file_exists($db_file) || (time() - filemtime($db_file) > 6000)) {
				$command = 'mysqldump -u ' . DB_USER . ' -p' . DB_PASS . ' ' . $db . ' > ' . $db_file;
				echo 'mysqldump ' . $db . '<br/>';
				exec($command);
			}
		}
		
		
		foreach ($folders AS $folder) {
			$db_file  = $dump . $folder . '.tgz';
			$tmp_file = $dump . $folder . '.tmp';	
				
			if (!file_exists($tmp_file) || (time() - filemtime($tmp_file) > 6000)) {
				unlink($tmp_file);
				$command = ' find ' . ROOT_PATH . '/' . $folder . '  -mtime -1 >> ' . $tmp_file;				
				echo 'find ' . $folder . '<br/>';
				exec($command);				
				$modified = file_get_contents($tmp_file);
				if ($modified) {
					if (
						strpos($modified, '.php') || strpos($modified, '.ctp') || strpos($modified, '.html')
						|| strpos($modified, '.css') || strpos($modified, '.js') || strpos($modified, '.jpeg')
						|| strpos($modified, '.jpg') || strpos($modified, '.gif') | strpos($modified, '.png')
					) {
						$command = 'tar -cvzpf ' . $db_file . ' ' . ROOT_PATH . '/' . $folder;
						echo 'tar ' . $folder . '<br/>';
						exec($command);						
					}
				}				
			}
		}
		$command = 'tar -cvzpf ' . $big_dump_file . ' ' . $dump;
		echo 'gzip all<br/>';		
		exec($command);
		
		foreach ($folders AS $folder) {
			$tmp_file = $dump . $folder . '.tmp';
			unlink($tmp_file);
		}
	}
	
	$date = date('Y-m-d', strtotime('-2 days'));
	$prev_dump_file = DUMPS_PATH . $date . '.all.tgz';
	$prev_dump      = DUMPS_PATH . $date . '/';	
	if (file_exists($prev_dump)) {
		$d = dir($prev_dump); 
		while($entry = $d->read()) { 
			if ($entry!= "." && $entry!= "..") {
				unlink($prev_dump . $entry);
			}
		} 
		$d->close(); 
		rmdir($prev_dump); 
	}
	if (file_exists($prev_dump_file)) {
		unlink($prev_dump_file);
	}
	echo 'OK!';
?>
Return current item: Backup Sites and DBs