{"id":137,"date":"2012-08-01T16:35:24","date_gmt":"2012-08-01T20:35:24","guid":{"rendered":"http:\/\/www.dev-notes.com\/blog\/?p=137"},"modified":"2018-03-11T21:52:25","modified_gmt":"2018-03-12T01:52:25","slug":"batch-file-to-create-regular-backups-with-cleanup","status":"publish","type":"post","link":"https:\/\/www.dev-notes.com\/blog\/2012\/08\/01\/batch-file-to-create-regular-backups-with-cleanup\/","title":{"rendered":"Batch file to create regular backups with cleanup"},"content":{"rendered":"<p>I run a version of the Windows batch script below to create a regular backup of one of the folders that contains my daily work; for this illustration, we will call the source folder c:source.  Every night, this batch file is triggered by Windows Scheduled Tasks to create a folder in my backup drive with the timestamp in the folder name; let us call this target folder D:target.  Before creating this daily backup, the script also checks to see how many backup sets are already existing in the backup folder, and if above the threshhold, the oldest one(s) are deleted; this feature helps making sure the space usage of redundant backup sets do not get out of control.  When scheduling this job with Windows Scheduled Tasks, you may wish to output to a file as well to create a log file of what old folders were removed and what files were copied (eg. &#8220;c:scriptsbackup.bat > logfile.log&#8221;).<\/p>\n<pre class=\"code\">\r\n@echo off\r\n\r\n:: Start Variables\r\n\r\nset NumberToKeep=5\r\nset BackupCmd=xcopy \/s \/c \/d \/e \/h \/i \/r \/y\r\n\r\nset BackupSource=C:\\source\\\r\nset BackupTarget=D:\\target\\\r\n\r\n:: End Variables\r\n\r\n\r\n\r\n\r\n:: Actual Script Starts Here!\r\necho+\r\necho STARTING BACKUP\r\necho %date% %time%\r\n\r\n:: 1. Delete older backup set(s) beyond the NumberToKeep\r\nfor \/F \"tokens=* skip=%NumberToKeep%\" %%I In ('dir \"%BackupTarget%\" \/AD \/B \/O-D \/TW') do (\r\n\techo+\r\n\techo DELETING OLD BACKUP SET %BackupTarget%%%~I\r\n\trd \/s \/q \"%BackupTarget%%%~I\"\r\n)\r\n\r\n:: 2. Create new backup set\r\nset bkuphour=%time:~0,2%\r\nif \"%bkuphour:~0,1%\"==\" \" set bkuphour=0%time:~1,1%\r\nset bkupfldr=%date:~10,4%_%date:~4,2%_%date:~7,2%_%bkuphour%_%time:~3,2%\r\n\r\necho+\r\necho CREATING FOLDER %BackupTarget%%bkupfldr%\\\r\nif not exist \"%BackupTarget%%bkupfldr%\\\" mkdir \"%BackupTarget%%bkupfldr%\\\"\r\n\r\necho+\r\necho BACKING UP FILES...\r\n%BackupCmd% \"%BackupSource%*.*\" \"%BackupTarget%%bkupfldr%\\\"\r\n\r\necho+\r\necho BACKUP COMPLETED!\r\necho %date% %time%<\/pre>\n<p>Hope you will find this useful!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article provides a sample Windows batch file to back up a file directory to a specified location, with a feature to clean up older backup sets.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-137","post","type-post","status-publish","format-standard","hentry","category-windows"],"_links":{"self":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/comments?post=137"}],"version-history":[{"count":2,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/137\/revisions"}],"predecessor-version":[{"id":176,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/137\/revisions\/176"}],"wp:attachment":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/media?parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/categories?post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/tags?post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}