SlideShare a Scribd company logo
1 of 42
‫‪ Powershell‬להמונים‬
    ‫אביעד דרעי | ‪ | DBA‬רפאל‬
‫קצת על רפאל...‬     ‫•‬
                 ‫קצת עליי...‬   ‫•‬
           ‫איך הכל התחיל?‬      ‫•‬
              ‫איפה הבעייה?‬     ‫•‬
         ‫• הדרישות עבור הפתרון‬
   ‫מה זה בעצם ‪?Powershell‬‬      ‫•‬
  ‫מה מאפשר ה-‪?Powershell‬‬       ‫•‬
‫השילוב של ‪SSIS+Powershell‬‬      ‫•‬
            ‫סיכום + שאלות‬      ‫•‬
‫קצת על רפאל...‬

     ‫• מפתחת, מייצרת ומספקת מגוון מערכות‬
     ‫לחימה מתקדמות אשר תורמות למערכת‬
                        ‫הביטחון יכולות:‬
                                   ‫– בים‬
                                ‫– באוויר‬
                                ‫– ביבשה‬
‫• רווחים בשנת 0102 – למעלה מ 8.1 מיליארד ₪‬
                    ‫• למעלה מ-0005 עובדים‬
‫קצת עליי...‬

‫• בוגר החוג למערכות מידע באוניברסיטת חיפה‬
               ‫• ‪ DBA‬תשתיתי כ-5.4 שנים:‬
                  ‫– 2‪MS SQL 2000-2008R‬‬

                         ‫– ‪Oracle 9i-11g‬‬

                     ‫– 01 ‪SQL Anywhere‬‬

                              ‫– ‪Sybase‬‬
‫באחריותי...‬
‫תחזוקה שוטפת של למעלה מ-001 שרתים במערכות מרכזיות.‬         ‫•‬
          ‫פיתוח, הדרכה והטמעה של מגוון שירותים וכלים:‬      ‫•‬
                                 ‫– ‪Reporting Services‬‬
                                ‫– ‪Integration Services‬‬
                                              ‫– ‪Spatial‬‬
                                         ‫– ‪PowerShell‬‬
                      ‫– ‪ Mirroring, Replication‬ועוד...‬
                          ‫שיפור ביצועים וכיוונון שאילתות‬   ‫•‬
                           ‫ייעוץ וליווי פרוייקטים ברפא"ל‬   ‫•‬
                             ‫אבטחת מידע בבסיסי נתונים‬      ‫•‬
‫איך הכל התחיל?‬
‫• מספר מצומם של שרתים, על כל שרת מספר קבצי‬
‫‪ batch‬וסקריפטים עבור גיבויים, תחזוקה והגדרות‬
                     ‫שונות של בסיסי הנתונים.‬
          ‫• בכל סקריפט קיימות הגדרות של שרת‬
  ‫הדואר,השו"ב ושרת התזמון המרכזי ממנו רצים‬
                                ‫הסקריפטים.‬
‫אז איפה הבעיה?‬
‫• מספר השרתים הולך ועולה משבוע לשבוע‬
 ‫• כשמקימים שרת חדש, מעתיקים אליו את הסקריפטים‬
                ‫ומשנים את משתני הסביבה השונים‬
          ‫• נוצר מצב של קושי בתחזוקת הסקריפטים:‬
         ‫– כל שינוי הכי קטן דורש מעבר על מאות סקריפטים‬
    ‫– מסקריפט אחד ראשי נוצרו עשרות עותקים וקשה לעקוב‬
                                            ‫אחריהם‬
‫• חוסר תאימות של הסקריפטים השונים ומספרם ההולך‬
       ‫וגדל גורם לכך שזמן יקר הולך לאיבוד‬
‫אז מה הפתרון?‬
‫• צריך למצוא פתרון אחר לתחזוקה השוטפת שיענה‬
                       ‫על הדרישות הבאות:‬
        ‫– יהיה עותק אחד ויחיד שירוץ ממקום מרכזי‬

          ‫– במידת האפשר יאחד את כל הסקריפטים‬

  ‫– יאפשר הוספת שרתים חדשים/גריעת שרתים ישנים‬
                              ‫באופן דינאמי וקל‬
‫– ידרוש מינימום תחזוקה ויאפשר אותה בקלות‬

‫– יאפשר גיבויי מקומי של בסיסי הנתונים על השרתים‬

            ‫– ירכז את הלוגים השונים למקום מרכזי‬

  ‫– במידת האפשר יהיה חינמי ולא תלוי במוצר מדף‬
?POWERSHELL ‫אז מה זה בעצם‬
‫• כלי שורת פקודה של מיקרוסופט, המרחיב את שורת‬
  ‫הפקודה הסטנדרטית (‪ )cmd‬ומעניק לו יכולות תכנות‬
                                      ‫נרחבות יותר‬
‫• משולב כחלק אינטגרלי בקו המוצרים האחרון כגון: ‪SQL‬‬
‫8002 ‪2008, Exchange 2007, Win 7, Win Server‬‬
                                              ‫ועוד‬
 ‫• אידאלי לאוטומציה של תהליכים שונים לניהול ותחזוקת‬
                              ‫שרתים ותחנות עבודה‬
ps1 :‫• סיומת קבצי הסקריפט‬
‫ שבנויים בצורת‬cmdlets (command-lets)‫• מורכב לרוב מ‬
                                 :‫ למשל‬verb-noun
