Showing posts with label vbs. Show all posts
Showing posts with label vbs. Show all posts

Wednesday, March 18, 2009

all-purpose vbs backup script

a simple vbs script that make a daily, weekly and monthly copy of a folder. It maintains 7 days worth of backups.
here's the code:

Dim oFS, backup_dir_w, backup_dir_m, backup_dir_d, backup_sorg
Dim timedir, timedir_del, date_7

'# SET YOUR PATHS HERE ####
backup_sorg = "C:\source_folder"
backup_dir_d = "C:\destination_folder\Daily\"
backup_dir_w = "C:\destination_folder\Weekly\"
backup_dir_m = "C:\destination_folder\Monthly\"

if right(backup_sorg, 1) = "\" then
backup_sorg = Left(backup_sorg, (Len(backup_sorg) - 1))
end if
if right(backup_dir_d, 1) <> "\" then
backup_dir_d = backup_dir_d & "\"
end if
if right(backup_dir_w, 1) <> "\" then
backup_dir_w = backup_dir_w & "\"
end if
if right(backup_dir_m, 1) <> "\" then
backup_dir_m = backup_dir_m & "\"
end if

Set oFS = CreateObject("Scripting.FileSystemObject")

'##### DAILY BACKUP FOLDER ####
timedir = backup_dir_d & Year(Date) & PadZero(Month(Date)) & PadZero(Day(Date)) & "\"
oFS.CreateFolder(timedir)

'#### DAILY FOLDER ####

oFS.CopyFolder backup_sorg, timedir

'#### DELETE 7 DAYS OLD BACKUP ####
date_7 = (DateAdd("d",-7,now()))
timedir_del = backup_dir_d & Year(date_7) & PadZero(Month(date_7)) & PadZero(Day(date_7))

If oFS.FolderExists(timedir_del & "\") Then
oFS.DeleteFolder timedir_del
End If
'################################

timedir = Left(timedir, (Len(timedir) - 1))

'#### WEEKLY BACKUP ####
if WeekDay(Now()) = 6 then
oFS.CopyFolder timedir, backup_dir_w
end if

'#### MONTHLY BACKUP ####
if Day(Now()) = 1 then
oFS.CopyFolder timedir, backup_dir_m
end if
'########################

Set oFS = Nothing

' PadZero - add 0 if value min 10
Function PadZero(val)

If (val < 10) Then
PadZero = "0" & val
Else
PadZero = val
End If

End Function

Tuesday, March 17, 2009

a vbs script to backup MySQL db

the script take daily, weekly and monthly backups of all tables in all MySQL databases. It mantains 7 days worth of backups.
here's the code:

Option Explicit

Dim backup_dir, num_days, user, password, arguments, backup_file, backup_dir_daily, backup_dir_weekly, backup_dir_monthly

Dim oShell, oFS, nResults


'SET HERE BACKUP PATH, DAYS OF MYSQL BACKUP, USER AND PWD

backup_dir = "C:\Backup\MySQLBK"

num_days = 7

user = "root"

password = "root"

'#####################################################

Set oFS = CreateObject("Scripting.FileSystemObject")

backup_dir_daily = backup_dir & "Daily"

backup_dir_weekly = backup_dir & "Weekly"


backup_dir_monthly = backup_dir & "Monthly"

If oFS.FolderExists(backup_dir_daily) = False Then

oFS.CreateFolder(backup_dir_daily)

End If

If oFS.FolderExists(backup_dir_weekly) = False Then

oFS.CreateFolder(backup_dir_weekly)

End If

If oFS.FolderExists(backup_dir_monthly) = False Then

oFS.CreateFolder(backup_dir_monthly)

End If

backup_file = backup_dir_daily & Year(Date) & PadZero(Month(Date)) & PadZero(Day(Date)) & "_all_databases.bak"

arguments = "--user=" & user & " --password=" & password & " --all-databases --result-file=" & backup_file

Set oShell = CreateObject("WScript.Shell")

nResults = oShell.Run("mysqldump.exe " & arguments, 1, TRUE)

Set oShell = Nothing

Dim folder, files, file, regex, Matches, Match, counter, backups, i, cur

Set regex = New regexp

regex.Pattern = "[0-9]{8}_all_databases.bak"

regex.Global = True


regex.IgnoreCase = True

Set folder = oFS.GetFolder(backup_dir_daily)

Set files = folder.Files

counter = 0


ReDim backups(0)


For Each file In files

Set Matches = regex.Execute(file.Name)

For Each Match in Matches


Redim Preserve backups(counter)

backups(counter) = Match.Value

counter = counter + 1

Next
Next


cur = 0


For i = counter - 1 To 0 Step -1


If (cur >= num_days) Then



oFS.DeleteFile(backup_dir_daily & backups(i))


Else

End If

cur = cur + 1

Next


'################WEEKLY BACKUP#####################

if WeekDay(Now()) = 6 then

oFS.CopyFile backup_file, backup_dir_weekly


else: end if


'######################################################


'################MONTHLY BACKUP#########################


if Day(Now()) = 1 then

oFS.CopyFile backup_file, backup_dir_monthly

else: end if


'######################################################



Set oFS = Nothing


' PadZero - add "0" to values < 10

Function PadZero(val)

If (val < 10) Then


PadZero = "0" & val

Else

PadZero = val


End If

End Function