BackupData() public static method

Create a backup file This is very useful function in case you need to overwrite some file. In that case there is a chance for program to crash during the write and this would left the file corrupted, this function will create a copy of a file using config.tempName() which later needs to be deleted (after you finish your write operation). The file that is to be backed up doesn't need to exist, if it's not present the function just return false
public static BackupData ( string name ) : bool
name string Full path of a file that you want to make a backup of
return bool
Example #1
0
 private void FlushRows()
 {
     if (Recovering)
     {
         return;
     }
     // prevent multiple threads calling this function at same time
     lock (this)
     {
         string file = Variables.ConfigurationDirectory + Path.DirectorySeparatorChar + "unwrittensql.xml";
         if (File.Exists(file))
         {
             Core.BackupData(file);
             if (!File.Exists(Configuration.TempName(file)))
             {
                 Syslog.WarningLog("Unable to create backup file for " + file);
                 return;
             }
         }
         try
         {
             File.Delete(file);
             XmlSerializer xs     = new XmlSerializer(typeof(Unwritten));
             StreamWriter  writer = File.AppendText(file);
             lock (unwritten)
             {
                 xs.Serialize(writer, unwritten);
             }
             writer.Close();
             if (File.Exists(Configuration.TempName(file)))
             {
                 File.Delete(Configuration.TempName(file));
             }
         } catch (Exception fail)
         {
             Core.HandleException(fail);
             Syslog.WarningLog("Recovering the mysql unwritten dump because of exception to: " + file);
             Core.RecoverFile(file);
         }
     }
 }
Example #2
0
        /// <summary>
        /// Save config
        /// </summary>
        public void SaveConfig()
        {
            string fn = GetConfigFilePath();

            try
            {
                XmlDocument data    = new XmlDocument();
                XmlNode     xmlnode = data.CreateElement("channel");
                InsertData("talkmode", Suppress.ToString(), ref data, ref xmlnode);
                InsertData("langcode", Language, ref data, ref xmlnode);
                InsertData("respond_message", RespondMessage.ToString(), ref data, ref xmlnode);
                InsertData("ignore-unknown", IgnoreUnknown.ToString(), ref data, ref xmlnode);
                InsertData("suppress-warnings", SuppressWarnings.ToString(), ref data, ref xmlnode);
                InsertData("respond_wait", RespondWait.ToString(), ref data, ref xmlnode);
                InsertData("sharedinfo", SharedDB, ref data, ref xmlnode);
                if (!String.IsNullOrEmpty(this.Password))
                {
                    InsertData("password", Password, ref data, ref xmlnode);
                }
                InsertData("defaultbot", DefaultInstance, ref data, ref xmlnode);
                if (!(SharedLinkedChan.Count < 1))
                {
                    foreach (Channel current in SharedLinkedChan)
                    {
                        InsertData("name", current.Name, ref data, ref xmlnode, "sharedch");
                    }
                }

                if (!(Infobot_IgnoredNames.Count < 1))
                {
                    foreach (string curr in Infobot_IgnoredNames)
                    {
                        InsertData("name", curr, ref data, ref xmlnode, "ignored");
                    }
                }

                if (ExtensionData.Count > 0)
                {
                    foreach (KeyValuePair <string, string> item in ExtensionData)
                    {
                        InsertData(item.Key, item.Value, ref data, ref xmlnode, "extension");
                    }
                }

                lock (this.SystemUsers.Users)
                {
                    foreach (SystemUser user in this.SystemUsers.Users)
                    {
                        XmlAttribute name = data.CreateAttribute("regex");
                        name.Value = user.Name;
                        XmlAttribute kk = data.CreateAttribute("role");
                        kk.Value = user.Role;
                        XmlNode db = data.CreateElement("user");
                        db.Attributes.Append(name);
                        db.Attributes.Append(kk);
                        xmlnode.AppendChild(db);
                    }
                }
                if (File.Exists(fn))
                {
                    Core.BackupData(fn);
                    if (!File.Exists(Configuration.TempName(fn)))
                    {
                        Syslog.WarningLog("Unable to create backup file for " + Name);
                    }
                }
                data.AppendChild(xmlnode);
                data.Save(fn);
                if (File.Exists(Configuration.TempName(fn)))
                {
                    File.Delete(Configuration.TempName(fn));
                }
            }
            catch (Exception)
            {
                Core.RecoverFile(fn, Name);
            }
        }