Esempio n. 1
0
        public void Backup(BackupParameters parameters)
        {
            var server = new Server(parameters.Server);
            try
            {
                server.ConnectionContext.LoginSecure = true;
                server.ConnectionContext.Connect();

                var backup = new Backup
                {
                    Action = BackupActionType.Database,
                    Database = parameters.DatabaseName
                };

                var backupPath = GenerateBackupPath(parameters);
                Logger.WriteLine("Backing up to {0}", backupPath);
                backup.Devices.AddDevice(backupPath, DeviceType.File);
                backup.BackupSetName = string.Format("{0} backup", parameters.DatabaseName);
                backup.PercentComplete += HandlePercentComplete;
                backup.Complete += HandleComplete;
                Logger.WriteLine("Running backup...");
                backup.SqlBackup(server);
            }
            finally
            {
                if (server.ConnectionContext.IsOpen)
                {
                    Logger.Write("Closing connection...");
                    server.ConnectionContext.Disconnect();
                    Logger.WriteLine("OK");
                }
            }
        }
Esempio n. 2
0
 private string GenerateBackupPath(BackupParameters parameters)
 {
     if (parameters.FilePath.IsNotNullOrEmpty())
     {
         return Path.IsPathRooted(parameters.FilePath) ?
             parameters.FilePath :
             Path.Combine(Settings.BackupDirectory, parameters.FilePath);
     }
     var filename = string.Format("{0}_{1}.bak",
         parameters.DatabaseName,
         DateTime.Now.ToString("yyyyMMdd_hhmmss"));
     return Path.Combine(Settings.BackupDirectory, filename);
 }
Esempio n. 3
0
        public override void DoExecute(CommandArgs args)
        {
            var connectionData = Settings.GetConnection(args.Arguments[0]);
            var parameters = new BackupParameters
                {
                    DatabaseName = connectionData.Name,
                    FilePath = args.Arguments[1],
                    Server = connectionData.Host
                };

            var restoreTask = TaskFactory.CreateRestoreTask(connectionData);
            restoreTask.PercentComplete += PrintPercentage;
            restoreTask.Complete += TaskComplete;
            restoreTask.Restore(parameters);
        }
Esempio n. 4
0
        public void Restore(BackupParameters parameters)
        {
            var databaseName = parameters.DatabaseName;
            var filePath = GetFullPathFrom(parameters.FilePath);

            Logger.WriteLine("Restoring {0} from file {1}", databaseName, filePath);
            var server = new Server(parameters.Server);
            try
            {
                server.ConnectionContext.LoginSecure = true;
                server.ConnectionContext.Connect();
                var restore = new Restore { Database = databaseName, Action = RestoreActionType.Database };
                restore.Devices.AddDevice(filePath, DeviceType.File);
                restore.ReplaceDatabase = true;

                var database = server.Databases[databaseName];
                if (database == null)
                {
                    Logger.WriteLine("Database {0} does not exist", databaseName);
                    return;
                }

                var users = server.GetLoginUsers(database);
                Logger.WriteLine("User mappings to restore: {0}", string.Join(",", users));

                RelocateFiles(database, restore);

                server.RenewConnection();

                restore.PercentComplete += HandlePercentComplete;
                restore.Complete += HandleComplete;
                restore.SqlRestore(server);

                RestoreUserMappings(server, users, databaseName);
            }
            finally
            {
                if (server.ConnectionContext.IsOpen)
                {
                    Logger.Write("Closing connection...");
                    server.ConnectionContext.Disconnect();
                    Logger.WriteLine("OK");
                }
            }
        }