Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What should be noticed in the backup script code of 10.4 in Python

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces what you need to pay attention to in Python 10.4 backup script code, the content is very detailed, interested friends can refer to, hope to be helpful to you.

There are some points to pay attention to in the code of version 4, the backup script in example 10.4.

#! / usr/bin/python

# Filename: backup_ver4.py

Import os

Import time

# 1. The files and directories to be backed up are

Specified in a list.

Source = ['/ home/swaroop/byte','/ home/swaroop/bin']

# If you are using Windows, use source =

[ringing C:\ Documents', rudder:\ Work'] or something

Like that

# 2. The backup must be stored in a main

Backup directory

Target_dir ='/ mnt/e/backup/' # Remember to

Change this to what you will be using

# 3. The files are backed up into a zip file.

# 4. The current day is the name of the subdirectory

In the main directory

Today = target_dir + time.strftime ('% Y% m% d')

# The current time is the name of the zip archive

Now = time.strftime ('% H% M% S')

# Take a comment from the user to create the name

Of the zip file

Comment = raw_input ('Enter a comment->')

If len (comment) = = 0: # check if a comment was entered

Target = today + os.sep + now + '.zip'

Else:

Target = today + os.sep + now +'_'+\

Comment.replace (','_') + '.zip'

# Notice the backslash!

# Create the subdirectory if it isn't already there

If not os.path.exists (today):

Os.mkdir (today) # make directory

Print 'Successfully created directory', today

# 5. We use the zip command (in Unix/Linux) to put

The files in a zip archive

Zip_command = "zip-qr'% s"% (target,'

'.join (source))

# Run the backup

If os.system (zip_command) = = 0:

Print 'Successful backup to', target

Else:

Print 'Backup FAILED'

With regard to the code related to the Python tutorial, it is mentioned that "I would also like to have an optimization that uses the tar command instead of the zip command." One advantage of this is that backups are faster and smaller when you use the tar and gzip commands together. If you want to use these archives in Windows, WinZip can also easily handle these .tar.gz files. The tar command is available by default on most Linux/Unix systems. Windows users can also download and install it.

The command string will now be:

Tar = 'tar-cvzf% s% s-X / home/swaroop/excludes.txt'

% (target,''.join (srcdir))

What needs to be added is that Windows users install LibArchive after the command is bsdtar, and if you want to use the-z option, you also need to install gzip software.

In the code

Target = today + os.sep + now + '.zip'

Change to

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

Use

Bsdtar-cvzf% s% s-X / home/swaroop/excludes.txt'% (target,'. Join (source))

The original English version does not replace the content of zip with tar. It is suggested that zipfile and tarfile in the Python standard library be used to rewrite ver5.

About Python 10.4 backup script code need to pay attention to what should be shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report