Remote backup of enterprise data via Python+ftps

Source: Internet
Author: User
Tags ssl certificate

First, demand analysis

Friends Company has an ERP server to do a timed output backup, set the output directory is D:\backup\ month Day directory, where the current date (similar to 20171011) This is the server scheduled backup automatically generated and output to this directory. You want to automatically back up to a remote server for data offsite backup.

Ideas:

Python periodically checks for the existence of a directory backed up with the current date every day, does not exist to create the current date itself, and deletes the old directory from the previous. Compress the current backup data directory and upload it to the remote FTPS server. Join task Schedule 11.30 per day (automatic backup at 11 points).


Second, the Code

#cat upload.py

#coding:utf-8import urllib,urllib2from ftplib import ftp_tlsimport osimport  reimport sysimport timeimport zipfileimport datetimeyestodaydir =  ( Datetime.date.today ()  - datetime.timedelta (Days=1)). Strftime ("%y%m%d") file_root_dir=  "d:\\ Backup "Todaydir = str (Time.strftime ("%y%m%d ", Time.localtime ())) Todayfilename = todaydir  +  '. zip ' flag = 1# #压缩函数def  zip_dir (dirname,zipfilename):     Filelist = []    if os.path.isfile (dirname):         filelist.append (dirname)     else :         for root, dirs, files in os.walk (dirname):             for name in files:             &nBsp;   filelist.append (Os.path.join (root, name))               zf = zipfile. ZipFile (zipfilename,  "W",  zipfile.zlib.deflated)     for tar in  Filelist:        arcname = tar[len (dirname):]         zf.write (Tar,arcname)     zf.close () #创建以当前日期的目录, and remove yesterday Catalog Def check_bak_dir ():     global flag    os.chdir (File_ Root_dir)     if not os.path.exists (todaydir):         print ("no %s"  %todaydir)         os.mkdir ( Todaydir)   #创建目录         FLAG = 0             #通过FLAg标志位来确定是否新创建目录         return flag    else:         pass        if os.path.exists (YestodayDir):         os.system ("rmdir %s /q/s"  %YestodayDir)       #删除昨天的备份       ## #上传到服务器def  upload (filename):     os.chdir (File_root_dir)     ftp = ftp_tls ()     ## Fill in your FTP username and connection port     ftp.connect ("Your ip or domain", port)      ftp.login (' Ftpuser ',  ftpuser ')      ftp.prot_p ()      # #服务器端存放的目录     ftp.cwd ("Home/back")            upload_file=unicode (filename,  "UTF8")    #windows     f =  open (upload_file,  ' RB ')    ftp.storbinary (' stor %s '  % os.path.basename (Upload_file), F)      f.close ()     ftp.quitif __name__ ==  "__main__":     check_bak_dir ()         if os.path.exists (TodayDir)  and FLAG:      #如果目录存在前不是新创建, compressing uploads          print ("diractory is ziping ... ")         zip _dir (Todaydir,todayfilename)         print ("UPLOAD BAKCUP  .....   ")         upload (todayfilename)          print ("today %s upload success!"  %todayfilename)         print


Iii. Adding a scheduled task

Start-to-control panel-Scheduled Tasks-Add a scheduled task like this:

650) this.width=650; "title=" Qq20171011155044.png "alt=" Wkiom1neoecx53laaag8lwv1hym639.png "src=" https:// S5.51cto.com/wyfs02/m01/08/3a/wkiom1neoecx53laaag8lwv1hym639.png "/>


Iv. Manual execution of the verification results as follows

650) this.width=650; "title=" 55.png "alt=" Wkiol1ndy26iiblzaagleh-k2vw348.png "src=" https://s4.51cto.com/wyfs02/M01 /a6/ed/wkiol1ndy26iiblzaagleh-k2vw348.png "/>

Login FTPs to see the uploaded 20171011.zip compressed backup file.

Note: The code here for privacy security to remove the FTP address and port and user name and password, it is also recommended to use FTP + SSL certificate login. To ensure safety.

This article is from the "Learning, learning" blog, please be sure to keep this source http://dyc2005.blog.51cto.com/270872/1971456

Remote backup of enterprise data via Python+ftps

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.