Ejemplo n.º 1
0
        /// <summary>
        /// 
        /// </summary>
        public static List<ConnectionGroup> SpritToGroups(List<Connection> connections)
        {
            if (connections == null) return null;
            List<ConnectionGroup> groups = new List<ConnectionGroup>();

            foreach(Connection conn in connections)
            {
                bool isUniqueConn = true;
                // find right group
                foreach(ConnectionGroup group in groups)
                {
                    if (group.Server.Equals(conn.Server))
                    {
                        group.Add(conn);
                        isUniqueConn = false;
                        break;
                    }
                }
                // create new group
                if (isUniqueConn)
                {
                    ConnectionGroup group = new ConnectionGroup(conn);
                    groups.Add(group);
                }
            }
            return groups;
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        public static List <ConnectionGroup> SpritToGroups(List <Connection> connections)
        {
            if (connections == null)
            {
                return(null);
            }
            List <ConnectionGroup> groups = new List <ConnectionGroup>();

            foreach (Connection conn in connections)
            {
                bool isUniqueConn = true;
                // find right group
                foreach (ConnectionGroup group in groups)
                {
                    if (group.Server.Equals(conn.Server))
                    {
                        group.Add(conn);
                        isUniqueConn = false;
                        break;
                    }
                }
                // create new group
                if (isUniqueConn)
                {
                    ConnectionGroup group = new ConnectionGroup(conn);
                    groups.Add(group);
                }
            }
            return(groups);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        public static void ExecuteGroup(ConnectionGroup group, string connGroupMask, int timeout, string sqlQueryMask, string backupNameMask)
        {
            if (group == null)
            {
                return;
            }

            SqlConnection conn = new SqlConnection(String.Format(connGroupMask,
                                                                 group.Server, group.User, group.Password, "False", "False", "True"));

            // connecting to group server
            try
            {
                Log.Add("Connection to server [{0}]...", group.Server);
                conn.Open();
                Log.Add("Connection was successfull", group.Server);

                foreach (Connection connElem in group.Connections)
                {
                    FileSystem.CreateIfMissing(connElem.BackupPath);

                    string backupName      = connElem.Database + String.Format(backupNameMask, DateTime.Now);
                    string backupQueryText = String.Format(sqlQueryMask, connElem.Database, connElem.BackupPath, backupName, connElem.Database);
                    // executing sql query to database
                    try
                    {
                        Log.Add("Backup from [{0}] to [{1}]...", connElem.Database, backupName);
                        SqlCommand sqlCommand = new SqlCommand(backupQueryText, conn);
                        sqlCommand.CommandTimeout = timeout;
                        sqlCommand.Prepare();
                        sqlCommand.ExecuteNonQuery();
                        Log.Add("Query executed. Result code: {0}", sqlCommand.UpdatedRowSource);
                    }
                    catch (SqlException ex)
                    {
                        Log.Add("Error with query executing. Code: [{0}], Message: [{1}]", ex.ErrorCode, ex.Message);
                    }
                }
            }
            catch (SqlException ex)
            {
                Log.Add("Error with connection to server. Code: [{0}], Message: [{1}]", ex.ErrorCode, ex.Message);
            }
            finally
            {
                conn.Close();
                Log.Add("Connection closed");
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 
        /// </summary>
        public static void ExecuteGroup(ConnectionGroup group, string connGroupMask, int timeout, string sqlQueryMask, string backupNameMask)
        {
            if (group == null) return;

            SqlConnection conn = new SqlConnection(String.Format(connGroupMask,
                group.Server, group.User, group.Password, "False", "False", "True"));

            // connecting to group server
            try
            {
                Log.Add("Connection to server [{0}]...", group.Server);
                conn.Open();
                Log.Add("Connection was successfull", group.Server);

                foreach (Connection connElem in group.Connections)
                {
                    FileSystem.CreateIfMissing(connElem.BackupPath);

                    string backupName = connElem.Database + String.Format(backupNameMask, DateTime.Now);
                    string backupQueryText = String.Format(sqlQueryMask, connElem.Database, connElem.BackupPath, backupName, connElem.Database);
                    // executing sql query to database
                    try
                    {
                        Log.Add("Backup from [{0}] to [{1}]...", connElem.Database, backupName);
                        SqlCommand sqlCommand = new SqlCommand(backupQueryText, conn);
                        sqlCommand.CommandTimeout = timeout;
                        sqlCommand.Prepare();
                        sqlCommand.ExecuteNonQuery();
                        Log.Add("Query executed. Result code: {0}", sqlCommand.UpdatedRowSource);
                    }
                    catch(SqlException ex)
                    {
                        Log.Add("Error with query executing. Code: [{0}], Message: [{1}]", ex.ErrorCode, ex.Message);
                    }
                }
            }
            catch (SqlException ex)
            {
                Log.Add("Error with connection to server. Code: [{0}], Message: [{1}]", ex.ErrorCode, ex.Message);
            }
            finally
            {
                conn.Close();
                Log.Add("Connection closed");
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            Display.SetWindowStyle(ProcessWindowStyle.Hidden);

            // init config file (and current log file)
            //SqlBackUpperConfig config = new SqlBackUpperConfig(args);
            SqlBackUpperLibConfig config = new SqlBackUpperLibConfig(ASSEMBLY_NAME, args);

            if (!config.IsSetConfig)
            {
                OnFinish(config);
                //Log.Add("Config file is not set. Exit..");
                return;
            }

            //ProcessWindowStyle windowStyle = config.WindowStyle;
            Display.SetWindowStyle(config.WindowStyle);

            // connecting to the server & execute sql query
            List <Connection> connections = config.Connections;

            if (connections != null)
            {
                if (config.UniteSameInst)
                {
                    // use grouping by server
                    List <ConnectionGroup> groups = ConnectionGroup.SpritToGroups(connections);
                    foreach (ConnectionGroup group in groups)
                    {
                        Log.AddLine();
                        SqlQuery.ExecuteGroup(group, config.ConnectionGroupMask, config.Timeout, config.SqlQueryMask, config.BackupNameMask);
                        foreach (Connection conn in group.Connections)
                        {
                            FileSystem.DeleteOldFiles(conn.BackupPath, conn.MaxBackups, conn.Database + "*");
                        }
                    }
                }
                else
                {
                    // execute all queries successively
                    foreach (Connection conn in connections)
                    {
                        Log.AddLine();
                        string backupName = conn.Database + String.Format(config.BackupNameMask, DateTime.Now);
                        SqlQuery.Execute(conn, config.ConnectionMask, config.Timeout, config.SqlQueryMask, backupName);
                        FileSystem.DeleteOldFiles(conn.BackupPath, conn.MaxBackups, conn.Database + "*");
                    }
                }
            }
            else
            {
                Log.Add("Error in config ([Connections] == null)");
            }

            // delete extra log files
            OnFinish(config);
            //FileSystem.DeleteOldFiles(config.LogPath, config.MaxLogs, "*.log");

            //Log.Add("Finish");
            ////
            //if ((windowStyle == ProcessWindowStyle.Normal || windowStyle == ProcessWindowStyle.Maximized) && config.ReadKeyInFinish)
            //{
            //    Console.WriteLine("Press any key to exit..");
            //    Console.ReadKey();
            //}
        }