–Get-help
–Write-host
–Get-content
–Copy-item
          -‫– על כל פקודה ניתן לקבל עזרה ופירוט ע"י שימוש ב‬
Get-help cmd-lets -detailed
?Powershell-‫מה מאפשר ה‬
            '‫• יכולות תכנות נרחבות כמו שימוש בפונקציות, לולאות, תנאים וכו‬
foreach ($db in $Srv.Databases)
   {
   $msg = $db.Name | Out-File $LogFile -Append
   $msg = " Status is: "+ $db.Status.ToString() | Out-File
   $LogFile -Append
   $msg = " Mirorring Status is: "+
   $db.MirroringStatus.ToString()| Out-File $LogFile -Append
   if ((-not $db.Status.ToString().Equals("Normal")) -and
   ($db.IsMirroringEnabled) -and ( $db.MirroringStatus -ne 5))
          {$DBStatus=1}
   }
‫עבודה אל מול שרתים ותחנות עבודה מרוחקות‬

begin
{
    $BckDir = $BckDir.Substring(0,$BckDir.Length -1 )
            –replace (":","$")
    if (Test-Path "$ServerName$BckDirfull_backup")
    {Get-ChildItem
            -Path "$ServerName$BckDirfull_backup"
            | Remove-Item -Recurse}
    return 0
}
‫הרחבה ע"י מחלקות ואובייקטים שונים למשל‬
                             .Net/Com/WMI
$Srv=New-Object
  "Microsoft.SqlServer.Management.Smo.Serv
  er" "$ServerName“
$SmtpClient = new-object
  system.net.mail.smtpClient
$MailMessage = New-Object
  system.net.mail.mailmessage
[System.Reflection.Assembly]::LoadWithPartial
  Name
  ("Microsoft.SharePoint")
Powershell-‫הקריאה ל‬
 ‫ במידה ורוצים להפעיל‬powershell.exe ‫• הפעלת סקריפט מתבצעת ע"י‬
   ‫ במידה‬sqlps.exe ‫, או ע"י‬PS ‫ולהשתמש באובייקטים סנדרטיים של‬
                      .SQL Server ‫ורוצים להשתמש בהרחבות עבור‬
    .‫• ניתן להרחיב את השפה ע"י קריאה לקובצי קונפיגורציה חיצוניים‬
             :NetApp Snap Manager for SQL Server ‫למשל עבור‬
• powershell -PSConsoleFile "D:Program
  FilesNetAppSnapManager for SQL ServerSmsqlShell.psc1"
  "&'%ScriptDir%maintian.ps1' Backup %ServerName% %LogDir%
  full %FullBackupDir% 1“
  ‫ ע"י‬SnapManager ‫• לאחר הקריאה להרחבה ניתן להפעיל פקודות של‬
                                                       :PS
• New-Backup -LogBkup -bkupsif -verify -truncatelogs -
  GenericNaming -retainbackups 7 -retainsnapofsnapinfo 7 | Out-File
  $log
‫קישורים מעניינים‬

• PowerShell wiki:
  http://en.wikipedia.org/wiki/Windows_PowerShell
• Why This SQL Server DBA is Learning
  Powershell by Ron Dameron
  http://www.simple-talk.com/sql/database-
  administration/why-this-sql-server-dba-is-
  learning-powershell/
• SQLPSX Project on Codeplex
  http://sqlpsx.codeplex.com/
‫דוגמאות בסיסיות‬
‫אז מה עושים עם ה-‪ POWERSHELL‬הזה?‬
‫• מבצע גיבויים‬

‫• מבצע בדיקת חיות ותקינות בסיסית של בסיסי הנתונים‬
                                ‫בשרת כל 5 דקות‬
                   ‫• מוריד ומעלה ‪ services‬בשרתים‬
                                       ‫• מריץ ‪SSIS‬‬
‫• במקרה של כשל, מכין דוח ‪ html‬ושולח אותו למפעילים‬
‫• פעם ביום עובר על כל בסיסי הנתונים בשרת ומשנה את‬
  ‫ההגדרות שלהם כך שיתאימו לתקנים ולשיטת העבודה‬
                                             ‫ברפאל‬

 ‫• פעם בשבוע ביצוע רה-ארגון לבסיסי הנתונים כולל ארגון‬
‫ובנייה מחדש של האינדקסים, כיווץ בסיסי הנתונים, עדכון‬
                 ‫סטטיסטיקות, מחיקת היסטוריות ועוד‬
‫• שליחת מיילים במקרה של כשל, כולל צירוף קובץ הלוג של‬
                                  ‫הפעולה שנכשלה‬

                         ‫• מחיקת קבצים ישנים לפי גיל‬

       ‫• ביצוע פעולות מותאמות על שרתים ובסיסי נתונים‬
                                   ‫מיוחדים/רגישים‬

‫• שליחת ‪ Traps‬למערכת השו"ב המרכזית ולמערכת התזמון‬
                                         ‫המרכזית‬
‫המימוש אצלנו‬
‫‪SQL Server Integration Services -SSIS‬‬


‫• מבוססים על טכולוגית ה-‪ .Net‬ומחליפים את ה-‬
                 ‫‪ DTS‬החל מגרסת 5002 ‪SQL‬‬
