I've been often asked to restore a database to another server and have all transactions logs applied to it up to a certain point. Using SQL Server 2005, you could setup Transaction Log shipping and it works very well, or you could copy the files over manually then apply them manually using SQL Server Enterprise Studio. Well I'm lazy so I came up with this simple script to do the work for me . . .
This script will ask for the database name to be restored as well as the directory where the transaction logs , drop any existing database of that name, restore the database and its transaction logs. It will not setup your database backups (full or transaction) nor will it replicate them over. I do have scripts for this but a tool like SyncBackSE works just as well. Oh this is a Windows batch script that utilizes osql which comes with SQL Server 2005.
@echo off
set BAKFILE=%1
set DBSNAME=%2
set LOGDIR=%3IF NOT DEFINED BAKFILE (GOTO HELP)
IF NOT DEFINED DBSNAME (GOTO HELP)
IF NOT DEFINED LOGDIR (GOTO HELP)echo Using the following variables to restore the database
echo Database Backup file - %BAKFILE%
echo Database Name - %DBSNAME%
echo Trans Log Directory - %LOGDIR%
choice /M "Do you want to continue"IF ERRORLEVEL 2 ( GOTO END)
set DATADIR=d:\DATA
set SQL=%TMP%\restore.sql
set OUT=.\restore_results.txtIF EXIST %SQL% ( del %SQL% )
IF NOT EXIST %BAKFILE% (
echo %BAKFILE does not exist. Must exit.
GOTO END
)echo USE [MASTER] > %SQL%
echo GO >> %SQL%
echo ALTER DATABASE %DBS% SET SINGLE_USER WITH ROLLBACK_IMMEDIATE >> %SQL%
echo GO >> %SQL%
echo DROP DATABASE %DBS% >> %SQL%
echo GO >> %SQL%echo RESTORE DATABASE [%DBSNAME%] > %SQL%
echo FROM DISK = '%BAKFILE%' >> %SQL%
echo WITH NORECOVERY >> %SQL%
echo GO >> %SQL%for /f "tokens=* delims= " %%a in ('dir /b /a-d /od %LOGDIR%\*.trn') do (
echo RESTORE LOG [%DBSNAME%] >> %SQL%
echo FROM DISK = '%LOGDIR%\%%a' >> %SQL%
echo WITH NORECOVERY >> %SQL%
echo GO >> %SQL%
set LAST=%%a
)echo RESTORE LOG [%DBSNAME%] >> %SQL%
echo FROM DISK = '%LOGDIR%\%LAST%' >> %SQL%
echo WITH RECOVERY >> %SQL%
echo GO >> %SQL%osql -E -w 5000 -i %SQL% -o %OUT%
GOTO END:HELP
echo.
echo Usage: restore_dbs.bat [BAKFILE] [DBSNAME] [LOG DIR]
echo.
echo.
echo BAKFILE - The full path to a recent backup file of the database
echo DBSNAME - Name of the new database
echo LOG DIR - The full path to a directory that contains all the recent tanslogs
echo *The LOG DIR should not contain any transaction logs older than BAKFILE
echo.
echo Eg: restore.bat d:\Backups\SampleDBS_backup_200705222201.bak SampleDBS d:\Temp
echo.:END
IF EXIST %SQL% ( del %SQL% )
Labels: batch sql
Today I want to briefly show you the power of the VMware's Get-State command. With it you can grab statistics on any VI entity - ESX Host, Virtual Machine, or Resource Pool. To use it, all you need to do is supply the entity you want to monitor and which statistics do you want to grab (common, CPU, memory, disk, or network). So let's get some stats on an ESX host.
$vc = Get-VIServer "vc1.example.com"
$esx = Get-VIHost "esx1.example.com"
Get-Stat $esx -Memory -maxsamples 3 -realtime
MetricId Timestamp Value Unit
--------- ---------- ----- ----
mem.vmmemctl.average 11/26/2007 1:52:2... 0 KB
mem.vmmemctl.average 11/26/2007 1:52:0... 0 KB
mem.vmmemctl.average 11/26/2007 1:51:4... 0 KBValue : 21.49
Timestamp : 11/26/2007 1:52:20 PM
MetricId : mem.usage.average
Unit : %
Description : Memory usage as percentage of total configured or available memory
Entity : VMware.VimAutomation.Client20.VMHostImplValue : 21.49
Timestamp : 11/26/2007 1:52:00 PM
MetricId : mem.usage.average
Unit : %
Description : Memory usage as percentage of total configured or available memory
Entity : VMware.VimAutomation.Client20.VMHostImplValue : 21.49
Timestamp : 11/26/2007 1:51:40 PM
MetricId : mem.usage.average
Unit : %
Description : Memory usage as percentage of total configured or available memory
Entity : VMware.VimAutomation.Client20.VMHostImplmem.active.average 11/26/2007 1:52:2... 78988 KB
mem.active.average 11/26/2007 1:52:0... 78988 KB
mem.active.average 11/26/2007 1:51:4... 78988 KB
mem.granted.average 11/26/2007 1:52:2... 2097504 KB
mem.granted.average 11/26/2007 1:52:0... 2097504 KB
mem.granted.average 11/26/2007 1:51:4... 2097504 KBGet-Stat $esx -common -maxsamples 1 -realtime
MetricId Timestamp Value Unit
--------- ---------- ----- ----
cpu.usage.average 11/26/2007 2:05:4... 4.86 %Value : 297
Timestamp : 11/26/2007 2:05:40 PM
MetricId : cpu.usagemhz.average
Unit : MHz
Description : CPU usage in MHz over the collected interval. For hosts this can be represented on a per Virtual Machine
basis as a stacked graph
Entity : VMware.VimAutomation.Client20.VMHostImplmem.usage.average 11/26/2007 2:05:4... 21.48 %
Value : 105
Timestamp : 11/26/2007 2:05:40 PM
MetricId : disk.usage.average
Unit : KBps
Description : Aggregated storage performance statistics. For hosts this can be represented on a per Virtual Machine bas
is as a stacked graph
Entity : VMware.VimAutomation.Client20.VMHostImplAnd finally,
$stats = Get-Stat $esx -common -maxsamples 1 -realtime
$stats. GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.ArraySince the object returned to you is an array,this allows you to store and manipulate the return results - like
$stats[2]
MetricId Timestamp Value Unit
--------- ---------- ----- ----
mem.usage.average 11/26/2007 2:07:0... 21.48 %
or $stats[2].Value
21.48
These values can then be piped into a table or graph to display the results.
Next week, I want to go back to something basic in Powershell - XML configuration. Powershell makes parsing XML documents very easy and I want to show how you can use this to create XML configuration files.
Labels: powershell, vmware, vmware powershell
This is a posting using Microsoft Live Writer which means this will be a short post.
Just want to say Happy Thanksgiving!!!!
Brian
Labels: vmware powershell
Labels: vmware powershell
use strict;
use warnings;
use Getopt::Long;
use VMware::VIRuntime;
use Term::ReadKey;
my %opts = (server => undef, username => undef,);
GetOptions (\%opts,"server=s", "username=s");
if( !defined ($opts{server} && $opts{username}) ) {
help();
exit (1);
}
ReadMode('noecho');
print "Enter $opts{username}\'s password: ";
my $password = ReadLine(0);
chomp $password;
ReadMode('normal');
# login
my $url = "https://" . $opts{server}. "/sdk/vimService";
Vim::login(service_url => $url, user_name => $opts{username}, password => $password);
# get VirtualMachine views for all powered on VM's
my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine');
# snapshot each VM
my $i;
foreach (@$vm_views) {
if( defined $_->snapshot ) {
print "-------------------------------------------------------------\n";
print $_->name . " has at least one snapshot defined . . . \n";
foreach $i (0 .. $#{$_->snapshot->rootSnapshotList} ) {
print "\t" . $_->snapshot->rootSnapshotList->[$i]->name . " @ " . $_->snapshot->rootSnapshotList->[$i]->createTime . "\n";
}
print "-------------------------------------------------------------\n";
}
}
# logout
Vim::logout();
sub help {
my $help_text = <<'END'; USAGE: query_snapshot.pl --server--user
Example:
query_snapshot.pl --server vc1 --user administrator
END
print $help_text;
}
Add-PSSnapin VMware.VimAutomation.Core
Get-VIServer vc1.example.com
Get-VM | % { $_.Name; Get-Snapshots $_ }
Labels: powershell, vmware
mutt -s $subject -c $ccpt_list -a $attachfile $rcpt_list < $email
Const SMTP_SERVER = "SMTP mail server address"---
Const SMTP_PORT = 25
Set objMessage = CreateObject("CDO.Message")
objMessage.Sender = sSender 'Email address of sender
objMessage.To = sRecipients 'Comma separate list of people who will receive the email
objMessage.Subject = sSubject 'The subject of the email
objMessage.Textbody = sBody 'The body of the email.
'This is optional. sAttachment is the fully qualified path to the file to attach
objMessage.AddAttachment sAttachment
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTP_SERVER
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTP_PORT
objMessage.Configuration.Fields.Update
objMessage.send
function send-email($s,$b,$to) {
$from = "send@company.com"; //Who will be sending the email
$domain = "smtp.company.com"; //SMTP server to send the message
$mail = new-object System.Net.Mail.MailMessage;
for($i=0; $i -lt $to.Length; $i++) {
$mail.To.Add($to[$i]);
}
$mail.From = new-object System.Net.Mail.MailAddress($from);
$mail.Subject = $s;
$mail.Body = $b;
$smtp = new-object System.Net.Mail.SmtpClient($domain);
$smtp.Send($mail);
}
$SUBJECT="This is the subject";Sending email is pretty easy and I know that there are a lot of places on the Web that already so you how to send email. Hopefully, though we can build off this to build much more complex scripts. I received an email from VMware concerning their Powershell interface to ESX. I am planning on next week to show how to write some simple scripts to manage an ESX build out with Powershell and we'll be using the send-email functions.
$EMAILADDR=@("person1@company.com","person2@company.com");
$BODY="Hello World";
send-email -s $SUBJECT -b $BODY -to $EMAILADDR
Labels: email, linux, powershell, vbscript, windows
October 2007 November 2007 December 2007 January 2008 February 2008 April 2008 July 2008
Subscribe to Posts [Atom]