-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleDailyAdmin
98 lines (82 loc) · 5.92 KB
/
SimpleDailyAdmin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Script to run daily Tableau Server Maintenance
# by Scoobywagon
#Edit the following to reflect your preferred directories and paths.
$log_path = "<Path to Tableau Server Logs directory.>"
$backup_path = "<Path to Tableau Server Backups directory.>"
$automation_log_dir = "<Path to logging directory>"
#set up email.
$sender_address = "<Email Sender address>"
$target = "<Intended Recipient email address>"
$smtp_server = "<SMTP Server Hostname>"
$subject = "<Server Name> Daily Maintenance"
$body = "Daily maintenance for $($today_mm_dd_yyyy)."
#set our runtime environment
$start_time = Get-Date
$today_mm_dd = Get-Date -format "MM-dd"
$today_mm_dd_yyyy = Get-Date -Format "MM-dd-yyyy"
$script_log = "$($automation_log_dir)\maintenance_log-$($today_mm_dd).log"
$tsm_log = "$($automation_log_dir)\tsm_log-$($today_mm_dd).log"
$time_tracker = "$($automation_log_dir)\maintenance_log.csv"
#Get Tableau install directory
$build_number = Get-ChildItem -Path env: | Where-Object Name -like "tableau_server_data_dir_ver*"
if (-not($build_number)) {
$now = Get-Date -Format "hh:mm:ss"
Write-Host "$($now) - No build number environment variable found. Is Tableau Server installed?"
Write-Output "$($now) - No build number environment variable found. Is Tableau Server installed?"
Break
}
$install_path = "E:\Tableau\Tableau Server\packages\"
$tsm_path = $install_path + "bin." + $build_number.value + "\tsm.cmd"
#write TSM arguments
$tsm_ziplogs = ("maintenance ziplogs -o -f daily-ziplogs-$($today_mm_dd)" ).Split()
$tsm_cleanup = ("maintenance cleanup").Split()
$tsm_backup = ("maintenance backup -f tableau-backup-$($today_mm_dd)").Split()
$tsm_export = ("settings export -f f:\tableau_backups\tsm_settings-$($today_mm_dd).json").Split()
#Run Ziplogs, track how long it takes, redirect command output to a log file.
$now = Get-Date -Format "hh:mm:ss"
Write-Host -nonewline "$($now) - Zipping logs "
Write-Output "$($now) - Zipping logs " | Out-File -Encoding unicode -Append -FilePath $script_log
Write-Output "----- Starting Ziplogs run -----" | Out-File -FilePath $tsm_log
$ziplogs_runtime = [math]::Round((Measure-Command {& $tsm_path $tsm_ziplogs | Out-File -FilePath $tsm_log}).TotalSeconds,2)
$ziplogs_filesize = [math]::Round((Get-ChildItem "$($log_path)daily-ziplogs-$($today_mm_dd).zip").length / 1kb, 2)
Write-Output "completed in $($ziplogs_runtime) seconds." | Out-File -Encoding unicode -Append -FilePath $script_log
Write-Host "completed in $($ziplogs_runtime) seconds."
Write-Output "Ziplogs,$($today_mm_dd_yyyy),$($ziplogs_runtime),$($ziplogs_filesize)" | Out-File -Encoding unicode -Append -FilePath $time_tracker
#Run Cleanup, track how long it takes, redirect command output to a log file.
$now = Get-Date -Format "hh:mm:ss"
Write-Host -NoNewline "$($now) - Cleaning up "
Write-Output "$($now) - Cleaning up " | Out-File -Encoding unicode -Append -FilePath $script_log
Write-Output "----- Starting Cleanup run -----" | Out-File -FilePath $tsm_log -append
$cleanup_runtime = [math]::round((Measure-Command {& $tsm_path $tsm_cleanup | Out-File -FilePath $tsm_log -append}).TotalSeconds, 2)
Write-Output "completed in $($cleanup_runtime) seconds." | Out-File -Encoding unicode -Append -FilePath $script_log
Write-Host "completed in $($cleanup_runtime) seconds."
Write-Output "Cleanup,$($today_mm_dd_yyyy),$($cleanup_runtime)"| Out-File -Encoding unicode -Append -FilePath $time_tracker
#Run backup, track how long it takes, redirect command output to a log file.
$now = Get-Date -Format "hh:mm:ss"
Write-Host -NoNewline "$($now) - Backing up"
Write-Output "$($now) - Backing up " | Out-File -Encoding unicode -Append -FilePath $script_log
Write-Output " ------ Starting backup run -----" | Out-File -FilePath $tsm_log -append
$backup_runtime = [math]::round((Measure-Command {& $tsm_path $tsm_backup | Out-File -FilePath $tsm_log -append}).TotalSeconds,2)
$backup_filesize = [math]::Round((Get-ChildItem "$($backup_path)tableau-backup-$($today_mm_dd).tsbak").length / 1kb, 2)
Write-Output "completed in $($backup_runtime) seconds." | Out-File -Encoding unicode -Append -FilePath $script_log
Write-Host "completed in $($backup_runtime) seconds."
Write-Output "Backup,$($today_mm_dd_yyyy),$($backup_runtime),$($backup_filesize)"| Out-File -Encoding unicode -Append -FilePath $time_tracker
#Export Tableau Server settings, track how long it takes, redirect command output to a log file.
$now = Get-Date -Format "hh:mm:ss"
Write-Host -NoNewline "$($now) - Exporting configuration "
Write-Output "$($now) - Exporting configuration " | Out-File -Encoding unicode -Append -FilePath $script_log
Write-Output " ----- Starting Configuration Export ----- " | Out-File -FilePath $tsm_log -Append
$export_runtime = [math]::Round((Measure-Command {& $tsm_path $tsm_export | Out-File -FilePath $tsm_log -Append}).TotalSeconds,2)
$export_filesize = [math]::Round((Get-ChildItem "$($backup_path)tsm_settings-$($today_mm_dd).json").length / 1kb, 2)
Write-Output "completed in $($export_runtime) seconds." | Out-file -Encoding unicode -Append -FilePath $script_log
Write-Host "Completed in $($export_runtime) seconds."
Write-Output "Settings Export,$($today_mm_dd_yyyy),$($export_runtime),$($export_filesize)" | Out-File -Encoding unicode -Append -FilePath $time_tracker
#get total runtime
$now = Get-Date
$complete_runtime = New-TimeSpan -Start $start_time -End $now
$complete_runtime_minutes = [math]::round($complete_runtime.TotalMinutes,2)
$complete_runtime_seconds = [math]::round($complete_runtime.TotalSeconds,2)
Write-Host "Maintenance complete. Total runtime: $($complete_runtime_minutes) minutes"
Write-Output "Maintenance Complete. Total runtime: $($complete_runtime_minutes) minutes" | Out-File -Encoding unicode -append -FilePath $script_log
Write-Output "Complete,$($today_mm_dd_yyyy),$($complete_runtime_seconds)" | Out-File -Encoding unicode -Append -FilePath $time_tracker
Send-MailMessage -SmtpServer $smtp_server -From $sender_address -To $target -Attachments $time_tracker -Subject $subject -Body $body