--- tags: OnSight Optical, work --- # Azure Database Scripting ## To Do - [ ] convert the sqlpackage.exe commands to sqlcmd - [x] create the script for automation (time or watch for new file?) ## Automation process - [x] create powershell commands for each step - [x] Enable Q drive - [x] Restore OCD Database - [x] Export database as bacpac - [x] deploy bacpac to azure - [x] delete old database - [x] rename new database to the old one - [x] adjust price tier to the lowest possible - [x] Automation - [x] Watch for new file and save name to variable - [x] use the new file as a trigger for the automation - [x] use the name variable to restore ## Tools These need to be installed first before running sqlcmd utility package.exe *** ```flow st=>start: OCD Backup e=>end: Azure Succesfully Updated st->e ``` ``` ############################################## ####Updates the Azure FlexsysData database#### ############################################## ##################################################################################################################### $PriorDate = (Get-Date).AddDays(-1).ToString("yyy_MM_dd") $MostRecentOCD = (Get-Item -Path Q:\\OCD_backup_$PriorDate*.bak).fullname ###################################################################### ####Makes sure the Q network drive is available for the SQL server#### ###################################################################### sqlcmd -E -S sys-admin\sqlexpress -Q "EXEC XP_CMDSHELL 'net use Q: \\osofs01\Flexsys /USER:Administrator RslMM1045010!'" ####################################################### ####Restores the Flexsys backup to the local server#### ####################################################### & sqlcmd -E -S sys-admin\sqlexpress -Q "RESTORE DATABASE OCD FROM DISK='$MostRecentOCD'" ###################################################################### ####Export the local Flexsys database to bacpac to export to Azure#### ###################################################################### & 'C:\Program Files (x86)\Microsoft SQL Server\150\DAC\bin\sqlpackage.exe' /a:Export /ssn:sys-admin\sqlexpress /sdn:OCD /tf:'Q:\\azure-imports\OCDbackup.bacpac' ######################################### ####Deletes the old database on Azure#### ######################################### sqlcmd -S onsightoptical.database.windows.net -d FlexsysData -U sqladmin -P optical4657! -Q "DROP DATABASE FlexsysData;" ################################# ####Deploys database to Azure#### ################################# & 'C:\Program Files (x86)\Microsoft SQL Server\150\DAC\bin\sqlpackage.exe' /a:import /tsn:tcp:onsightoptical.database.windows.net /tdn:FlexsysData /tu:sqladmin /tp:optical4657! /sf:'Q:\\azure-imports\OCDbackup.bacpac' /p:DatabaseEdition=Standard /p:DatabaseServiceObjective=S1 /p:DatabaseMaximumSize=2 ################################ ####Lowers the pricing tiers#### ################################ & sqlcmd -S onsightoptical.database.windows.net -d FlexsysData -U sqladmin -P optical4657! -Q "ALTER DATABASE FlexsysData MODIFY (EDITION='Standard', MAXSIZE=2 GB, SERVICE_OBJECTIVE = 'S0');" ################################################################################################################### ############################################## ####Gregory Johnson - System Administrator#### #### greg.johnson@onsightoptical.com #### ############################################## ```