private void copyJobs(Server destserver, bool disableonsource, bool disableondest, bool dropdest) { JobCollection destjobs = destserver.JobServer.Jobs; foreach (Job job in sourceserver.JobServer.Jobs) { string jobname = job.Name; ItemToCopy item = itemsToCopy.Find(x => x.Name == jobname); if (item.IsChecked) { if (!DBChecks.LoginExists(destserver, job.OwnerLoginName)) { showOutput.displayOutput(string.Format("[Job: {0}] Owner {1} doesn't exist ion destination. Skipping. ", jobname, job.OwnerLoginName)); continue; } foreach (JobStep jobstep in job.JobSteps) { if (!DBChecks.DatabaseExists(destserver, jobstep.DatabaseName)) { showOutput.displayOutput(string.Format("[Job: {0}] Database {1} doesn't exist on destination. Skipping. ", jobname, jobstep.DatabaseName)); continue; } if (!string.IsNullOrEmpty(jobstep.ProxyName) && !DBChecks.ProxyExists(destserver, jobstep.ProxyName)) { showOutput.displayOutput(string.Format("[Job: {0}] Proxy {1} doesn't exist on destination. Skipping. ", jobname, jobstep.ProxyName)); continue; } } if (!string.IsNullOrEmpty(job.OperatorToEmail) && !DBChecks.OperatorExists(destserver, job.OperatorToEmail)) { showOutput.displayOutput(string.Format("[Job: {0}] Operator {1} doesn't exist on destination. Skipping. ", jobname, job.OperatorToEmail)); continue; } if (!string.IsNullOrEmpty(job.OperatorToNetSend) && !DBChecks.OperatorExists(destserver, job.OperatorToNetSend)) { showOutput.displayOutput(string.Format("[Job: {0}] Operator {1} doesn't exist on destination. Skipping. ", jobname, job.OperatorToNetSend)); continue; } if (!string.IsNullOrEmpty(job.OperatorToPage) && !DBChecks.OperatorExists(destserver, job.OperatorToPage)) { showOutput.displayOutput(string.Format("[Job: {0}] Operator {1} doesn't exist on destination. Skipping. ", jobname, job.OperatorToPage)); continue; } if (DBChecks.JobExists(destserver, jobname)) { if (!dropdest) { showOutput.displayOutput(string.Format("Job: {0} already exists on destination server. Skipping. ", jobname)); continue; } else { try { destserver.JobServer.Jobs[jobname].Drop(); destserver.JobServer.Refresh(); } catch (Exception ex) { showOutput.displayOutput(ex.Message); continue; } } } try { StringCollection sql = job.Script(); for (int i = 0; i < sql.Count; i++) { sql[i] = sql[i].Replace(sourceserver.Name, destserver.Name); } destserver.ConnectionContext.ExecuteNonQuery(sql); if (disableondest) { DBFunctions.ChangeJobStatus(destserver, jobname, false); } if (disableonsource) { DBFunctions.ChangeJobStatus(sourceserver, jobname, false); } showOutput.displayOutput(string.Format("Copied job {0} to {1}", jobname, destserver.Name)); } catch (Exception ex) { showOutput.displayOutput(string.Format("Failed to copy job {0} to {1}", jobname, destserver.Name)); showOutput.displayOutput(ex.Message); continue; } } } }
private void syncDatabasePerms(Login sourcelogin, Login destlogin, Server sourceserver, Server destserver) { // Remove user from destination if it does not exist on source if (destlogin.EnumDatabaseMappings() != null) { foreach (DatabaseMapping dbmap in destlogin.EnumDatabaseMappings()) { string dbname = dbmap.DBName; Database destdb = destserver.Databases[dbname]; Database sourcedb = sourceserver.Databases[dbname]; string dbusername = dbmap.UserName; string dbloginname = dbmap.LoginName; if (DBChecks.DatabaseExists(sourceserver, destdb.Name) && !DBChecks.DatabaseUserExists(sourcedb, dbusername) && DBChecks.DatabaseUserExists(destdb, dbusername)) { try { DBFunctions.DropDBUser(sourcedb, destdb, dbusername); } catch (Exception ex) { showOutput.displayOutput(string.Format("Failed to drop user {0} From {1} on destination.", dbusername, dbname), true); showOutput.displayOutput(ex.Message); } try { DBFunctions.RevokeDBPerms(sourcedb, destdb, dbusername); } catch (Exception ex) { showOutput.displayOutput(string.Format("Failed to revoke permission for user {0} on {1}.", dbusername, dbname), true); showOutput.displayOutput(ex.Message, true); } } } } // Add the database mappings and permissions { if (sourcelogin.EnumDatabaseMappings() != null) { foreach (DatabaseMapping dbmap in sourcelogin.EnumDatabaseMappings()) { string dbname = dbmap.DBName; Database destdb = destserver.Databases[dbname]; Database sourcedb = sourceserver.Databases[dbname]; string dbusername = dbmap.UserName; string dbloginname = dbmap.LoginName; // Only if database exists on destination and its status is normal if (DBChecks.DatabaseExists(destserver, sourcedb.Name) && DBChecks.LoginExists(destserver, dbloginname) && !DBChecks.DatabaseUserExists(destdb, dbusername) && destdb.Status == DatabaseStatus.Normal) { // Add DB User try { DBFunctions.AddDBUser(destdb, dbusername); } catch (Exception ex) { showOutput.displayOutput(string.Format("Failed to add user {0} to database {1}", dbusername, dbname), true); showOutput.displayOutput(ex.Message, true); } //Change the owner if (sourcedb.Owner == dbusername) { DBFunctions.ChangeDbOwner(destserver, null, dbusername, dbname); } //Map the roles try { DBFunctions.AddUserToDBRoles(sourcedb, destdb, dbusername); } catch (Exception ex) { showOutput.displayOutput(string.Format("Error adding user {0} to role on database {1}", dbusername, dbname), true); showOutput.displayOutput(ex.Message, true); } //Map permissions try { DBFunctions.GrantDBPerms(sourcedb, destdb, dbusername); } catch (Exception ex) { showOutput.displayOutput(string.Format("Error granting permission for user {0} on database {1}", dbusername, dbname), true); showOutput.displayOutput(ex.Message, true); } } showOutput.displayOutput(string.Format("Database permissions synced for user {0} on database {1}", dbusername, dbname)); } } } }