Most Recent Thoughts

Easy Windows Batch File Backups (with MySQL, MsSQL, and email notifications included)

Posted January 11th, 2010 at 3:13 am by Matt Chepeleff

Here’s a post for fellow geeks.  Today I wanted to beef the backups for sites I serve and at the same time keep my brand server clean from software if I could.  I use Mozy Pro on the server but wanted periodic snapshots of sites at different intervals as well.  Only issue is I host sites that use MySQL and MsSQL databases – so this script accommodates both.  I wrote the following batch file which backs up website source files & databases to a .Rar file.  Then it sends me an email notification not only that a backup has occurred…but also with the details of all other backup files available.

I scheduled a .bat file with this script in it for each site I host and it drops everything into a single file for me per the schedule frequency.  I’m even free to email myself or clients their entire application source.

Prerequsites for this script to work are Winrar (not the free version, but a purchased version as I don’t think the free versions allow command line interfaces through rar.exe) and SendEmail (free).  I’ve separated the code a bit so you should be able to customize the top portion with your paths, etc.

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
@ECHO off
 
REM *** CONFIGURE HERE (Leave mysql or mssql blank to skip database backup) ***
 
SET rootdir=\Inetpub\yourdomain.com
SET siteName=YourDomain.com
SET backupFolder=WWW
 
SET mysql=database_name
SET mssql=database_name
 
SET mysqlServer=localhost
SET mysqlPass=password
SET mssqlServerInstance=localhost\instanceName
 
SET WinRarexeLocation=C:\progra~1\WinRAR\rar
SET sendMailexeLocation=C:\folder_name\sendEmail.exe
 
SET emailFromAddress="auto_backups@yourdomain.com"
SET emailToAddress="steve@yourdomain.com"
SET smtpServer=smtp.yourdomain.com:25
 
REM *** Careful editing below this ********************************************
 
cd\
cd%rootdir%
 
REM *** Backup MySQL and/or MsSQL databases if db names specified ***
if defined mysql mysqldump -h %mysqlServer% -u root -p %mysql% > db_backup.sql --password=%mysqlPass%
if defined mssql sqlcmd -S "%mssqlServerInstance%" -Q "BACKUP DATABASE %mssql% TO DISK = 'C:%rootdir%\%mssql%_db.bak';"
 
REM *** Save contents of specified directory to .rar archive ***
%WinRarexeLocation% a -agMM_DD_YYYY_HHMMSS BAK .rar %backupFolderr% db_backup.sql %mssql%_db.bak
 
del db_backup.sql
del %mssql%_db.bak
 
set myDir1=C:%rootdir%\*.rar
set fileinfo=
 
for /f "delims=" %%a in ('dir /b /a-d %myDir1% 2^>NUL') do call :process %%a %%~zna
%sendMailexeLocation% -f %emailFromAddress% -t %emailToAddress -u "%siteName% Backed Up!" -s %smtpServer% -m "All source and/or database files for %siteName% have been backed up.\n\nBackup files available are:\n\nNAME                                   FILE SIZE\n-------------------------------------------------------------\n%fileinfo%\n\nEnjoy!"
 
:process
if not "%fileinfo%"=="" set fileinfo=%fileinfo%\n
set fileinfo=%fileinfo%%~1   %~2

I put all website source code into a WWW for each site – so that’s why I added a backupFolder variable to the script.  You can list as many other files or folders (separated by a space) or use *.* to include all files in the specified folder.

Feel free to customize and use as needed.

Tags:

New Site, mat.tc API and Wordpress Plugin

Posted January 10th, 2010 at 1:54 am by Matt Chepeleff

Unless you’re reading this via RSS, you’ve already seen the new site I’ve rolled out. I really like it and hope you do as well. I did more than just redesign the way it looks though. It’s more than a pretty face.

I spent some time trying to clean up the information on the site too.  Specifically, I shortened the about page and really over hauled the portfolio page.  The portfolio is way better than it was:

New & Old Portfolio Pages

I think the new page is a lot cleaner and because there’s SO much less text, it should be easier to read and understand.

I also thought I’d play with some APIs and get into Wordpress a but while I built the new site.  I certainly brushed up my PHP and also learned a bit about Wordpress Plugins.  I wrote a plugin like Twitter Tools from Alex King – except mine uses my own http://mat.tc url shortener.  Obviously to do this I wrote a simple shorten API modeled after bit.ly’s API.  It works wonderfully.  I can tweet from the site when logged in.  Automatic new post tweets also use mat.tc now which saves me the extra steps.

Twitter Tools doesn’t use OAuth though, so I had to rewrite a bit of the code so Twitter would recognize my calls and show the correct “From: ” line for tweets.  But that wasn’t too bad.

To round out the new site, I created a new logo too.  The old logo (in the image above) was just a simple play on my initials.  While exploring new ideas I came back to this ‘letters in my name’ idea.  So the little dude is made up of letters in my name (the m, c, and 2 l’s).  I added the tie because I thought it clarified it was a person – plus the business tie-in worked (sorry).  Font is Insignia.

Anyway, I’m really glad the content on the site is updated.  The new design, logo and mat.tc/Twitter integration is also nice.  I threw in a little flash on the login page for fun too: check it out.

Enjoy!

Tags: ,