{"id":2204,"date":"2015-09-02T08:26:35","date_gmt":"2015-09-02T08:26:35","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2204"},"modified":"2015-09-02T08:26:35","modified_gmt":"2015-09-02T08:26:35","slug":"scheduled-backup-of-files-with-python-in-windows","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/09\/02\/scheduled-backup-of-files-with-python-in-windows\/","title":{"rendered":"Scheduled backup of files with Python in Windows"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPython.js\"><\/script>\n<h3>OVERVIEW<\/h3>\n<p>Sometimes using a full-fledged backup software or the built-in Windows Backup service is an overkill for your requirements. If your need is simply to backup some files and folders periodically, then a bit of Python programming combined with the Windows Task Scheduler takes care of things.<\/p>\n<h3>SOURCE CODE<\/h3>\n<p>There is a single python file which does the actual backup. There are two kinds of backup:<\/p>\n<ul>\n<li>Copying files to another disk or folder<\/li>\n<li>Compressing files and folders and saving the zip file<\/li>\n<\/ul>\n<p>If you choose the zip option, make sure that your data does not create a zip file more than 2 Gb as zip files cannot work if they are more than approx. 2Gb in size.<\/p>\n<h3>TASK SCHEDULER<\/h3>\n<p>The Windows Task Scheduler is used to run the backup script as and when required. Rather than try to run the python script directly from the Task Scheduler, it is better to create a batch file which calls the python script and then run the batch in Task Scheduler.<\/p>\n<p>It is good to have multiple sets of backup. To do so, you can create two identical scripts where the locations of the archived files are different. An easy way is to run a first set on Sun\/Tue\/Thu\/Sat and the second set on Mon\/Wed\/Fri .<\/p>\n<p>&nbsp;<br \/>\n<pre class=\"brush: python\">##\r\n# Script to copy files and compress them and put them in a separate location\r\n# Amit Sengupta, Sep 2015, HYD\r\n# Written in Python 2.7\r\n###\r\n\r\nimport os\r\nimport zipfile\r\nimport shutil\r\n\r\n#copy files and folder and compress into a zip file\r\ndef\tdoprocess(source_folder, target_zip):\r\n\tzipf = zipfile.ZipFile(target_zip, &quot;w&quot;)\r\n\tfor subdir, dirs, files in os.walk(source_folder):\r\n\t\tfor file in files:\r\n\t\t\tprint os.path.join(subdir, file)\r\n\t\t\tzipf.write(os.path.join(subdir, file))\r\n\t\r\n\tprint &quot;Created &quot;, target_zip\r\n\t\r\n\r\n#copy files to a target folder\t\r\ndef\tdocopy(source_folder, target_folder):\r\n\tfor subdir, dirs, files in os.walk(source_folder):\r\n\t\tfor file in files:\r\n\t\t\tprint os.path.join(subdir, file)\r\n\t\t\tshutil.copy2(os.path.join(subdir, file), target_folder)\r\n\t\r\n\t\t\r\n\r\nif __name__ ==&#039;__main__&#039;:\r\n\tprint &#039;Starting execution&#039;\r\n\t\r\n\t#compress to zip\r\n\tsource_folder = &#039;D:\\\\projects\\\\dummydata&#039;\r\n\ttarget_zip = &#039;e:\\\\backups\\\\dummydata.zip&#039;\r\n\tdoprocess(source_folder, target_zip)\t\r\n\t\t\t\r\n\t#copy to backup folder\r\n\tsource_folder = &#039;D:\\\\data\\\\mp3files&#039;\r\n\ttarget_folder = &#039;e:\\\\backups\\\\mp3backup&#039;\r\n\tdocopy(source_folder, target_folder)\r\n\t\r\n\t\r\n\tprint &#039;Ending execution&#039;<\/pre><\/p>\n<p>The batch file can contain a single line which runs python from its installed folder and the full path of the python script. Eg.Mybackup.bat<\/p>\n<h4 style=\"text-align: center;\">\nc:\\python27\\python d:\\scripts\\backup-files.py<\/h4>\n<p>&nbsp;<\/p>\n<p>To run a task on certain days of the week, choose Weekly trigger and select the desired weekdays as shown below:<\/p>\n<p><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/task.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2208\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/task.png\" alt=\"task\" width=\"619\" height=\"528\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/task.png 619w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2015\/09\/task-300x256.png 300w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW Sometimes using a full-fledged backup software or the built-in Windows Backup service is an overkill for your requirements. If your need is simply to <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/02\/scheduled-backup-of-files-with-python-in-windows\/\" title=\"Scheduled backup of files with Python in Windows\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2107,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,297,15],"tags":[],"class_list":["post-2204","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","category-python","category-windows"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2204"}],"version-history":[{"count":5,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2204\/revisions"}],"predecessor-version":[{"id":2210,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2204\/revisions\/2210"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2107"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}