‫• משמשים למעבר נתונים בין פלטפורומות שונות‬
 ‫תוך הפעלת לוגיקות שונות על הנתונים במהלך‬
                                      ‫המעבר‬
‫• משמשים גם לביצוע פעולות תחזוקה שונות כמו‬
           ‫גיבויים, רה-ארגון של אינדקסים וכו'.‬
‫גיבוי באמצעות ‪ ssis‬ו-‪powershell‬‬

‫• לצורך הפתרון בניתי ‪ SSIS‬חדש מסוג ‪SQLDBMaint‬‬
‫• כל פרמטר שניתן היה – הומר לפרמטר שמתקבל באופן‬
    ‫דינאמי משורת הפקודה. למשל: מיקום הגיבויים,סוג‬
                    ‫הגיבויים, זמן שמירת הגיבויים ועוד‬
‫מבנה הקבצים והתיקיות‬

‫• תיקייה מרכזית על השרת הראשי, בה יושב סקריפט‬
                          ‫ה-‪ Powershell‬הבודד‬
  ‫• לכל שרת מנוהל יש תת תיקיה ובתוכה קבצים עם‬
    ‫הגדרות עבור השרת ותיקיית ‪ log‬עבור הלוגים‬
‫איך עובד התהליך?‬

‫• מערכת התזמון המרכזית מפעילה קובץ ‪ batch‬בשעה‬
               ‫מסויימת. בדוגמא הזו נציג גיבוי לוג‬
?‫מה בעצם קורה‬

‫ שנראה‬dmp_log.bat ‫• הקובץ שמורץ למעשה הוא הקובץ‬
                                             :‫כך‬

• call %~dp0db_env.bat

• sqlps "&'%ScriptDir%maintian.ps1' Backup
  %ServerName% %LogDir% log
  %logBackupDir% 3"
Db_env.bat

‫ מכיל פרמטרים שמשתנים משרת לשרת‬db_env-‫• קובץ ה‬
                   Powershell-‫ונקראים לפני הרצת ה‬
set ServerName=SERVERNAME
set FullBackupDir=E:mssql_dump
set LogBackupDir=F:mssql_dump
set DiffBackupDir=G:mssql_dump
set LogDir=D:jobsmssql%ServerName%log
set ScriptDir=D:jobsmssql
‫עיבוד המשתנים‬
‫העברת המשתנים לפונקצייה המתאימה‬
‫בניית שורת הפקודה‬

      ‫ עם‬PS‫) נבנתה בתוך קובץ ה‬dtexec( ‫• שורת הפקודה‬
           ‫הפרמטרים שנשלחו בהתאם לשרתים השונים‬
$strSsis = 'dtexec /DTS "MSDBMaintenance PlansBackup" '
$strSsis +='/SET "package.Variables[User::BackupPath].Value;$BckPath" '
$strSsis +='/SET "package.Variables[User::BackupType].Value;$BckType" '
$strSsis +='/SET "package.Variables[User::BackupTypeID].Value;$BckTypeID" '
$strSsis +='/SET "package.Variables[User::Extension].Value;$Ext" '
$strSsis +='/SET "package.Variables[User::IsDifferntial].Value;$IsDif" '
$strSsis +='/SET "package.Variables[User::ServerName].Value;$Server" '
# Execute the sring and save the log to file
Invoke-Expression $strSsis | Out-File $LogFile
‫ניטור הפעולה‬
‫דוגמאות נוספות‬
‫אז מה בעצם הרווחתי?‬

 ‫איחוד של מספר קבצי תחזוקת מערכת לקובץ‬       ‫•‬
                                 ‫אחד גנרי‬
         ‫ריכוז כל ההרצות למקום מרכזי אחד‬     ‫•‬
           ‫ריכוז כל הלוגים למקום מרכזי אחד‬   ‫•‬
        ‫שמירה על אחידות של בסיסי הנתונים‬     ‫•‬
‫תחזוקה קלה, פשוטה ומהירה שחוסכת עשרות‬        ‫•‬
                              ‫שעות עבודה‬
‫זמן לעשות חיים :)‬
‫‪ PS‬מאפשר יכולות חדשות לניהול תחנות עבודה ושרתים‬      ‫•‬
‫‪ PS‬מעניק גמישות רחבה יותר (לעומת ‪)...VBS/Batchs‬‬      ‫•‬
                                      ‫‪ PS‬קל לשימוש‬   ‫•‬
                                 ‫הטמעה מהירה וקלה‬    ‫•‬
      ‫שימוש ב-‪ PS‬חוסך זמן יקר (תחזוקה, עדכונים...)‬   ‫•‬
6   sql explorer - powershell dba

More Related Content

Viewers also liked

myVR presentation at HXGN Live 2013
myVR presentation at HXGN Live 2013myVR presentation at HXGN Live 2013
myVR presentation at HXGN Live 2013Olav Sylthe
 
Xvision Field Activity Planner in Maritimt Magasin no 9
Xvision Field Activity Planner in Maritimt Magasin no 9Xvision Field Activity Planner in Maritimt Magasin no 9
Xvision Field Activity Planner in Maritimt Magasin no 9Olav Sylthe
 
A vida segundo o Espírito - Lição 07 - 2º Trimestre de 2016
A vida segundo o Espírito - Lição 07 - 2º Trimestre de 2016A vida segundo o Espírito - Lição 07 - 2º Trimestre de 2016
A vida segundo o Espírito - Lição 07 - 2º Trimestre de 2016Pr. Andre Luiz
 
