Concise Python tutorial reading notes (ii) for important file backup

Source: Internet
Author: User

First, feasibility analysis:

Generally from the economic, technical, social, human four direction analysis.


Second, demand analysis:

Requirements analysis is what needs to be implemented, and this is clear-file backup

Several questions:

Where is our backup?

What time is the backup?

What files are backed up?

How do I store backups (file types)?

What is the name of the backup file? (Need to be plain, usually named at the current time)


Third, the implementation process:

Programme one:

#!/usr/lib/env python

Import OS
Import time
backlist=['/etc ', '/root ']
To= '/mnt/'

Target=to+time.strftime ('%y%m%d%h%m%s ') + '. tar.gz '

Gz_command= "Tar-czf%s%s"% (target, '. Join (backlist))

If Os.system (Gz_command) ==0:
print ' successfull '
Else
print ' failed '



Improvement Programme II:

Scenario two focuses on the multi-build date subdirectory, which is more clear
Today = Target_dir + time.strftime ('%y%m%d ')
now = Time.strftime ('%h%m%s ')

If not os.path.exists (today):
Os.mkdir (today) # Make Directory
print ' Successfully created directory ', today

target = today + Os.sep + now + '. Zip '
Zip_command = "Zip-qr '%s '%s"% (target, ". Join (source))


Note: OS.SEP representative/or \



Improvement Scenario Three: You can add annotations to the backup file name:

#!/usr/bin/env python

Import OS

Import time

source=['/var/log ', '/etc/ssh ']


target= '/mnt/'


Today=target+time.strftime ('%y%m%d ')

Now=time.strftime ('%h%m%s ')

Comment=raw_input (' pls input the comment--> ')


If Len (comment) ==0:

target1=today+os.sep+now+ '. tar.gz '

Else

target1=today+os.sep+now+ ' _ ' +comment.replace (', ' _ ') + '. tar.gz '

If not os.path.exists (today):

Os.mkdir (today)

Comm= "Tar-czf%s%s"% (Target1, ". Join (source))


If Os.system (comm) ==0:

print ' successfull '

Else

print ' failed '



Scenario Four: increasing interactivity

The ideal way to create these archives is to use the ZipFile and Tarfile modules separately. They are part of the Python standard library and can be

For your use. Using these libraries avoids the use of os.system, the deprecated function.

On the other hand, you can interactively input the directories that need to be backed up and add them to the list by using the Extend method.



This article is from the "Victor's Struggle" blog, please be sure to keep this source http://victor2016.blog.51cto.com/6768693/1875945

Concise Python tutorial reading notes (ii) for important file backup

Related Article

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.