/* 整库备份: mongodump -h dbhost -d dbname -o dbdirectory -h:MongDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017 -d:需要备份的数据库实例,例如:test -o:备份的数据存放位置,例如:c:\data\dump,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。 */ //backup private void backipToolStripMenuItem_Click(object sender, EventArgs e) { TreeNode node = SelectedNode; if (node == null) return; DB db = null; string tbName = null; if (node.SelectedImageIndex == 1) { //db TreeNode root = node.Parent; db = root.Tag as DB; db.dbname = node.Name; } else if (node.SelectedImageIndex == 3) { //collection TreeNode root = node.Parent.Parent; db = root.Parent.Tag as DB; db.dbname = root.Name; tbName = node.Name; } if (db != null) { OperDB d = new OperDB(); d.server = db.server; d.port = db.port; d.username = db.username; d.password = db.password; d.dbname = db.dbname; d.collection = tbName; d.backup = true; d.auth = db.auth(); ConfirmForm form = new ConfirmForm(d); if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK) { } } }
public ConfirmForm(OperDB db) { InitializeComponent(); this.db = db; }
private void mongoexec(OperDB db) { string dumppath = string.Empty; try { dumppath = GetWinServiceInstallPath("MongoDB"); dumppath = dumppath.Substring(0, dumppath.LastIndexOf("\\mongod.exe")); } catch { } if (string.IsNullOrEmpty(dumppath)) { dumppath = AppDomain.CurrentDomain.BaseDirectory + "mongo"; } string exe = db.backup ? "mongodump.exe" : "mongorestore.exe"; if (string.IsNullOrEmpty(dumppath) || !File.Exists(string.Format("{0}\\{1}", dumppath, exe))) { throw new Exception(""); } string args = string.Empty; if (db.backup) { args = string.Format("-h {0}:{1} -d {2} -o {3}", db.server, db.port, db.dbname, db.folder); } else { args = string.Format("-h {0}:{1} -d {2} -dir {3}", db.server, db.port, db.dbname, db.folder); } if (!string.IsNullOrEmpty(db.collection)) { args += string.Format(" -c {0}", db.collection); } if (db.auth) { args += string.Format(" -u {0} -p {1}", db.username, db.password); } try { //Directory.Delete("", true); } catch { } try { Program.WriteLog(dumppath); Program.WriteLog(exe); Program.WriteLog(args); Process p = new Process(); p.StartInfo.FileName = exe;//"mongodump.exe"; p.StartInfo.WorkingDirectory = dumppath; p.StartInfo.Arguments = args; //p.StartInfo.CreateNoWindow = false; //p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.Start(); p.WaitForExit(); p.Close(); } catch (Exception ex) { MessageBox.Show("failed :" + ex.Message); return; } if (db.backup) { string msg = "Backup success,Do you need to open the folder?"; if (MessageBox.Show(msg, "Success", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == System.Windows.Forms.DialogResult.Yes) { if (!Directory.Exists(db.folder)) Directory.CreateDirectory(db.folder); System.Diagnostics.Process.Start("explorer.exe", db.folder); } } else { MessageBox.Show("Restore success."); } this.Close(); }