Menu

1 / 2

Python Backup Automation: Never Lose Data Again (Free Alternative to BackBlaze)

DEV Community·Brad·6 months ago
#BUJ3iKYj
Reading 0:00
15s threshold

BackBlaze costs $99/year. Building your own backup system with Python takes about 100 lines and costs almost nothing.

Incremental Backup Engine

Only back up files that have changed:

import hashlib import shutil import json from pathlib import Path from datetime import datetime

MANIFEST = Path . home () / ' .backups ' / ' manifest.json '

def file_hash ( path : Path ) -> str : h = hashlib . sha256 () with open ( path , " rb " ) as f : for chunk in iter ( lambda : f . read ( 8192 ), b "" ): h . update ( chunk ) return h . hexdigest ()

def load_manifest () -> dict : if MANIFEST . exists (): with open ( MANIFEST ) as f : return json . load ( f ) return {}

def backup_directory ( source : Path , dest : Path ) -> dict : manifest = load_manifest () source_key = str ( source ) if source_key not in manifest : manifest [ source_key ] = {}

stats = { " new " : 0 , " updated " : 0 , " unchanged " : 0 } timestamp = datetime . now (). strftime ( " %Y%m%d_%H%M%S " ) backup_dest = dest / timestamp

for file in source .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.