Concise Python Tutorials

Source: Internet
Author: User
Tags date zip backup linux

We've studied a lot of the Python language and now we're going to learn how to combine that with the content. We will design a program that can do something that is really useful.

Problem

The question I asked was: I want a program that can create a backup for all my important files.

Although this is a simple question, the problem itself does not give us enough information to solve it. Further analysis is required. For example, how do we determine which files to back up? Where is the backup saved? How do we store backups?

After properly analyzing the problem, we began to design our program. We made a list of how our program should work. For this question, I have created the following list to show how I can make it work. If you design it, you may not be able to solve the problem--everyone has a way of doing things, which is normal.

Files and directories that need to be backed up are specified by a list.

The backup should be saved in the primary backup directory.

The file is backed up into a zip file.

The name of the ZIP archive is the current date and time.

We use the standard ZIP command, which is usually provided by default with the Linux/unix distribution. Windows users can use the Info-zip program. Note that you can use any archive command, as long as it has a command-line interface, so that we can pass parameters to it from our script.

Solution

When we basically complete the design of the program, we can write code, it is the implementation of our solution.

Version One

Example 10.1 Backup script-version One

#!/usr/bin/python
# Filename:backup_ver1.py
import os
import time
# 1. The files and directories to is backed up are specified in a list.
Source = ['/home/swaroop/byte ', '/home/swaroop/bin ']
# If You are are using Windows, use Source = [R ' C:\Documents ', R ' d:\w Ork '] or something like that
# 2. The backup must is stored in a main backup directory
target_dir = '/mnt/e/backup/' # Remember to change this to what Y ou'll be using
# 3. The files are backed up into a zip file.
# 4. The name of the ZIP archive is the current date and time
target = Target_dir + time.strftime ('%y%m%d%h%m%s ') + '. Zip '
# 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 '

(source file: code/backup_ver1.py)

Output

$ python backup_ver1.py
Successful backup to /mnt/e/backup/20041208073244.zip

Now that we're in the beta, we're testing whether our programs are working correctly. If it is different from what we expect, we have to debug our program to eliminate flaws (errors) in the program.

How it works

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.