Backup A Data Base Using A VBScript - Automated
Backup A Data Base Using A VBScript - Automated
(A) Code can be obtained from the Reference (1) or at Appendix – Code 1
Instructions
Using Windows Applications available on your PC, Windows Task Scheduler, Notepad and CScript
(Located: C:\Windows\System32\cscript.exe) to auto run a .vbs file.
Finally, we will create an automation rule (or task) using a native Windows application called
Windows Task Scheduler. To find this hidden gem, just type “Windows Task Scheduler” in the
search bar of your Taskbar and launch it.
You should see a Task Scheduler dialog appear that looks something similar to the image below. If
you do not see the Actions pane to the right, click the last icon in the application’s toolbar.
You will also be able to determine you would like the task to run if you are logged in/out of your PC. I
assume most of you will want to enable the task to run while your computer is logged off, especially
if your task is running on off hours.
In the Triggers Tab, you will be able to create in-depth scheduling rules for your automation. In the
below example, I am creating a Trigger to execute the Task every day at 6:30am.
There are also options in the Advanced Settings section to repeat the task after the initial trigger. For
example, you may what to refresh a stock portfolio model every 10 minutes after the stock market
opens. The Advanced Settings section gives you the freedom to repeat the task every X number of
minutes after the initial trigger has executed.
The Actions Tab is where you will map out which program you would like to launch. In our example,
we are going to want to run the .vbs file we created that will open and run our Excel macro. We will
utilize a program called CScript to execute our .vbs file. You should have this program already
installed if you have a Windows OS. Below is the path to the application for your reference:
Program/Script: "C:\Windows\System32\cscript.exe"
Next, we will want to reference the file to open via CScript. Paste in the full file path of the .vbs file
we created earlier. Mine looks something like this:
PLEASE NOTE: Both of these inputs MUST be surrounded by double quotation marks or else your
task will not function!
Finally, the Settings tab provides a few additional options to customize your task. I have highlighted
one important option below if you are incorporating a “completion notification” message box into
your Visual Basic script (FYI we are using a message box in the example).
It is important to understand that if a message box appears on the screen, by default your task
cannot repeat itself until the message box is closed by the user. To get around this, you can tell the
task to kill itself if and rerun if no one ended up closing the message box during the time between
triggers.
(C) Vbscript to delete backup files more than XX days old
Instructions
References
1. https://www.devhut.net/2018/02/22/ms-access-backup-a-database-using-a-vbscript/
2. https://www.thespreadsheetguru.com/blog/how-to-automatically-run-excel-vba-macros-
daily
3. https://www.symantec.com/connect/downloads/vbscript-delete-old-files
Appendix 1 – VBScript Code for Backup
'----------------------------------------------------------------------
-----------------
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Backup an Access Database
' Copyright : The following is release as Attribution-ShareAlike 4.0
International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-
sa/4.0/
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
'
***********************************************************************
***************
' 1 2018-02-22 Initial Release
'----------------------------------------------------------------------
-----------------
Dim sSourceFolder
Dim sBackupFolder
Dim sDBFile
Dim sDBFileExt
'----------------------------------------------------------------------
-----------------
'----------------------------------------------------------------------
-----------------
'
***********************************************************************
***************
'
***********************************************************************
***************
' You shouldn't need to edit anything below this point normally, so
be careful!
'
***********************************************************************
***************
'
***********************************************************************
***************
'----------------------------------------------------------------------
-----------------
'----------------------------------------------------------------------
-----------------
'----------------------------------------------------------------------
-----------------
' Procedure : BackupDb
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Database Backup Routine
' Copyright : The following is release as Attribution-ShareAlike 4.0
International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-
sa/4.0/
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sSourceFolder : Folder in which the database resides ->
"C:\Temp\Databases"
' sBackupFolder : Folder where the backups should be saved ->
"C:\Temp\Databases\Backups"
' sDBFile : Filename of the database to backup ->
"TestDatabase"
' sDBFileExt : Extension of the database to backup -> "accdb"
' bForceCopy : Should the backup be performed if a lock file is
present -> True/False
' bRunSilent : Should Messages be displayed when the procedure is run
-> True/False
' bCompact : Should the backup datbase be automatically
compacted -> True/False
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
'
***********************************************************************
***************
' 1 2018-02-22 Initial Release
'----------------------------------------------------------------------
-----------------
Function BackupDb(sSourceFolder, sBackupFolder, sDBFile, sDBFileExt,
bForceCopy, bRunSilent, bCompact)
Dim oFSO
dim sDBLockFileExt
'Cleanup
Set oFSO = Nothing
End Function
'----------------------------------------------------------------------
-----------------
' Procedure : CopyFile
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Backup the file while appending a date/time stamp to the
filename
' Copyright : The following is release as Attribution-ShareAlike 4.0
International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-
sa/4.0/
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' oFSO : File System Object
' sSourceFolder : Folder in which the database resides ->
"C:\Temp\Databases"
' sBackupFolder : Folder where the backups should be saved ->
"C:\Temp\Databases\Backups"
' sDBFile : Filename of the database to backup ->
"TestDatabase"
' sDBFileExt : Extension of the database to backup -> "accdb"
' bCompact : Should the backup datbase be automatically
compacted -> True/False
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
'
***********************************************************************
***************
' 1 2018-02-22 Initial Release
'----------------------------------------------------------------------
-----------------
Function CopyFile(oFSO, sSourceFolder, sBackupFolder, sDBFile,
sDBFileExt, bCompact)
Dim sDateTimeStamp
Const OVER_WRITE_FILES = True
'----------------------------------------------------------------------
-----------------
' Procedure : CompactDB
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Compact the specified database file
' Copyright : The following is release as Attribution-ShareAlike 4.0
International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-
sa/4.0/
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sFile : Database file (path & full filename with extension) to
compact
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
'
***********************************************************************
***************
' 1 2018-02-22 Initial Release
'----------------------------------------------------------------------
-----------------
Function CompactDB(sFile)
Dim oWshShell
Dim sAppEXE
Dim sRegKey
Dim sAccessPath
sAppEXE = "MSACCESS.EXE"
sRegKey =
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App
Paths\" & sAppEXE & "\Path"
Set oWshShell = CreateObject("WScript.Shell")
sAccessPath = oWshShell.RegRead(sRegKey)
oWshShell.Run chr(34) & sAccessPath & sAppEXE & chr(34) & " " &
_
chr(34) & sFile & chr(34) & " /compact", 0, True
'Source: http://saltwetbytes.wordpress.com/2012/10/16/vbscript-adding-
datetime-stamp-to-log-file-name/
Function Pad(CStr2Pad, ReqStrLen)
Dim Num2Pad
Pad = CStr2Pad
If len(CStr2Pad) < ReqStrLen Then
Num2Pad = String((ReqStrlen - Len(CStr2Pad)), "0")
Pad = Num2Pad & CStr2Pad
End If
End Function
Appendix 2 – VBScript Code for Deleting older Backup files
'************ Start of Code **********************
Option Explicit
On Error Resume Next
Dim oFSO, oFolder, sDirectoryPath
Dim oFileCollection, oFile, sDir
Dim iDaysOld
' Specify Directory Path From Where You want to clear the old files
iDaysOld = 10
'This section will filter the log file as I have used for for test case
'Specify the Extension of file that you want to delete
'and the number with Number of character in the file extension
End If
Next