MyBB Community Forums

Full Version: Backup Module Addition
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Here's a Patches export file for the above, which makes the backup task more memory friendly, and also faster. It's faster by reducing the gzip level from 9 to 5, which cuts CPU time by about half. For my 100k posts database (40MB compressed), the difference in size was negligible (<1MiB). gzip level 9 wastes a lot of CPU on nothing.

It also changes the backup filename in two ways:
- it adds the date into the filename, so it's called backup__20130423_220040_XWefwiefowihf for a backup that was created on 23th April 2013 at 22:00:40.
- the filename starts out as *.incomplete.sql.gz and is renamed to *.sql.gz only after the script has finished. So in the event that the task was killed prematurely due to memory or time limits of your host, you'll see right away by the filename that it's an incomplete backup.
Note that this code doesn't check for errors due to full disk (I'm not sure what PHP does in that case).

These patches require you to use gzwrite (if your backups are already *.sql.gz, you're good) and mysqli (regular / old mysql or other databases won't work - change to mysqli in inc/config.php).

Changes in detail for inc/tasks/backupdb.php:

/* - /* 		$file = MYBB_ADMIN_DIR.'backups/backup_'.substr(md5($mybb->user['uid'].TIME_NOW), 0, 10).random_str(54);
/* + */ $file = MYBB_ADMIN_DIR.'backups/backup_'.date("_Ymd_His_").random_str(16);
====
/* - /* 			$fp = gzopen($file.'.sql.gz', 'w9');
/* + */ $fp = gzopen($file.'.incomplete.sql.gz', 'wb5');
====
			gzclose($fp);
/* + */ rename($file.'.incomplete.sql.gz', $file.'.sql.gz');
====
/* - /* 			$query = $db->simple_select($table);
/* + */ $query = mysqli_query($db->read_link, "SELECT * FROM {$db->table_prefix}{$table}", MYSQLI_USE_RESULT);
====
/* - /* 		$contents = $header;
/* + */ gzwrite($fp, $header, strlen($header));
====
/* - /* 			$contents .= $structure;
/* - /* 			clear_overflow($fp, $contents);
/* + */ gzwrite($fp, $structure, strlen($structure));
====
/* - /* 				$contents .= $insert;
/* - /* 				clear_overflow($fp, $contents);
/* - /* 			}
/* + */ gzwrite($fp, $insert, strlen($insert));
/* + */ }
/* + */ $db->free_result($query);

Note that this does not affect the manual creation of backups using the Admin CP -> Tools -> Backup system. But you can always manually create a new backup using the task by going to Admin CP -> Tools -> Tasks -> Run this task now (for the Weekly Backup Task).
With UTF-8 validation added to $db->escape_string(), you may be seeing incomplete backups in MyBB 1.6.12, as the backup task calls it a few million times...

/* - /* 						$insert .= $comma."'".$db->escape_string($row[$field])."'";
/* + */ $insert .= $comma."'".mysqli_real_escape_string($db->read_link, $row[$field])."'";
Pages: 1 2