2016 3 TRI LIÇÃO 8 - A EVANGELIZAÇÃO DOS GRUPOS RELIGIOSOS
2016 3 TRI LIÇÃO 8 - A EVANGELIZAÇÃO DOS GRUPOS RELIGIOSOS2016 3 TRI LIÇÃO 8 - A EVANGELIZAÇÃO DOS GRUPOS RELIGIOSOS
2016 3 TRI LIÇÃO 8 - A EVANGELIZAÇÃO DOS GRUPOS RELIGIOSOSNatalino das Neves Neves
 
MAIS UM DIA - LIVRES PARA ADORAR
MAIS UM DIA - LIVRES PARA ADORARMAIS UM DIA - LIVRES PARA ADORAR
MAIS UM DIA - LIVRES PARA ADORARDiegodmo
 

Viewers also liked (10)

myVR presentation at HXGN Live 2013
myVR presentation at HXGN Live 2013myVR presentation at HXGN Live 2013
myVR presentation at HXGN Live 2013
 
Vou deixar na cruz - kleber lucas
Vou deixar na cruz - kleber lucasVou deixar na cruz - kleber lucas
Vou deixar na cruz - kleber lucas
 
Te louvarei - Toque no altar
Te louvarei - Toque no altarTe louvarei - Toque no altar
Te louvarei - Toque no altar
 
Xvision Field Activity Planner in Maritimt Magasin no 9
Xvision Field Activity Planner in Maritimt Magasin no 9Xvision Field Activity Planner in Maritimt Magasin no 9
Xvision Field Activity Planner in Maritimt Magasin no 9
 
A vida segundo o Espírito - Lição 07 - 2º Trimestre de 2016
A vida segundo o Espírito - Lição 07 - 2º Trimestre de 2016A vida segundo o Espírito - Lição 07 - 2º Trimestre de 2016
A vida segundo o Espírito - Lição 07 - 2º Trimestre de 2016
 
7 habbits
7 habbits7 habbits
7 habbits
 
ME RENDO A TI
ME RENDO A TIME RENDO A TI
ME RENDO A TI
 
2016 3 TRI LIÇÃO 8 - A EVANGELIZAÇÃO DOS GRUPOS RELIGIOSOS
2016 3 TRI LIÇÃO 8 - A EVANGELIZAÇÃO DOS GRUPOS RELIGIOSOS2016 3 TRI LIÇÃO 8 - A EVANGELIZAÇÃO DOS GRUPOS RELIGIOSOS
2016 3 TRI LIÇÃO 8 - A EVANGELIZAÇÃO DOS GRUPOS RELIGIOSOS
 
MAIS UM DIA - LIVRES PARA ADORAR
MAIS UM DIA - LIVRES PARA ADORARMAIS UM DIA - LIVRES PARA ADORAR
MAIS UM DIA - LIVRES PARA ADORAR
 
Palavras ao vento
Palavras ao ventoPalavras ao vento
Palavras ao vento
 

Similar to 6 sql explorer - powershell dba

Powershell - ISUG 99 (Aviad Deri)
Powershell  - ISUG 99 (Aviad Deri)Powershell  - ISUG 99 (Aviad Deri)
Powershell - ISUG 99 (Aviad Deri)sqlserver.co.il
 
Exploring Oracle Database Architecture (Hebrew)
Exploring Oracle Database Architecture (Hebrew)Exploring Oracle Database Architecture (Hebrew)
Exploring Oracle Database Architecture (Hebrew)Ram Kedem
 
7 sql azure for sql explore
7   sql azure for sql explore7   sql azure for sql explore
7 sql azure for sql exploresqlserver.co.il
 
Active active sql 2008 r2 cluster - Aviad Deri
Active active sql 2008 r2 cluster - Aviad DeriActive active sql 2008 r2 cluster - Aviad Deri
Active active sql 2008 r2 cluster - Aviad Derisqlserver.co.il
 
Sql Server Replication Isug 1 2009
Sql Server Replication   Isug 1 2009Sql Server Replication   Isug 1 2009
Sql Server Replication Isug 1 2009sqlserver.co.il
 
Managing oracle Database Instance
Managing oracle Database InstanceManaging oracle Database Instance
Managing oracle Database InstanceRam Kedem
 
SAP NetWeaver Gateway OData
SAP NetWeaver Gateway ODataSAP NetWeaver Gateway OData
SAP NetWeaver Gateway ODataDaniel Cnaan
 
Whats New In Windows 2008 R2 High Availability
Whats New In Windows 2008 R2 High AvailabilityWhats New In Windows 2008 R2 High Availability
Whats New In Windows 2008 R2 High AvailabilityAmit Gatenyo
 
Sql Server Replication Idf
Sql Server Replication   IdfSql Server Replication   Idf
Sql Server Replication Idfsqlserver.co.il
 
Windows 2008 for Microsoft Solution Briefing
Windows 2008 for Microsoft Solution BriefingWindows 2008 for Microsoft Solution Briefing
Windows 2008 for Microsoft Solution BriefingAmit Gatenyo
 
