Use Python to write backup scripts under Windows and Ubuntu

Source: Internet
Author: User
Tags rar zip

1.windows 7:

Install python2.x or 3.x, programming languages have different places, need attention, the web is mostly 2.x tutorials.

After CMD, before running Python xxx.py, you need to set the environment variable, write the path name of the Python installation to the variable value, the variable name or path

I encountered the situation is in the Py file Direct shortcut key Fn+f5 Run, packaging failed, but in cmd python xxx.py normal run package script, the code is as follows:

The code is as follows Copy Code

#-*-coding:cp936-*-
Import OS
Import time

# in the list, specify the file name and or directory that you want to back up
Source = [R ' "D:kankan"]

# Backup to the following directory

The code is as follows Copy Code
Target_dir = ' h:\ '

# Backup to zip file, file name is: The time of day and month. zip

The code is as follows Copy Code
target = Target_dir + ' backup ' + '. Zip '

# Compress files with winrar command line, provided winrar is in Windows XP path

The code is as follows Copy Code
Zip_command = "rar A%s%s"% (target, '. Join (source))


# Run this backup program to back up

The code is as follows Copy Code
If Os.system (zip_command) = = 0:
print ' successful backup to ', target
Else
print ' Backup failed! '

Note When using the RAR a command to package files or folders, you need to ensure that the environment variables have been configured, path and RAR installation path, Control Panel-system-advanced-environment variables-system variables

2.linux Ubuntu

The code is as follows Copy Code

#-*-coding:cp936-*-
Import OS
Import time

# in the list, specify the file name and or directory that you want to back up

The code is as follows Copy Code
Source = [R ' "/home/xiaonuo/somefile"]

# Backup to the following directory

The code is as follows Copy Code
Target_dir = '/home/xiaonuo/zipfile '

# Backup to zip file, file name is: The time of day and month. zip

The code is as follows Copy Code
target = Target_dir + ' backup ' + '. Zip '

# Compress files with winrar command line, provided winrar is in Windows XP path

The code is as follows Copy Code
Zip_command = "Zip-qr%s%s"% (target, '. Join (source))


# Run this backup program to back up

The code is as follows Copy Code
If Os.system (zip_command) = = 0:
print ' successful backup to ', target
Else
print ' Backup failed! '

Linux version most of the default installed Python, in the terminal directly into the Python (path) xxx.py can execute the written PY code, Zip command is the system itself, do not need multiple.

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.