Use python to write backup scripts in windows and Ubuntu

Source: Internet
Author: User

1. Windows 7:

Install python2.x or 3.x,Programming LanguageNote that most of the tutorials on the Internet are 2. x.

After cmd, before running Python XXX. py, you need to set the environment variable and write the path name during Python installation to the variable value. The variable name is still the path

In my situation, the shortcut key FN + F5 is used to run the py file. The packaging fails, but Python XXX. py runs the packaging script normally in cmd,CodeAs follows:

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

# Specify the name and directory of the file to be backed up in the list
Source = [R ' "D: \ Kankan" ' ]

# Back up data to the following directory
Target_dir = ' H :\\ '

# The backup file is a zip file with the file name: year, month, hour, minute, second. Zip
Target = target_dir + ' Backup ' + ' . Zip '

# Use the WinRAR command line to compress the file, provided that WinRAR is in the path of WindowsXP
Zip_command = " RAR a % S % s " % (Target, ' ' . Join (source ))


# Run this backup Program To back up
If OS. System (zip_command) = 0 :
Print ' Successful backup ' , Target
Else :
Print ' Backup failed! '

Note that when you use the RAR a command to package files or folders, make sure that the environment variables have been configured, and the path also contains the installation path of RAR, control panel-system-advanced-environment variables-System Variables

2. Linux Ubuntu

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

# Specify the name and directory of the file to be backed up in the list
Source = [R ' "/Home/xiaonuo/somefile" ' ]

# Back up data to the following directory
Target_dir = ' /Home/xiaonuo/zipfile '

# The backup file is a zip file with the file name: year, month, hour, minute, second. Zip
Target = target_dir + ' Backup ' + ' . Zip '

# Use the WinRAR command line to compress the file, provided that WinRAR is in the path of WindowsXP
Zip_command = " Zip-QR % S % s " % (Target, ' ' . Join (source ))


# Run this backup program to back up data
If OS. System (zip_command) = 0 :
Print ' Successful backup ' , Target
Else :
Print ' Backup failed! '

In most Linux versions, python is installed by default. You can directly enter Python (PATH) xxx. py in the terminal to execute the prepared py code. The zip command is provided by the system and does not require multiple methods.

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.