החלטתם שהארגון שלכם צריך אוטומציה חכמה. מה עכשיו?
החלטתם שהארגון שלכם צריך אוטומציה חכמה. מה עכשיו?החלטתם שהארגון שלכם צריך אוטומציה חכמה. מה עכשיו?
החלטתם שהארגון שלכם צריך אוטומציה חכמה. מה עכשיו?Boris Chernyak
 
יוני ברק הכשרה ביטוח
יוני ברק הכשרה ביטוחיוני ברק הכשרה ביטוח
יוני ברק הכשרה ביטוחAnochi.com.
 
היכרות עם וורדפרס ועוד חן כהן
היכרות עם וורדפרס ועוד   חן כהןהיכרות עם וורדפרס ועוד   חן כהן
היכרות עם וורדפרס ועוד חן כהןMiriam Schwab
 

Similar to 6 sql explorer - powershell dba (20)

Powershell - ISUG 99 (Aviad Deri)
Powershell  - ISUG 99 (Aviad Deri)Powershell  - ISUG 99 (Aviad Deri)
Powershell - ISUG 99 (Aviad Deri)
 
Exploring Oracle Database Architecture (Hebrew)
Exploring Oracle Database Architecture (Hebrew)Exploring Oracle Database Architecture (Hebrew)
Exploring Oracle Database Architecture (Hebrew)
 
7 sql azure for sql explore
7   sql azure for sql explore7   sql azure for sql explore
7 sql azure for sql explore
 
PHP Scalability
PHP ScalabilityPHP Scalability
PHP Scalability
 
אחסון מידע - ל-websql ו-indexdb רן בר-זיק
אחסון מידע - ל-websql ו-indexdb רן בר-זיקאחסון מידע - ל-websql ו-indexdb רן בר-זיק
אחסון מידע - ל-websql ו-indexdb רן בר-זיק
 
Active active sql 2008 r2 cluster - Aviad Deri
Active active sql 2008 r2 cluster - Aviad DeriActive active sql 2008 r2 cluster - Aviad Deri
Active active sql 2008 r2 cluster - Aviad Deri
 
Sql Server Replication Isug 1 2009
Sql Server Replication   Isug 1 2009Sql Server Replication   Isug 1 2009
Sql Server Replication Isug 1 2009
 
Managing oracle Database Instance
Managing oracle Database InstanceManaging oracle Database Instance
Managing oracle Database Instance
 
1 crystal bowl
1   crystal bowl1   crystal bowl
1 crystal bowl
 
SAP NetWeaver Gateway OData
SAP NetWeaver Gateway ODataSAP NetWeaver Gateway OData
SAP NetWeaver Gateway OData
 
SAP NetWeaver Gateway OData
SAP NetWeaver Gateway ODataSAP NetWeaver Gateway OData
SAP NetWeaver Gateway OData
 
Whats New In Windows 2008 R2 High Availability
Whats New In Windows 2008 R2 High AvailabilityWhats New In Windows 2008 R2 High Availability
Whats New In Windows 2008 R2 High Availability
 
Expand Cli Command
Expand Cli CommandExpand Cli Command
Expand Cli Command
 
Html5
Html5Html5
Html5
 
Sql Server Replication Idf
Sql Server Replication   IdfSql Server Replication   Idf
Sql Server Replication Idf
 
Windows 2008 for Microsoft Solution Briefing
Windows 2008 for Microsoft Solution BriefingWindows 2008 for Microsoft Solution Briefing
Windows 2008 for Microsoft Solution Briefing
 
החלטתם שהארגון שלכם צריך אוטומציה חכמה. מה עכשיו?
החלטתם שהארגון שלכם צריך אוטומציה חכמה. מה עכשיו?החלטתם שהארגון שלכם צריך אוטומציה חכמה. מה עכשיו?
החלטתם שהארגון שלכם צריך אוטומציה חכמה. מה עכשיו?
 
יוני ברק הכשרה ביטוח
יוני ברק הכשרה ביטוחיוני ברק הכשרה ביטוח
יוני ברק הכשרה ביטוח
 
היכרות עם וורדפרס ועוד חן כהן
היכרות עם וורדפרס ועוד   חן כהןהיכרות עם וורדפרס ועוד   חן כהן
היכרות עם וורדפרס ועוד חן כהן
 
SAPUI5 on SAP Web IDE
SAPUI5 on SAP Web IDESAPUI5 on SAP Web IDE
SAPUI5 on SAP Web IDE
 

More from sqlserver.co.il

Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013sqlserver.co.il
 
Things you can find in the plan cache
Things you can find in the plan cacheThings you can find in the plan cache
Things you can find in the plan cachesqlserver.co.il
 
Sql server user group news january 2013
Sql server user group news   january 2013Sql server user group news   january 2013
Sql server user group news january 2013sqlserver.co.il
 
Query handlingbytheserver
Query handlingbytheserverQuery handlingbytheserver
Query handlingbytheserversqlserver.co.il
 
Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012sqlserver.co.il
 
Products.intro.forum version
Products.intro.forum versionProducts.intro.forum version
Products.intro.forum versionsqlserver.co.il
 
SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3sqlserver.co.il
 
SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2sqlserver.co.il
 
SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1sqlserver.co.il
 
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended EventsSQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Eventssqlserver.co.il
 
SQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStoreSQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStoresqlserver.co.il
 
SQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DACSQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DACsqlserver.co.il
 
SQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: SpatialSQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: Spatialsqlserver.co.il
 
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf FraenkelBi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf Fraenkelsqlserver.co.il
 

More from sqlserver.co.il (20)

Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013
 
Things you can find in the plan cache
Things you can find in the plan cacheThings you can find in the plan cache
Things you can find in the plan cache
 
Sql server user group news january 2013
Sql server user group news   january 2013Sql server user group news   january 2013
Sql server user group news january 2013
 
DAC 2012
DAC 2012DAC 2012
DAC 2012
 
Query handlingbytheserver
Query handlingbytheserverQuery handlingbytheserver
Query handlingbytheserver
 
Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012
 
Products.intro.forum version
Products.intro.forum versionProducts.intro.forum version
Products.intro.forum version
 
SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3
 
SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2
 
SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1
 
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended EventsSQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
 
SQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStoreSQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStore
 
SQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DACSQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DAC
 
SQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: SpatialSQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: Spatial
 
מיכאל
מיכאלמיכאל
מיכאל
 
נועם
נועםנועם
נועם
 
עדי
עדיעדי
עדי
 
מיכאל
מיכאלמיכאל
מיכאל
 
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf FraenkelBi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
 
DBCC - Dubi Lebel
DBCC - Dubi LebelDBCC - Dubi Lebel
DBCC - Dubi Lebel
 

