Example #1
0
        public static void BackupDatabase(string server, string database, string repository, Builder definition)
        {
            BackupFile f = new BackupFile(repository, database);

            f.DeleteIfExists();
            f.AppendLine("<?xml version=\"1.0\"?>");
            f.AppendLine("<" + database + ">");

            foreach (TableDefn t in definition.Tables)
            {
                BackupTable(t.TableName, f, t);
            }

            f.AppendLine("</" + database + ">");

            Tools.Zip(f.ChangeExt(f.FileNameToday, "zip"), f.FileNameToday);
            File.Delete(f.FileNameToday);
        }
Example #2
0
        public static void RestoreDatabase(string server, string database, string repository, Builder definition, string backup)
        {
            ExecSQL("USE " + database);

            // Confirm that tables are empty
            foreach (TableDefn t in definition.Tables)
            {
                if (HasContent(t.TableName))
                {
                    Console.WriteLine("Cannot restore database " + database + " some of its tables have content");
                    return;
                }
            }

            Hashtable hash = new Hashtable();

            BackupFile f = new BackupFile(repository, database);

            string archive = null;

            if (backup == null)
            {
                archive = f.ArchiveLatest;
            }
            else
            {
                if (backup.Right(4) != ".zip")
                {
                    Console.WriteLine("Cannot restore database " + database + " from " + backup + " - it must be a zip file");
                    return;
                }
                archive = repository + Tools.PathDelimiter + backup;
                if (!File.Exists(archive))
                {
                    Console.WriteLine("Cannot restore database " + database + " from " + archive + " - it isn't there!");
                    return;
                }
                if (File.Exists(f.ChangeExt(archive, "xml")))
                {
                    Console.WriteLine("Cannot restore database " + database + " from " + archive + " - xml file is also in repository.");
                    return;
                }
            }

            Console.WriteLine($"Using {archive}");

            string filename = f.ChangeExt(archive, "xml");

            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            Tools.UnZip(archive, repository);
            if (!File.Exists(filename))
            {
                Console.WriteLine("Sleeping for 1000msecs");
                Tools.Sleep(1000);
                if (!File.Exists(filename))
                {
                    throw new Exception("Backup " + filename + " not in " + archive);
                }
            }

            bool inDatabase = false;

            using (XmlReader rdr = XmlReader.Create(f.OpenStream(filename)))
            {
                while (rdr.Read())
                {
                    if (rdr.NodeType == XmlNodeType.XmlDeclaration && !inDatabase && rdr.Name == "xml")
                    {
                    }
                    else if (rdr.NodeType == XmlNodeType.Whitespace)
                    {
                    }
                    // Confirm we have the Database start node
                    else if (rdr.NodeType == XmlNodeType.Element && !inDatabase && rdr.Name == database)
                    {
                        inDatabase = true;
                    }
                    else if (rdr.NodeType == XmlNodeType.Element && inDatabase)
                    {
                        TableDefn table = definition.FindTable(rdr.Name);
                        if (table == null)
                        {
                            Console.WriteLine("Could not find table: " + rdr.Name);
                            System.Environment.Exit(0);
                        }
                        Console.WriteLine("Starting to restore table " + table.TableName);
                        bool idCol = table.HasIdentityColumn && Connection.Me.Type != "MySQL";
                        RestoreTable(rdr, table, idCol, hash);
                    }
                    else if (rdr.NodeType == XmlNodeType.EndElement && inDatabase && rdr.Name == database)
                    {
                        // Have found the Database end node
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Unexpected node: " + rdr.Name + " type: " + rdr.NodeType.ToString());
                        System.Environment.Exit(0);
                    }
                }
            }

            File.Delete(filename);
        }