首页 > 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. 2018年5月16日23:26 | #1

    online pharmacy
    aarp recommended canadian pharmacies
    canadian online pharmacy

  2. 2018年5月20日16:20 | #2

    Trusted Online Pharmacies
    aarp recommended canadian pharmacies
    Online Canadian Pharmacies

  3. 2018年5月21日10:52 | #3

    gundrymd foods that contain polyphenols probiotics [url=https://probiotics.us.com]foods that contain polyphenols[/url]

  4. 2018年5月29日03:20 | #4

    online pharmacy without scripts
    best canadian mail order pharmacies
    online pharmacies

  5. 2018年5月30日22:36 | #5

    canadian pharmacy reviews
    canadian pharmacies online prescriptions
    canadian online pharmacy

  6. 2018年6月17日08:53 | #6

    I actuaⅼly still can’t quite think I could аlways be one of those
    reading the important ցuіdelines found оn your web site.
    My family and I are really thankful for the generosity and ffor giving me the
    poѕsibіlity to pursue my chosen career рath. Many thanks for thе important information I obtained
    fгom уour website.

  7. 2018年7月16日19:03 | #7

    We are your native garage door restore expert.

  8. 2018年7月29日22:33 | #8

    You must spend time on Twitter and get to know individuals.

  9. 2018年9月20日00:46 | #9

    Hi there, I enjoy reading all of your post. I wanted to write a little comment
    to support you.

  10. 2018年9月25日10:44 | #10

    Unquestionably consider that that you said. Your favourite reason appeared to
    be on the net the easiest factor to take note of.
    I say to you, I certainly get annoyed while people think about concerns that
    they plainly do not recognise about. You controlled
    to hit the nail upon the highest and also defined out the whole thing
    without having side effect , other folks could take a signal.
    Will probably be back to get more. Thank you

  11. 2018年10月4日09:49 | #11

    I studied in just a few faculties for acting.

  12. 2018年10月5日00:24 | #12

    Hi there, yeah this piece of writing is really pleasant and I have learned lot of things from it on the topic of
    blogging. thanks.

  13. 2018年10月5日11:58 | #13

    Hey there superb website! Does running a blog like this take a lot of work?
    I have no knowledge of computer programming but I was hoping to start my own blog soon. Anyhow,
    if you have any suggestions or tips for new blog owners
    please share. I know this is off subject however I simply needed to ask.
    Cheers!

  14. 2018年10月6日05:28 | #14

    Hi, I do think this is a great website. I stumbledupon it
    ;) I’m going to come back yet again since I saved as a
    favorite it. Money and freedom is the best way to change, may you be rich and continue to help others.

  15. 2018年10月6日12:32 | #15

    Good post. I’m facing many of these issues as well..

  16. 2018年10月7日02:26 | #16

    Excellent items from you, man. I have remember your stuff previous to and you are
    simply extremely wonderful. I actually like what you’ve obtained right
    here, really like what you’re saying and the best way in which you assert it.
    You make it entertaining and you continue to take care of to stay it wise.
    I can not wait to read much more from you. This is actually
    a wonderful website.

  17. 2018年10月7日04:37 | #17

    Unquestionably believe that which you stated.

    Your favorite reason appeared to be on the net the easiest thing to be aware of.
    I say to you, I certainly get annoyed while people think about worries
    that they just do not know about. You managed to hit
    the nail upon the top and defined out the whole thing without
    having side-effects , people can take a signal.

    Will likely be back to get more. Thanks

  18. 2018年10月7日18:54 | #18

    I don’t even understand how I ended up here, however I thought this submit was once good.
    I don’t understand who you’re but definitely you are going to a well-known blogger should you aren’t
    already. Cheers!

  19. 2018年10月7日20:18 | #19

    fantastic points altogether, you just gained a new reader.
    What would you suggest in regards to your submit that you simply made some days in the past?
    Any positive?

  20. 2018年10月9日17:00 | #20

    May I simply just say what a relief to discover
    somebody who really understands what they’re talking about
    online. You certainly understand how to bring a problem to
    light and make it important. A lot more people ought to
    look at this and understand this side of the story.
    I was surprised that you’re not more popular because you most certainly have the gift.

  21. 2018年10月10日18:00 | #21

    Everything is very open with a very clear description of the challenges.
    It was definitely informative. Your website is useful. Many thanks for sharing!

  22. 2018年10月11日21:43 | #22

    Wow, this paragraph is pleasant, my sister is analyzing these
    things, thus I am going to tell her.

  23. 2018年11月11日06:33 | #23

    You actually make it seem so easy with your presentation but I find
    this topic to be actually something which I think I would never understand.

    It seems too complicated and extremely broad for
    me. I am looking forward for your next post, I’ll try to
    get the hang of it!

  24. 2018年11月11日15:26 | #24

    Its not my first time to go to see this website, i am visiting this site dailly and get nice
    information from here daily.

  25. 2018年11月12日04:49 | #25

    What i do not understood is in reality how you are not actually a
    lot more well-preferred than you might be now.

    You’re so intelligent. You recognize thus considerably in relation to this subject, produced me in my view believe it from a lot of varied angles.

    Its like men and women don’t seem to be involved until it’s something to do with Girl gaga!
    Your individual stuffs great. Always deal with it up!

  26. 2018年11月13日00:14 | #26

    Hello there! Quick question that’s totally off topic.

    Do you know how to make your site mobile friendly?
    My web site looks weird when viewing from my apple iphone.
    I’m trying to find a template or plugin that might be able to correct this
    problem. If you have any suggestions, please share. Many thanks!

  27. 2018年11月14日21:06 | #27

    Very nice article, just what I was looking for.

  28. 2018年11月15日01:45 | #28

    I was curious if you ever considered changing the layout of your
    blog? 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 1 or
    two pictures. Maybe you could space it out better?

  29. 2018年11月15日10:14 | #29
  30. 2018年11月15日14:00 | #30

    This is a topic that’s close to my heart… Best wishes!
    Where are your contact details though?

  31. 2018年11月17日02:17 | #31

    Hi! This is kind of off topic but I need some advice from an established
    blog. Is it tough to set up your own blog?
    I’m not very techincal but I can figure things out pretty fast.
    I’m thinking about creating my own but I’m not sure where to
    start. Do you have any points or suggestions?
    Cheers

  32. 2018年11月17日10:34 | #32

    Sling tv coupons and promo codes for november 2018
    What a information of un-ambiguity and preserveness of precious familiarity regarding unexpected feelings.
    Sling tv coupons and promo codes for november 2018

  33. 2018年11月17日22:28 | #33

    It’s wonderful that you are getting thoughts from this paragraph as well as from our argument made at this time.

  34. 2018年11月18日23:23 | #34

    Heya i’m for the first time here. I found this board and
    I find It truly useful & it helped me out a lot. I hope to give something back and aid others like you helped me.

  35. 2018年12月1日12:33 | #35
  36. 2018年12月5日06:37 | #36

    https://essaywritingservice2018.com/ – essay writing service

  37. 2018年12月7日18:41 | #37
评论分页
1 2 187
订阅评论
0+8= (必填)