Skip to content

Instantly share code, notes, and snippets.

View JustinMcNamara74's full-sized avatar

Justin McNamara JustinMcNamara74

View GitHub Profile
@JustinMcNamara74
JustinMcNamara74 / DBOutput_cleanup.pl
Last active August 29, 2015 14:13
#PERL Counts each "|" denoting a new column, and outputs the first 10 columns per line.
#!/usr/bin/env perl
while (my $row = <>) {
chomp $row;
my $num_of_pipes = $row =~ tr/|//;
while (($num_of_pipes < 10) && ($num_of_pipes > 0)) {
my $next_line = <>;
$row .= $next_line;
$num_of_pipes = $row =~ tr/|//;
}
@JustinMcNamara74
JustinMcNamara74 / extract_tables.sh
Last active August 29, 2015 14:16
#BASH Extract a list of tables from a MySQL database.
#!/bin/bash
PATH=/usr/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/youruser/bin
DB=db
USER=usr
PASS=pw
PATHTO=/path/to/
DIR=extract$(date +%Y%m%d)
#Declare an array of tables
@JustinMcNamara74
JustinMcNamara74 / backup_shell_scripts.sh
Last active July 28, 2017 14:37
#Bash Backup all shell scripts in a specific directory, to a destination directory.
#!/bin/bash
DATE=`date "+%y-%m-%d_%H:%M:%S"`
TOPATH=/to/file/path
FROMPATH=/from/file/path
#Backup all shell script files in FROMPATH and put them in TOPATH
/bin/find $FROMPATH -type f -name '*.sh' -print0 | tar zcf $FILEPATH/scripts_backup_$DATE.tar.gz --null -T -
@JustinMcNamara74
JustinMcNamara74 / crontab_backup.sh
Created March 12, 2015 13:46
#BASH Backup crontab
#!/bin/bash
#Simple crontab backup
crontab -l > /crontab_stuff/crontab_backups_$(date +%Y%m%d)
@JustinMcNamara74
JustinMcNamara74 / Index_Optimize.bat
Created March 16, 2015 17:56
#MSSQL US/USVI IndexOptimize - USER_DATABASES
sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE [dbo].[IndexOptimize] @Databases = 'Envision, Envision_Web, Envision_US, Envision_US_Web', @UpdateStatistics = 'ALL', @LogToTable = 'Y'" -b
sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE [dbo].[IndexOptimize] @Databases = 'Envision, Envision_Web, Envision_US, Envision_US_Web', @FragmentationLow = 'INDEX_REORGANIZE,INDEX_REBUILD_ONLINE,INDEX_REBUILD_OFFLINE', PageCountLevel = 0, @UpdateStatistics = 'ALL', @LogToTable = 'Y'" -b
@JustinMcNamara74
JustinMcNamara74 / alter_index_info.sql
Created March 17, 2015 16:00
#MSSQL SQL Command to view index information.
-- Alter_Index information
SELECT DatabaseName,
SchemaName,
ObjectName,
CASE WHEN ObjectType = 'U' THEN 'USER_TABLE' WHEN ObjectType = 'V' THEN 'VIEW' END AS ObjectType,
IndexName,
CASE WHEN IndexType = 1 THEN 'CLUSTERED' WHEN IndexType = 2 THEN 'NONCLUSTERED' WHEN IndexType = 3 THEN 'XML' WHEN IndexType = 4 THEN 'SPATIAL' END AS IndexType,
PartitionNumber,
ExtendedInfo.value('(ExtendedInfo/PageCount)[1]','int') AS [PageCount],
@JustinMcNamara74
JustinMcNamara74 / avg_fragmentation_percentage.sql
Last active August 29, 2015 14:17
#MSSQL View table pagecount and fragmentation levels.
-- Avg Fragmentation Percentages
DECLARE @DatabaseID int
SET @DatabaseID = DB_ID()
SELECT DB_NAME(@DatabaseID) AS DatabaseName,
schemas.[name] AS SchemaName,
objects.[name] AS ObjectName,
indexes.[name] AS IndexName,
@JustinMcNamara74
JustinMcNamara74 / check_free_space.sql
Last active August 29, 2015 14:17
#MSSQL Check free space (added GB)
SELECT DB_NAME() AS DbName, name AS FileName,
size/128.0 AS CurrentSizeMB,
size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)/128.0 AS FreeSpaceMB,
(size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)/128.0)/1024 AS FreeSpaceGB
FROM sys.database_files;
@JustinMcNamara74
JustinMcNamara74 / UserPermissions.sql
Last active May 1, 2024 21:49
#MSSQL List all user permissions/roles for all users
/*
********************************************************************************************************************************
Credits: @Jeremy
Posted: http://stackoverflow.com/questions/7048839/sql-server-query-to-find-all-permissions-access-for-all-users-in-a-database
********************************************************************************************************************************
Security Audit Report
1) List all access provisioned to a sql user or windows user/group directly
2) List all access provisioned to a sql user or windows user/group through a database or application role
3) List all access provisioned to the public role
@JustinMcNamara74
JustinMcNamara74 / CreateEncryptionKeyStructure.sql
Last active August 29, 2015 14:18
#MSSQL Creates all items necessary for SQL Server Encryption/Decryption
/**
A few notes:
- Remember the DB Master Key!!
- When creating certificates/keys be descriptive, as there may be many on your instance of SQL Server
**/
IF (select Count(*) from sys.symmetric_keys where name like '%DatabaseMasterKey%') = 0
BEGIN
CREATE master key Encryption by password = 'SomePassword';