GetLastBackupTime() 공개 정적인 메소드

public static GetLastBackupTime ( SqlExecutor dbExecutor, string backupNamePrefix ) : System.DateTime
dbExecutor SqlExecutor
backupNamePrefix string
리턴 System.DateTime
예제 #1
0
        public override void ExecuteCommand()
        {
            Log.Trace("Connecting to server '{0}' to back up database '{1}'.", ConnectionString.InitialCatalog, Util.GetDatabaseServerName(ConnectionString));

            _startedBackup = false;

            var cstr = Util.GetMasterConnectionString(ConnectionString.ConnectionString);

            using (var connection = new SqlConnection(cstr))
                using (var db = new SqlExecutor(connection))
                {
                    connection.Open();

                    if (!Force)
                    {
                        Log.Trace("Checking for a backup in progress.");
                        if (Util.BackupIsInProgress(db, BackupNamePrefix))
                        {
                            Log.Trace("Found a backup in progress; exiting.");
                            return;
                        }

                        Log.Trace("Found no backup in progress.");

                        Log.Trace("Getting last backup time.");
                        var lastBackupTime = Util.GetLastBackupTime(db, BackupNamePrefix);
                        if (lastBackupTime >= DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(IfOlderThan)))
                        {
                            Log.Info("Skipping Backup. Last Backup was less than {0} minutes ago", IfOlderThan);
                            return;
                        }
                        Log.Trace("Last backup time is more than {0} minutes ago. Starting new backup.", IfOlderThan);
                    }
                    else
                    {
                        Log.Trace("Forcing new backup");
                    }

                    // Generate a backup name
                    var timestamp = Util.GetTimestamp();

                    _backupName = BackupNamePrefix + timestamp;

                    if (!WhatIf)
                    {
                        db.Execute(string.Format("CREATE DATABASE {0} AS COPY OF {1}", _backupName, ConnectionString.InitialCatalog));
                        _startedBackup = true;
                    }

                    Log.Info("Started Copy of '{0}' to '{1}'", ConnectionString.InitialCatalog, _backupName);
                }
        }