public function backupDatabase(): TempFile
	{
		$this->check();
		$backup = new TempFile('database.sql', true);
		$tables = $this->driver->getTables();

		$backup->write("SET NAMES utf8;\n");
		$backup->write("SET time_zone = '+00:00';\n");
		$backup->write("SET foreign_key_checks = 0;\n");
		$backup->write("SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';\n\n\n");

		foreach ($tables as $table) {
			if (in_array($table, $this->excludeTables)) {
				continue;
			}
			$backup->write("DROP TABLE IF EXISTS `$table`;\n");

			$createTable = $this->driver->getCreateTable($table);
			$backup->write("$createTable;\n\n");

			$rows = $this->driver->getRows($table);
			$insert = [];
			$columns = null;
			$counter = 0;
			foreach ($rows as $row) {
				$counter++;
				if ($columns === null) {
					$colName = [];
					foreach ($row as $key => $value) {
						$colName[] = "`$key`";
					}
					$columns = implode(', ', $colName);
				}
				$cols = [];
				foreach ($row as $column) {
					if (is_string($column)) {
						$column = addslashes($column);
						$column = preg_replace("/\n/", "\\n", $column);
					} elseif ($column === null) {
						$cols[] = 'NULL';
						continue;
					}
					$cols[] = '"' . $column . '"';
				}
				$insert[] = implode(', ', $cols);

				if ($this->maxRows && $counter >= $this->maxRows) {
					$this->writeInsert($backup, $table, $columns, $insert);
					$counter = 0;
					$insert = [];
				}
			}
			$this->writeInsert($backup, $table, $columns, $insert);
			$backup->write("\n\n");
		}

		return $backup;
	}