6 sql explorer - powershell dba

  • 1. ‫‪ Powershell‬להמונים‬ ‫אביעד דרעי | ‪ | DBA‬רפאל‬
  • 2. ‫קצת על רפאל...‬ ‫•‬ ‫קצת עליי...‬ ‫•‬ ‫איך הכל התחיל?‬ ‫•‬ ‫איפה הבעייה?‬ ‫•‬ ‫• הדרישות עבור הפתרון‬ ‫מה זה בעצם ‪?Powershell‬‬ ‫•‬ ‫מה מאפשר ה-‪?Powershell‬‬ ‫•‬ ‫השילוב של ‪SSIS+Powershell‬‬ ‫•‬ ‫סיכום + שאלות‬ ‫•‬
  • 3. ‫קצת על רפאל...‬ ‫• מפתחת, מייצרת ומספקת מגוון מערכות‬ ‫לחימה מתקדמות אשר תורמות למערכת‬ ‫הביטחון יכולות:‬ ‫– בים‬ ‫– באוויר‬ ‫– ביבשה‬ ‫• רווחים בשנת 0102 – למעלה מ 8.1 מיליארד ₪‬ ‫• למעלה מ-0005 עובדים‬
  • 4. ‫קצת עליי...‬ ‫• בוגר החוג למערכות מידע באוניברסיטת חיפה‬ ‫• ‪ DBA‬תשתיתי כ-5.4 שנים:‬ ‫– 2‪MS SQL 2000-2008R‬‬ ‫– ‪Oracle 9i-11g‬‬ ‫– 01 ‪SQL Anywhere‬‬ ‫– ‪Sybase‬‬
  • 5. ‫באחריותי...‬ ‫תחזוקה שוטפת של למעלה מ-001 שרתים במערכות מרכזיות.‬ ‫•‬ ‫פיתוח, הדרכה והטמעה של מגוון שירותים וכלים:‬ ‫•‬ ‫– ‪Reporting Services‬‬ ‫– ‪Integration Services‬‬ ‫– ‪Spatial‬‬ ‫– ‪PowerShell‬‬ ‫– ‪ Mirroring, Replication‬ועוד...‬ ‫שיפור ביצועים וכיוונון שאילתות‬ ‫•‬ ‫ייעוץ וליווי פרוייקטים ברפא"ל‬ ‫•‬ ‫אבטחת מידע בבסיסי נתונים‬ ‫•‬
  • 7. ‫• מספר מצומם של שרתים, על כל שרת מספר קבצי‬ ‫‪ batch‬וסקריפטים עבור גיבויים, תחזוקה והגדרות‬ ‫שונות של בסיסי הנתונים.‬ ‫• בכל סקריפט קיימות הגדרות של שרת‬ ‫הדואר,השו"ב ושרת התזמון המרכזי ממנו רצים‬ ‫הסקריפטים.‬
  • 9. ‫• מספר השרתים הולך ועולה משבוע לשבוע‬ ‫• כשמקימים שרת חדש, מעתיקים אליו את הסקריפטים‬ ‫ומשנים את משתני הסביבה השונים‬ ‫• נוצר מצב של קושי בתחזוקת הסקריפטים:‬ ‫– כל שינוי הכי קטן דורש מעבר על מאות סקריפטים‬ ‫– מסקריפט אחד ראשי נוצרו עשרות עותקים וקשה לעקוב‬ ‫אחריהם‬ ‫• חוסר תאימות של הסקריפטים השונים ומספרם ההולך‬ ‫וגדל גורם לכך שזמן יקר הולך לאיבוד‬
  • 11. ‫• צריך למצוא פתרון אחר לתחזוקה השוטפת שיענה‬ ‫על הדרישות הבאות:‬ ‫– יהיה עותק אחד ויחיד שירוץ ממקום מרכזי‬ ‫– במידת האפשר יאחד את כל הסקריפטים‬ ‫– יאפשר הוספת שרתים חדשים/גריעת שרתים ישנים‬ ‫באופן דינאמי וקל‬
  • 12. ‫– ידרוש מינימום תחזוקה ויאפשר אותה בקלות‬ ‫– יאפשר גיבויי מקומי של בסיסי הנתונים על השרתים‬ ‫– ירכז את הלוגים השונים למקום מרכזי‬ ‫– במידת האפשר יהיה חינמי ולא תלוי במוצר מדף‬
  • 13. ?POWERSHELL ‫אז מה זה בעצם‬
  • 14. ‫• כלי שורת פקודה של מיקרוסופט, המרחיב את שורת‬ ‫הפקודה הסטנדרטית (‪ )cmd‬ומעניק לו יכולות תכנות‬ ‫נרחבות יותר‬ ‫• משולב כחלק אינטגרלי בקו המוצרים האחרון כגון: ‪SQL‬‬ ‫8002 ‪2008, Exchange 2007, Win 7, Win Server‬‬ ‫ועוד‬ ‫• אידאלי לאוטומציה של תהליכים שונים לניהול ותחזוקת‬ ‫שרתים ותחנות עבודה‬
  • 15. ps1 :‫• סיומת קבצי הסקריפט‬ ‫ שבנויים בצורת‬cmdlets (command-lets)‫• מורכב לרוב מ‬ :‫ למשל‬verb-noun –Get-help –Write-host –Get-content –Copy-item -‫– על כל פקודה ניתן לקבל עזרה ופירוט ע"י שימוש ב‬ Get-help cmd-lets -detailed
  • 16. ?Powershell-‫מה מאפשר ה‬ '‫• יכולות תכנות נרחבות כמו שימוש בפונקציות, לולאות, תנאים וכו‬ foreach ($db in $Srv.Databases) { $msg = $db.Name | Out-File $LogFile -Append $msg = " Status is: "+ $db.Status.ToString() | Out-File $LogFile -Append $msg = " Mirorring Status is: "+ $db.MirroringStatus.ToString()| Out-File $LogFile -Append if ((-not $db.Status.ToString().Equals("Normal")) -and ($db.IsMirroringEnabled) -and ( $db.MirroringStatus -ne 5)) {$DBStatus=1} }
  • 17. ‫עבודה אל מול שרתים ותחנות עבודה מרוחקות‬ begin { $BckDir = $BckDir.Substring(0,$BckDir.Length -1 ) –replace (":","$") if (Test-Path "$ServerName$BckDirfull_backup") {Get-ChildItem -Path "$ServerName$BckDirfull_backup" | Remove-Item -Recurse} return 0 }
  • 18. ‫הרחבה ע"י מחלקות ואובייקטים שונים למשל‬ .Net/Com/WMI $Srv=New-Object "Microsoft.SqlServer.Management.Smo.Serv er" "$ServerName“ $SmtpClient = new-object system.net.mail.smtpClient $MailMessage = New-Object system.net.mail.mailmessage [System.Reflection.Assembly]::LoadWithPartial Name ("Microsoft.SharePoint")
  • 19. Powershell-‫הקריאה ל‬ ‫ במידה ורוצים להפעיל‬powershell.exe ‫• הפעלת סקריפט מתבצעת ע"י‬ ‫ במידה‬sqlps.exe ‫, או ע"י‬PS ‫ולהשתמש באובייקטים סנדרטיים של‬ .SQL Server ‫ורוצים להשתמש בהרחבות עבור‬ .‫• ניתן להרחיב את השפה ע"י קריאה לקובצי קונפיגורציה חיצוניים‬ :NetApp Snap Manager for SQL Server ‫למשל עבור‬ • powershell -PSConsoleFile "D:Program FilesNetAppSnapManager for SQL ServerSmsqlShell.psc1" "&'%ScriptDir%maintian.ps1' Backup %ServerName% %LogDir% full %FullBackupDir% 1“ ‫ ע"י‬SnapManager ‫• לאחר הקריאה להרחבה ניתן להפעיל פקודות של‬ :PS • New-Backup -LogBkup -bkupsif -verify -truncatelogs - GenericNaming -retainbackups 7 -retainsnapofsnapinfo 7 | Out-File $log
  • 20. ‫קישורים מעניינים‬ • PowerShell wiki: http://en.wikipedia.org/wiki/Windows_PowerShell • Why This SQL Server DBA is Learning Powershell by Ron Dameron http://www.simple-talk.com/sql/database- administration/why-this-sql-server-dba-is- learning-powershell/ • SQLPSX Project on Codeplex http://sqlpsx.codeplex.com/
  • 22. ‫אז מה עושים עם ה-‪ POWERSHELL‬הזה?‬
  • 23. ‫• מבצע גיבויים‬ ‫• מבצע בדיקת חיות ותקינות בסיסית של בסיסי הנתונים‬ ‫בשרת כל 5 דקות‬ ‫• מוריד ומעלה ‪ services‬בשרתים‬ ‫• מריץ ‪SSIS‬‬ ‫• במקרה של כשל, מכין דוח ‪ html‬ושולח אותו למפעילים‬
  • 24. ‫• פעם ביום עובר על כל בסיסי הנתונים בשרת ומשנה את‬ ‫ההגדרות שלהם כך שיתאימו לתקנים ולשיטת העבודה‬ ‫ברפאל‬ ‫• פעם בשבוע ביצוע רה-ארגון לבסיסי הנתונים כולל ארגון‬ ‫ובנייה מחדש של האינדקסים, כיווץ בסיסי הנתונים, עדכון‬ ‫סטטיסטיקות, מחיקת היסטוריות ועוד‬
  • 25. ‫• שליחת מיילים במקרה של כשל, כולל צירוף קובץ הלוג של‬ ‫הפעולה שנכשלה‬ ‫• מחיקת קבצים ישנים לפי גיל‬ ‫• ביצוע פעולות מותאמות על שרתים ובסיסי נתונים‬ ‫מיוחדים/רגישים‬ ‫• שליחת ‪ Traps‬למערכת השו"ב המרכזית ולמערכת התזמון‬ ‫המרכזית‬
  • 27. ‫‪SQL Server Integration Services -SSIS‬‬ ‫• מבוססים על טכולוגית ה-‪ .Net‬ומחליפים את ה-‬ ‫‪ DTS‬החל מגרסת 5002 ‪SQL‬‬ ‫• משמשים למעבר נתונים בין פלטפורומות שונות‬ ‫תוך הפעלת לוגיקות שונות על הנתונים במהלך‬ ‫המעבר‬ ‫• משמשים גם לביצוע פעולות תחזוקה שונות כמו‬ ‫גיבויים, רה-ארגון של אינדקסים וכו'.‬
  • 28. ‫גיבוי באמצעות ‪ ssis‬ו-‪powershell‬‬ ‫• לצורך הפתרון בניתי ‪ SSIS‬חדש מסוג ‪SQLDBMaint‬‬
  • 29. ‫• כל פרמטר שניתן היה – הומר לפרמטר שמתקבל באופן‬ ‫דינאמי משורת הפקודה. למשל: מיקום הגיבויים,סוג‬ ‫הגיבויים, זמן שמירת הגיבויים ועוד‬
  • 30. ‫מבנה הקבצים והתיקיות‬ ‫• תיקייה מרכזית על השרת הראשי, בה יושב סקריפט‬ ‫ה-‪ Powershell‬הבודד‬ ‫• לכל שרת מנוהל יש תת תיקיה ובתוכה קבצים עם‬ ‫הגדרות עבור השרת ותיקיית ‪ log‬עבור הלוגים‬
  • 31. ‫איך עובד התהליך?‬ ‫• מערכת התזמון המרכזית מפעילה קובץ ‪ batch‬בשעה‬ ‫מסויימת. בדוגמא הזו נציג גיבוי לוג‬
  • 32. ?‫מה בעצם קורה‬ ‫ שנראה‬dmp_log.bat ‫• הקובץ שמורץ למעשה הוא הקובץ‬ :‫כך‬ • call %~dp0db_env.bat • sqlps "&'%ScriptDir%maintian.ps1' Backup %ServerName% %LogDir% log %logBackupDir% 3"
  • 33. Db_env.bat ‫ מכיל פרמטרים שמשתנים משרת לשרת‬db_env-‫• קובץ ה‬ Powershell-‫ונקראים לפני הרצת ה‬ set ServerName=SERVERNAME set FullBackupDir=E:mssql_dump set LogBackupDir=F:mssql_dump set DiffBackupDir=G:mssql_dump set LogDir=D:jobsmssql%ServerName%log set ScriptDir=D:jobsmssql
  • 36. ‫בניית שורת הפקודה‬ ‫ עם‬PS‫) נבנתה בתוך קובץ ה‬dtexec( ‫• שורת הפקודה‬ ‫הפרמטרים שנשלחו בהתאם לשרתים השונים‬ $strSsis = 'dtexec /DTS "MSDBMaintenance PlansBackup" ' $strSsis +='/SET "package.Variables[User::BackupPath].Value;$BckPath" ' $strSsis +='/SET "package.Variables[User::BackupType].Value;$BckType" ' $strSsis +='/SET "package.Variables[User::BackupTypeID].Value;$BckTypeID" ' $strSsis +='/SET "package.Variables[User::Extension].Value;$Ext" ' $strSsis +='/SET "package.Variables[User::IsDifferntial].Value;$IsDif" ' $strSsis +='/SET "package.Variables[User::ServerName].Value;$Server" ' # Execute the sring and save the log to file Invoke-Expression $strSsis | Out-File $LogFile
  • 39. ‫אז מה בעצם הרווחתי?‬ ‫איחוד של מספר קבצי תחזוקת מערכת לקובץ‬ ‫•‬ ‫אחד גנרי‬ ‫ריכוז כל ההרצות למקום מרכזי אחד‬ ‫•‬ ‫ריכוז כל הלוגים למקום מרכזי אחד‬ ‫•‬ ‫שמירה על אחידות של בסיסי הנתונים‬ ‫•‬ ‫תחזוקה קלה, פשוטה ומהירה שחוסכת עשרות‬ ‫•‬ ‫שעות עבודה‬
  • 41. ‫‪ PS‬מאפשר יכולות חדשות לניהול תחנות עבודה ושרתים‬ ‫•‬ ‫‪ PS‬מעניק גמישות רחבה יותר (לעומת ‪)...VBS/Batchs‬‬ ‫•‬ ‫‪ PS‬קל לשימוש‬ ‫•‬ ‫הטמעה מהירה וקלה‬ ‫•‬ ‫שימוש ב-‪ PS‬חוסך זמן יקר (תחזוקה, עדכונים...)‬ ‫•‬