Automated regular vault backups with a .bat file and Task Scheduler in Windows

This is very easy, takes just a few minutes to set up, requires no third party tools and can be done entirely using functions natively available in windows.

This will create a daily/weekly/whatever backup, and append the date and time to each backup folder so that it doesn’t overwrite the last, like so (in this example, it’s set to Daily backup at 6pm):

Backup_2023-03-20_18-00-00
Backup_2023-03-19_18-00-00
etc.

This will work for any directory you set, it’s not just limited to Obsidian.


  1. Copy paste this code into notepad:
@echo off
set Source=C:\Users\%username%\Obsidian
set Destination=C:\Users\%username%\Obsidian backups
set FolderName=Backup_%date:~-4,4%-%date:~-10,2%-%date:~-7,2%_%time:~0,2%-%time:~3,2%-%time:~6,2%
mkdir "%Destination%\%FolderName%"
xcopy /s /e /h /y "%Source%" "%Destination%\%FolderName%"
  • 1.1 - Set your source & destination directories, and save it as a .bat file (e.g. ObsidianBackup.bat)

Note: At this stage, running this will open a CMD window whenever the script runs, showing you the copy operation. If you don’t care about hiding this, skip the next step.

  1. To hide the CMD window, simply copy paste the following code into a new notepad.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\path\to\backup.bat" & Chr(34), 0
Set WshShell = Nothing
  • 2.2 Replace the ‘C:\path\to\backup.bat’ with the path to that .bat file you just made

  • 2.3 save it as a .vbs file, such as “HiddenObsidianBackup.vbs.” This script will launch your .bat file silently.

  1. Open Task Scheduler and create a “New Basic Task”, name it, then set the trigger (‘daily’, ‘weekly’, ‘when a specific event is logged’, etc), then select “Start a program” as the action, browse to select the .vbs (or .bat) file you saved earlier, click “Finish”.

Let me know how you end up modifying the code for your own purposes. I would love to see improvements to this, surely there are issues with this I have not foreseen but so far it’s been keeping my vault nicely backed up.

7 Likes

Nice work. However, the %date% and %time% return different format depending on your locale setting, so the string manipulation may not work as intended.

1 Like

True! Thanks for pointing that out. However I am not the author of this script so I don’t know much on how to fix it for those with different locales. Hopefully someone else chimes in.

1 Like