Simple backup script (VBS)
Q. I would like to have a simple backup script, running under windows, which would:
1. Create an archive of my files
2. Delete the archive after a period of time
A. To create an archive, we'll use RAR archiver. It can be downloaded from https://www.rarlab.com/download.htm. After you install this application, copy the file rar.exe (found in C:\Program Files\WinRAR) to C:\Windows. This way you can run rar.exe from anywhere on the system and it will find it.
For the purpose of this tutorial we'll use:
Expiration date: 15 days (so we keep the backups for 2 weeks)
Backup path (Destination): C:\backups
Files that are being backed up (Source): C:\inetpub\wwwroot (IIS websites)
Dim Fso
Dim Directory
Dim Modified
Dim Files
Dim MyDate
Dim OutputFile
MyDate = Replace(Date, "/", "-")
OutputFile = "backup-" & mydate & ".rar"
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "C:\windows\rar.exe a c:\backups\" & OutputFile & " C:\inetpub\wwwroot"
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Directory = Fso.GetFolder("C:\backups")
Set Files = Directory.Files
For Each Modified in Files
If DateDiff("D", Modified.DateLastModified, Now) > 15 Then Modified.Delete
Next
Firstly, the script will create an archive file with a date embedded (so it will be in the format: backup-10-4-2011.rar for a backup that was created on October 4th, 2011)
Then it runs the external tool, rar.exe, where it adds files to the newly created archive
Finally, it checks in the backup directory for files older that 15 days and deletes them.
Update: Thanks JK - fixed the errors.
Dim OutputFile MyDate = Replace(Date, "/", "-")
and
OutputFile = "backup-" & mydate & ".rar" Set objShell = WScript.CreateObject("WScript.Shell")
Gives an error when running