SoFunction
Updated on 2024-11-13

Example of writing scripts in Python to realize full and incremental backups

Demand:

There are two folders dst and src under /root/backup. requested full backup on Monday and incremental backup on rest of the days. Backup from src to dst.

Thoughts and Key Points:

Create a file with a dictionary of src filenames and their md5 values.

A full backup writes the filename and md5 value inside a file. cPickle's Knowledge Points.

Incremental backup when comparing whether the file name is in the key, no need to backup; if so, whether the md5 value of the file has changed, changed need to backup

() splice path, (), ()

(What is the) day of the week?

cPickle, which records all Python variable types non-destructively. File operations.

Use of tarfile for file packing

hashlib is used to calculate the value of the file md5. Be careful not to open a file at a time, 4k to open, to prevent the opening of a very large file burst memory.

with file() can open a file after not()

#!/usr/bin/env python
import time
import os
import cPickle as p
import tarfile
import hashlib
baseDir = '/root/backup'
srcDir = 'src'
dstDir = 'dst'
fullName = "full_%s_%" % (srcDir, ('%Y%m%d'))
incrName = "incr_%s_%" % (srcDir, ('%Y%m%d'))
md5file = ''
def md5sum(fname):
 m = hashlib.md5()
 with file(fname) as f:
  while True:
   data = (4096)
   if len(data) == 0:
    break
   (data)
 return ()
def fullBackup():
 md5Dict = {}
 fileList = ((baseDir,srcDir))
 for eachFile in fileList:
  md5Dict[eachFile] = md5sum((baseDir,srcDir,eachFile))
 with file((baseDir,dstDir,md5file),'w') as f:
  (md5Dict,f)
 tar = ((baseDir,dstDir,fullName),'w:gz')
 (baseDir)
 (srcDir)
 ()
def incrBackup():
 newmd5 = {}
 fileList = ((baseDir,srcDir))
 for eachFile in fileList:
  newmd5[eachFile] = md5sum((baseDir,srcDir,eachFile))
 with file((baseDir,dstDir,md5file)) as f:
  storedmd5 = (f)
 tar = ((baseDir,dstDir,incrName),'w:gz')
 (baseDir)
 for eachKey in newmd5:
  if (eachKey not in storedmd5) or (newmd5[eachKey] != storedmd5[eachKey]):
   ((srcDir,eachKey))
 ()
 with file((baseDir,dstDir,md5file),'w') as f:
  (newmd5,f)
def main():
 if ('%a') == 'Mon':
  fullBackup()
 else:
  incrBackup()
if __name__ == '__main__':
 main()
~  

The above example of writing scripts in Python to achieve full and incremental backups is all that I have shared with you, and I hope that it will give you a reference, and I hope that you will support me more.