{"id":253,"date":"2025-11-20T15:11:16","date_gmt":"2025-11-20T15:11:16","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=253"},"modified":"2025-11-20T15:11:16","modified_gmt":"2025-11-20T15:11:16","slug":"40-real-world-python-projects-automatic-backup-script","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=253","title":{"rendered":"40 &#8211; Real-World Python Projects &#8211; Automatic Backup Script"},"content":{"rendered":"\n<h6 class=\"wp-block-heading\"><\/h6>\n\n\n\n<p>A Python tool that <strong>automatically backs up your important files<\/strong> to another folder, external drive, or cloud folder (Google Drive\/OneDrive).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfaf <strong>What This Backup Script Can Do<\/strong><\/h1>\n\n\n\n<p>\u2714 Automatically copies all files from source \u2192 backup folder<br>\u2714 Creates a new backup folder with date (optional)<br>\u2714 Logs which files were copied<br>\u2714 Can run daily\/weekly using scheduler<br>\u2714 Supports multiple source folders<br>\u2714 Avoids copying duplicates<br>\u2714 Shows backup summary<br>\u2714 Works on Windows, Mac, Linux<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\uddf0 <strong>Python Modules Used<\/strong><\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>shutil<\/code> \u2013 copying files<\/li>\n\n\n\n<li><code>os<\/code> \u2013 file paths<\/li>\n\n\n\n<li><code>datetime<\/code> \u2013 timestamp folders<\/li>\n\n\n\n<li><code>hashlib<\/code> \u2013 prevents duplicate backup<\/li>\n\n\n\n<li><code>schedule<\/code> \u2013 auto backups<\/li>\n<\/ul>\n\n\n\n<p>All are <strong>built-in<\/strong> (no installation needed), except schedule (optional):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install schedule\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udcc1 Folder Structure<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>AutoBackup\/\n\u2502\u2500\u2500 backup.py\n\u2502\u2500\u2500 config.json\n\u2502\u2500\u2500 logs.txt\n\u2502\u2500\u2500 backups\/\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udccc <strong>config.json Example<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"source_paths\": &#91;\n        \"C:\/Users\/Sameer\/Documents\",\n        \"C:\/Users\/Sameer\/Desktop\/Projects\"\n    ],\n    \"backup_path\": \"D:\/Backups\",\n    \"create_daily_folder\": true\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\udde9 <strong>FULL WORKING BACKUP SCRIPT (backup.py)<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nimport shutil\nimport json\nfrom datetime import datetime\nimport hashlib\n\n# Load config\nconfig = json.load(open(\"config.json\"))\nSOURCE_PATHS = config&#91;\"source_paths\"]\nBACKUP_PATH = config&#91;\"backup_path\"]\nDAILY_FOLDER = config&#91;\"create_daily_folder\"]\n\n# Create daily backup folder\nif DAILY_FOLDER:\n    today = datetime.now().strftime(\"%Y-%m-%d\")\n    BACKUP_PATH = os.path.join(BACKUP_PATH, today)\n\nos.makedirs(BACKUP_PATH, exist_ok=True)\n\ndef file_hash(path):\n    \"\"\"Return MD5 hash of a file to prevent duplicate backup.\"\"\"\n    hasher = hashlib.md5()\n    with open(path, 'rb') as f:\n        buf = f.read()\n        hasher.update(buf)\n    return hasher.hexdigest()\n\ndef backup_files():\n    logs = &#91;]\n    existing_hashes = set()\n\n    # Load existing backup hashes\n    for root, dirs, files in os.walk(BACKUP_PATH):\n        for file in files:\n            full_path = os.path.join(root, file)\n            existing_hashes.add(file_hash(full_path))\n\n    for src in SOURCE_PATHS:\n        for root, dirs, files in os.walk(src):\n            for file in files:\n                src_file = os.path.join(root, file)\n\n                # Skip duplicates\n                if file_hash(src_file) in existing_hashes:\n                    continue\n\n                rel_path = os.path.relpath(root, src)\n                dest_dir = os.path.join(BACKUP_PATH, rel_path)\n                os.makedirs(dest_dir, exist_ok=True)\n\n                dst_file = os.path.join(dest_dir, file)\n                shutil.copy2(src_file, dst_file)\n                logs.append(f\"Copied: {src_file} \u2192 {dst_file}\")\n\n    # Save logs\n    with open(\"logs.txt\", \"a\") as f:\n        f.write(\"\\n\".join(logs) + \"\\n\")\n\n    print(\"Backup completed.\")\n    print(f\"Total files copied: {len(logs)}\")\n\n\nif __name__ == \"__main__\":\n    backup_files()\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u25b6 Run Manually<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>python backup.py\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u23f2\ufe0f <strong>Auto Backup Every Day<\/strong><\/h1>\n\n\n\n<p>Create: <strong>schedule_backup.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import schedule\nimport time\nimport os\n\ndef run_backup():\n    os.system(\"python backup.py\")\n\nschedule.every().day.at(\"09:00\").do(run_backup)\n\nwhile True:\n    schedule.run_pending()\n    time.sleep(1)\n<\/code><\/pre>\n\n\n\n<p>Runs backup every day at <strong>9 AM<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Python tool that automatically backs up your important files to another folder, external drive, or cloud folder (Google Drive\/OneDrive). \ud83c\udfaf What This Backup Script Can Do \u2714 Automatically copies all files from source \u2192 backup folder\u2714 Creates a new backup folder with date (optional)\u2714 Logs which files were copied\u2714 Can run daily\/weekly using scheduler\u2714 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-253","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=253"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/253\/revisions"}],"predecessor-version":[{"id":254,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/253\/revisions\/254"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}