Python---backup directories and files

Source: Internet
Author: User

To make a backup of important files:

Ideas:

1. The files and directories that need to be backed up are specified by a list.
2. The backup should be saved in the primary backup directory.
3. File backup as a tar.gz file.
The name of the 4.zip archive is the current date and time.
5. We use the standard tar.gz command.

#!/usr/bin/env python#coding=utf-8#filename: backup_ver2.pyimport osimport time#1.  List of backup files source = ['/home/medees/pictures/',  '/home/medees/script/'] #If  you are  Using windows,use source = [r ' C:\Documents ',  r ' D:\Work '] or something  like that#2.  the home directory where the backup files are stored target_dir =  '/home/medees/backup/'    #Remember  to  change this to what you will be using#3. the files are  backed  up into a zip file#4.  the day of the main backup directory Today = target_dir  + time.strftime ('%y%m%d ') #  compressed current time now = time.strftime ('%h%m%s ') #Take  a  comment from the user to create the name of the zip  Filecomment = raw_input (' enter a comment -->  ') If len (comment)  ==  0:     #check  if a comment was entered    target =  today + os.sep + now +  '. tar.gz ' else:    target =  today + os.sep + now +  ' _ '  +          comment.replace ('   ',  ' _ ')  +  '. tar.gz ' #如果当天日期的目录不存在则创建if  not  Os.path.exists (Today):     os.mkdir (today)     print  ' successfully  created directory ', today#5. we use the zip command  (in  Unix/linux)  to put the files in a zip archiv#zip_command =  " zip -qr  '%s '  %s  %  (target,  '   '. Join (source)) tar_command =  "tar  -cvzf %s %s " %  (target,  '  ". Join (source)) #Run  the backupif  Os.system (Tar_command)  ==&NBsp;0:    print  ' Successful backup to ', targetelse:     print  ' backup failed '


This article is from the "Notes" blog, so be sure to keep this source http://sunflower2.blog.51cto.com/8837503/1546651

Python---backup directories and files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.