SoFunction
Updated on 2024-12-19

Python Backup Program Code Implementation

A backup program for Python

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

This is a backup script that divides the directory by the current date, uses the time as the filename, and allows you to add notes to the filename.

The compression method is zip, which can be changed if you have special needs.

Instance code:


#! /usr/bin/python
#coding=utf-8
 
# This is a backup script that splits the directory by the current date, uses the time as the filename, and allows you to add notes to the filename.
# Use zip as the compression method, you can change it if you have special needs.
import os
import time
 
source = ['/home/leeicoding/workspace/j2ee','/home/leeicoding/workspace/python']
 
target_dir = '/home/leeicoding/bak'
# Get system time
today = target_dir + ('%Y%m%d')
now  = ('%H%M%S')
# Input remarks
comment = raw_input('Please enter a note:')
if len(comment) == 0:
  print('No remarks')
  target = today +  + now + '.zip'
else:
  target = today +  + now + (' ','_') + '.zip'
 
if not (today):
  (today)
  print('Create directory'+today+'Success')
 
 
# Backup command
# q silent mode r recursive directory
zip_command = 'zip -qr "%s" %s' % (target, ' '.join(source))
 
if (zip_command) == 0:
  print('Backup successful, stored in: '+target)

Thanks for reading, I hope this helps, and thanks for supporting this site!