从命令行备份MySql数据库

时间:2010-06-11 06:22:09

标签: mysql mysqldump

我可以通过执行以下命令来通过命令行备份mysql数据库:

  

C:\ MySQL \ MySQL Server   5.0 \ bin \ mysqldump \“-uroot -ppassword sample> \”D:/admindb/AAR12.sql \“

但我的.mysql文件中没有DROP和CREATE数据库查询

我应该在语法中添加什么来获取创建信息到我生成的.sql文件?

    -- MySQL dump 10.11
--
-- Host: localhost    Database: sample
-- ------------------------------------------------------
-- Server version   5.0.67-community-nt

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `awss_red_force`
--
--

DROP TABLE IF EXISTS `awss_red_force`;
SET @saved_cs_client     = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `awss_red_force` (
  `int_scenario_id` int(11) NOT NULL,
  `str_entity_name` varchar(100) default NULL,
  `str_hla_type` varchar(30) default NULL,
  `str_parent_name` varchar(100) default NULL,
  `dbl_x` double default NULL,
  `dbl_y` double default NULL,
  `dbl_z` double default NULL,
  PRIMARY KEY  (`int_scenario_id`),
  KEY `awss_red_force_ibfk_1` (`int_scenario_id`),
  CONSTRAINT `awss_red_force_ibfk_1` FOREIGN KEY (`int_scenario_id`) REFERENCES `scenario` (`int_scenario_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;

--
-- Dumping data for table `awss_red_force`
--

LOCK TABLES `awss_red_force` WRITE;
/*!40000 ALTER TABLE `awss_red_force` DISABLE KEYS */;
/*!40000 ALTER TABLE `awss_red_force` ENABLE KEYS */;
UNLOCK TABLES;

3 个答案:

答案 0 :(得分:2)

您可以使用--add-drop-table--create-options选项。

  

- 加 - 降式表

     

在每个之前添加DROP TABLE语句   CREATE TABLE语句。

mysqldump

实际上即使没有任何其他密钥,也应添加CREATE TABLE

更新:

backup.bat

的内容
C:\MySQL\MySQL Server 5.0\bin\mysqldump -uroot -ppassword sample >
                                                     D:/admindb/AAR12.sql
copy db_restore.sql+AAR12.sql restore.sql

假设您手动创建db_restore.sql并放置所有数据库范围的创建/删除操作。 backup.bat应该生成带有数据库drop / create和表数据的restore.sql。

答案 1 :(得分:0)

答案 2 :(得分:0)

private void TakeDBBackup() 
{ 
string tables = ""; 

// Displaying tables from apman database 
var cmd = 
new MySqlCommand( 
"select table_name from information_schema.tables where table_schema='apman' and table_type like '%TABLE'", 
ClientConnection); 
MySqlDataReader read2 = cmd.ExecuteReader(); 
while (read2.Read()) 

// Initializing tables into a varible string. 
tables += read2["table_name"] + " "; 
read2.Close(); 

// Displays current date and time name as folder name. 
DateTime backupTime = DateTime.Now; 
int year = backupTime.Year; 
int month = backupTime.Month; 
int day = backupTime.Day; 
int hour = backupTime.Hour; 
int minute = backupTime.Minute; 
int second = backupTime.Second; 

// Creating Dumpdata directory 
string dest = Directory.GetCurrentDirectory() + "\\Dumpdata\\"; 
Directory.CreateDirectory(dest); 
string src = dest + year + "-" + month + "-" + day + "-" + hour + "-" + minute + "-" + second + ".sql"; 
var file = new StreamWriter(src); 
string cmd1 = string.Format("--force --user=root --password=passd apman > {0} {1}", src, tables); 
var proc = new ProcessStartInfo(); 
proc.FileName = "mysqldump.exe"; 
proc.Arguments = cmd1; 
proc.RedirectStandardInput = true; 
proc.RedirectStandardOutput = true; 
proc.UseShellExecute = false; 
proc.RedirectStandardError = true; 
proc.CreateNoWindow = true; 
Process p = Process.Start(proc); 
string res = p.StandardOutput.ReadToEnd(); 
p.Start(); 
p.OutputDataReceived += (o, args) => File.AppendAllText(src, args.Data); 
file.WriteLine(res); 
//p.WaitForExit(); 
p.Close(); 
Console.WriteLine(@"Database backup is taken"); 
file.Close(); 
}