Python backup program code implementation

  • 2020-05-27 05:58:02
  • OfStack

Python a backup program

This is a backup script. Please change the route yourself.

This is a backup script. It is divided into directories according to the current date, with the time as the file name, and can add remarks to the file name.

With zip as the compression mode, special requirements can be changed.

Example code:



#! /usr/bin/python
#coding=utf-8
 
# This is a 1 Three backup scripts , Divide directories by current date , Take the time as the file name , And you can add a note to the file name .
# In order to zip Mode as a compression mode ,  Special requirements can be changed .
import os
import time
 
source = ['/home/leeicoding/workspace/j2ee','/home/leeicoding/workspace/python']
 
target_dir = '/home/leeicoding/bak'
# Acquisition system time 
today = target_dir + time.strftime('%Y%m%d')
now  = time.strftime('%H%M%S')
#  Enter the note 
comment = raw_input(' Please enter remarks :')
if len(comment) == 0:
  print(' No remark ')
  target = today + os.sep + now + '.zip'
else:
  target = today + os.sep + now + comment.replace(' ','_') + '.zip'
 
if not os.path.exists(today):
  os.mkdir(today)
  print(' Create a directory '+today+' successful ')
 
 
#  The backup command 
# q  silently  r Recursive directory 
zip_command = 'zip -qr "%s" %s' % (target, ' '.join(source))
 
if os.system(zip_command) == 0:
  print(' Backup successfully stored : '+target)

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: