首页 > linux, mysql > python写的备份mysql自动上传ftp服务器

python写的备份mysql自动上传ftp服务器

2010年7月1日 eric 发表评论 阅读评论

这几天没有怎么去研究nginx,闲来无事,写了一个python脚本·写的有点乱,以后慢慢精简,慢慢进化.
功能:
备份 mysql 的数据库,备份到 /home/eric 目录下,保留5天的备份文件过期删除,自动上传到 ftp server 服务器。

#!/usr/bin/python
# Filename: mysqlbackup-ftp.py
# version 0.01
# QQ:277057817
import  os
import  time
import  sys
import  datetime
from stat import *
from ftplib import FTP

## mysql user
User = 'root'

## mysql password
Passwd = '123456'

## mysqldump command
Mysqlcommand = '/usr/bin/mysqldump'

## you want backup mysql database
Mysqldata = 'mysql'

## you want  backup to dir
tobackup = '/home/eric/'

## backup file name
backfilename = tobackup + time.strftime('%Y-%m-%d')+'.gz'

## backup gzip command
gzip_command = "%s -u%s -p%s --opt %s |gzip >%s" %(Mysqlcommand,User,Passwd,Mysqldata,backfilename)

if os.system(gzip_command)==0:
        print 'Successful backup to',backfilename
else:
        print 'Backup FAILED'

# ################删除过期备份文件#############################
# 创建文件列表
filelist=[]
filelist=os.listdir(tobackup)
## 循环删除本地前5天的备份数据
for i in range(len(filelist)):
        ft=time.gmtime(os.stat(tobackup+filelist[i])[ST_MTIME])
        ftl=time.strftime('%Y-%m-%d',ft)
        year,month,day=ftl.split('-')
        ftll=datetime.datetime(int(year),int(month),int(day))

        localt=time.gmtime()
        localtl=time.strftime('%Y-%m-%d',localt)
        year,month,day=localtl.split('-')
        localtll=datetime.datetime(int(year),int(month),int(day))
        days=(localtll-ftll).days

        if days >5:
                try:
                        os.remove(tobackup+filelist[i])
                        print 'delete is ok'
                except:
                        log=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+"  remove "+tobackup+filelist[i]+"  fail \n"
                        print log

########### FTP 上传#####################
## upload ftp server
ftp=FTP()

ftp.set_debuglevel(2)
ftp.connect('192.168.6.2','21')
ftp.login('eric','123456')

#ftp.cwd()
bufsize = 1024
file_handler = open(backfilename,'rb')
ftp.storbinary('STOR %s' % os.path.basename(backfilename),file_handler,bufsize)
ftp.set_debuglevel(0)

file_handler.close()
ftp.quit()

转载请注明:文章转载自:Eric's linux and nginx! (http://www.nginxs.com)
本文地址:http://www.nginxs.com/linux/187.html

分类: linux, mysql 标签: , ,
  1. 2017年10月25日14:55 | #1

    I’m not that much of a online reader to be honest but
    your sites really nice, keep it up! I’ll go ahead and bookmark
    your site to come back later on. Many thanks

  2. 2017年10月25日18:04 | #2

    Hi! This post couldn’t be written any better!
    Reading through this post reminds me of my previous room mate!
    He always kept talking about this. I will forward this article
    to him. Pretty sure he will have a good read.
    Thanks for sharing!

  3. 2017年10月26日01:22 | #3

    Hi, i read your blog from time to time and i own a similar one and
    i was just wondering if you get a lot of spam comments?
    If so how do you protect against it, any plugin or anything you can recommend?
    I get so much lately it’s driving me insane so any assistance is very much
    appreciated.

  4. 2017年10月26日02:24 | #4

    This is very interesting, You’re a very skilled blogger.
    I’ve joined your feed and look forward to seeking more
    of your magnificent post. Also, I’ve shared your web site in my
    social networks!

  5. 2017年10月26日08:18 | #5

    Quality content is the important to interest the people to pay a
    visit the web site, that’s what this site is providing.

  6. 2017年10月27日14:24 | #6

    I am sure this paragraph has touched all the internet visitors,
    its really really good post on building up new weblog.

  7. 2017年10月28日13:47 | #7

    Pretty component of content. I simply stumbled upon your web site and in accession capital to claim that
    I acquire in fact enjoyed account your blog posts.
    Anyway I will be subscribing for your augment or even I achievement you get entry to persistently fast.

  8. 2017年10月28日14:42 | #8

    You really make it seem so easy with your presentation but
    I find this topic to be really something that I think I would never understand.
    It seems too complex and extremely broad for me.

    I’m looking forward for your next post, I will try to get the hang of it!

  9. 2017年10月28日15:05 | #9

    Hi there to all, as I am really eager of reading
    this weblog’s post to be updated regularly. It contains good stuff.

  10. 2017年10月28日22:45 | #10

    This design is steller! You definitely know how
    to keep a reader entertained. Between your wit and your videos, I
    was almost moved to start my own blog (well, almost…HaHa!) Fantastic job.

    I really enjoyed what you had to say, and more than that, how you
    presented it. Too cool!

  11. 2017年10月29日07:44 | #11

    Phenergan Discount Isotretinoin Online Equivalent Cialis Pour Femme viagra Buy Albendazole Shipped Ups Dutasteride Free Doctor Consultation Cytotec Et La Femme

  12. 2017年10月29日09:16 | #12

    Hello to every one, because I am genuinely eager of reading this weblog’s post to
    be updated daily. It consists of good material.

  13. 2017年10月30日17:19 | #13

    Great information. Lucky me I ran across your blog by chance
    (stumbleupon). I have bookmarked it for later!

  14. 2017年10月31日11:23 | #14

    First of all I want to say great blog! I had a quick question which I’d like to ask if you do not mind.
    I was curious to know how you center yourself and clear
    your head prior to writing. I’ve had a hard time clearing my thoughts in getting my thoughts out there.
    I truly do enjoy writing however it just seems like the first 10 to 15 minutes are lost simply just trying to figure out how to begin. Any ideas or tips?
    Cheers!

  15. 2017年10月31日18:30 | #15

    I am curious to find out what blog platform you happen to be utilizing?
    I’m experiencing some small security problems with my latest site and I would
    like to find something more safeguarded. Do you have any recommendations?

  16. 2017年11月1日06:23 | #16

    Principio Attivo Paroxetina Dapoxetina Ficha Tecnica Buy Wellbutrin Online Philippines [url=http://cheapviafast.com]buy viagra[/url] Propecia Side Effects Hair Loss

  17. 2017年11月1日11:05 | #17

    Everything is very open with a clear description of the challenges.
    It was definitely informative. Your website is very helpful.
    Thank you for sharing!

  18. 2017年11月2日02:43 | #18

    Your way of explaining all in this post is truly good, all be capable of effortlessly
    know it, Thanks a lot.

  19. 2017年11月2日21:10 | #19

    I do believe all of the ideas you have offered for your
    post. They’re very convincing and will certainly
    work. Nonetheless, the posts are too short for beginners.
    Could you please lengthen them a little from next time?
    Thanks for the post.

  20. 2017年11月3日17:41 | #20

    each time i used to read smaller content that also clear their motive, and that
    is also happening with this piece of writing which I am reading now.

  21. 2017年11月5日13:23 | #21

    Levitra Generico Acquisto Cialis Di Contrabbando [url=http://costofcial.com]cheap cialis[/url] Online Order Tadalis Sx Soft Buy Propecia Hong Kong

  22. 2017年11月6日15:44 | #22

    I have read several excellent stuff here. Certainly worth bookmarking for
    revisiting. I wonder how a lot effort you place to make such a magnificent informative website.

  23. 2017年11月11日08:39 | #23

    Currently it sounds like Movable Type is the best blogging platform available right now.
    (from what I’ve read) Is that what you’re using on your
    blog?

  24. 2017年11月11日18:05 | #24

    I believe this is one of the so much vital info for me.
    And i am glad studying your article. But want to remark on few basic issues, The site taste is wonderful, the articles is really nice : D.
    Good process, cheers

  25. 2017年11月12日08:34 | #25

    Do you mind if I quote a couple of your posts as long as
    I provide credit and sources back to your blog?
    My blog is in the very same niche as yours and my users would genuinely benefit from
    a lot of the information you present here. Please let me know if this ok with you.
    Regards!

  26. 2017年11月12日18:07 | #26

    Can you carve out sliding courtyard door at home?

  27. 2017年11月12日19:57 | #27

    Great goods from you, man. I have understand your stuff previous to and you
    are just extremely great. I actually like what you have acquired here, certainly
    like what you’re stating and the way in which you say it.

    You make it entertaining and you still care for to keep it wise.
    I can not wait to read much more from you. This is really a wonderful site.

  28. 2017年11月18日21:50 | #28

    I relish, cause I discovered just what I was taking a look for.
    You’ve ended my 4 day long hunt! God Bless you man. Have
    a nice day. Bye

  29. 2017年11月19日18:11 | #29

    Have you ever considered about including a little bit more than just your articles?
    I mean, what you say is valuable and all. However just
    imagine if you added some great graphics or video clips to give your posts more, “pop”!
    Your content is excellent but with images
    and clips, this site could definitely be one of the greatest
    in its niche. Superb blog!

  30. 2017年11月22日02:50 | #30

    Levitra Valore Compro Viagra En Valencia Maxaquin [url=http://buyvarden.com]general pharmacy levitra[/url] Cost For Viagra Without Insurance 283

  31. 2017年11月22日14:13 | #31

    Hello there, You’ve done an incredible job. I will definitely digg it and personally suggest to
    my friends. I am confident they will be benefited from this website.

  32. 2017年11月25日10:45 | #32

    Wow! After all I got a web site from where I can really take valuable facts
    regarding my study and knowledge.

  33. 2017年11月28日03:17 | #33

    Safe Place Online To Order Cytotec [url=http://buysildenaf.com]generic viagra[/url] Viagra Frei Verkauflich

  34. 2017年11月29日20:51 | #34

    Wonderful site you have here but I was curious if you knew of any message boards that cover the same
    topics talked about in this article? I’d really love to
    be a part of online community where I can get feed-back from other
    experienced individuals that share the same interest.
    If you have any recommendations, please let me know. Thank you!

  35. 2017年12月5日03:34 | #35

    Kamagra Anwendung [url=http://ciali10mg.com]buy cialis[/url] Tamoxifen Buy Online Uk

  36. 2017年12月27日23:35 | #36

    online casino real money
    online casino real money
    casino online
    casino online usa
    online casino real money

  37. 2018年1月1日15:39 | #37

    casino games
    free casino games
    free casino games
    casino games
    casino games

  38. 2018年1月2日00:02 | #38

    play online casino game
    casino online
    casino on net game

  39. 2018年1月3日10:36 | #39

    loans no credit check
    loans online
    payday loans online
    payday loans no credit
    loans online

  40. 2018年1月7日23:32 | #40

    slots for free
    slots for free
    casino slots free
    free casino games slot
    free online slots

  41. 2018年1月10日09:36 | #41

    I’m really impressed with your writing skills as well as with the layout
    on your weblog. Is this a paid theme or did you modify it yourself?

    Either way keep up the nice quality writing, it’s rare
    to see a nice blog like this one today.

  42. 2018年1月12日14:14 | #42

    It is the best time to make some plans for the future and it is time to be happy.
    I have read this post and if I could I wish to suggest you few interesting things or advice.

    Perhaps you could write next articles referring to this article.
    I wish to read more things about it!

  43. 2018年1月12日21:48 | #43

    I was wondering if you ever thought of changing the structure of your
    site? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with
    it better. Youve got an awful lot of text for only having one or two pictures.
    Maybe you could space it out better?

  44. 2018年1月15日09:15 | #44

    loans for bad credit
    payday loans online no credit check
    payday loans no credit
    payday loans no credit check
    payday loans no credit check

  45. 2018年1月18日20:36 | #45

    best us casinos online
    sizzling 777 slots free online
    casino games real money
    download free casino slot games
    free slots vegas world

  46. 2018年1月18日22:21 | #46

    payday loans
    payday loans online
    payday loans online
    payday loans no credit check
    payday loans no credit check

评论分页
1 2 3 4 187
订阅评论
0+7= (必填)