Python实现备份EC2的重要文件和MySQL数据库到S3

news/2024/7/8 6:26:22

今天尝试了使用boto这个工具来用python备份文件到S3,废话不说,上代码:

1. 备份重要文件到S3:


import os
connected = 0
def connect():
    access_key = 'YOURKEY
    secret_key = 'YOURKEY'
    from boto.s3.connection import S3Connection
    global conn
    conn = S3Connection(access_key, secret_key)
    global connected
    connected = 1

def put(fileName, bucketName):
    if connected == 0:
        print 'Not connected!'
    elif connected == 1:
        local_file = fileName.strip()
        bucket = bucketName.strip()
        from boto.s3.key import Key
        b = conn.get_bucket(bucket)
        k = Key(b)
        k.key = local_file
        k.set_contents_from_filename(local_file)
        
if __name__ == '__main__':
    connect()
    sourceFolder = '/var/www/www.ttgrow.com/ttgrow/photos/storyPhotos'
    print 'story Photo sync in progress'
    for root, dirs, files in os.walk(sourceFolder):
        for file in files:
            print '  '+str(os.path.join(root,file))
            put(os.path.join(root,file),'ttgrow-photo')
    sourceFolder = '/var/www/www.ttgrow.com/ttgrow/photos/thumbnails'
    print 'thumbnail sync in progress'
    for root, dirs, files in os.walk(sourceFolder):
        for file in files:
            print '  '+str(os.path.join(root,file))
            put(os.path.join(root,file),'ttgrow-photo')
    print 'finished'

2. 备份mysql数据库到S3:

import os
connected = 0
def connect():
    access_key = 'YOURKEY'
    secret_key = 'YOURKEY'
    from boto.s3.connection import S3Connection
    global conn
    conn = S3Connection(access_key, secret_key)
    global connected
    connected = 1

def put(fileName, bucketName):
    if connected == 0:
        print 'Not connected!'
    elif connected == 1:
        local_file = fileName.strip()
        bucket = bucketName.strip()
        from boto.s3.key import Key
        b = conn.get_bucket(bucket)
        k = Key(b)
        k.key = local_file
        k.set_contents_from_filename(local_file)
        
if __name__ == '__main__':
    from datetime import datetime
    import os
    temp = datetime.today()
    fileName = '/tmp/dbbak-'+str(temp.year)+'-'+str(temp.month)+'-'+str(temp.day)+'-'+str(temp.hour)+'-'+str(temp.minute)+'.sql'
    os.system("mysqldump -h YOUR_RDS_LOCATION -u USRNAME -pPASSWORD DBNAME > "+fileName)
    print 'backup DB finished'
    connect()
    put(fileName,'ttgrow-db')

    print 'upload to S3 finished'

再把执行脚本加到定时器就每天可以定时执行了 :)


更多操作见官方文档:

http://aws.amazon.com/python/



版权所有。转载本BLOG内任何文章,请以超链接形式注明出处。

http://www.niftyadmin.cn/n/3649164.html

相关文章

手势的简单使用

第一步:定义手势 private GestureDetector detector 第二步:初始化手势并初始化事件 detectornew GestureDetector(this,new GestureDetector.SimpleOnGestureListener(){Overridepublic void onLongPress(MotionEvent e) {super.onLongPress(e);Toast.…

小程序组件转换vue组件_Vue中的组件转换入门

小程序组件转换vue组件When we build applications, we aim to make them intuitive for users. We want our users to have a smooth experience using it, and to feel our application flowing from one point to another, rather than just jump between screens. 当我们构…

Django 的css和js压缩插件:django_compressor

Django 的css和js压缩插件:django_compressor 作者:Wally Yu 今天尝试了django_conpressor,一个在django框架中压缩css和js的插件,灰常有用 我把它加载在我的base的HTML template中,原来未经压缩的css和js是&#x…

使用Gallery

画廊功能&#xff0c;在实际的开发中使用到的地方还是挺多的&#xff01; 1.重写的适配器&#xff1a; [java] view plaincopyprint?public class GalAdapter extends BaseAdapter { private Context context; private List<Integer> list; pub…

DrawerLayout和Navigation实现侧滑菜单

DrawerLayout 1.以android.support.v4.widget.DrawerLayout为根控件&#xff0c;导入: compile com.android.support:design:24.2.1 2.Drawerlayout下包裹两个控件&#xff0c;第一个是内容控件&#xff0c;第二个是侧滑控件&#xff0c;使用android:layout_gravity来指定它的滑…

nuxt.js 全局 js_如何在Nuxt.js应用程序中实现身份验证

nuxt.js 全局 jsIn this tutorial, you’ll implement authentication in a Nuxt.js app using the Auth module. For the purpose of this tutorial we’ll be using JWT for authentication. 在本教程中&#xff0c;您将使用Auth模块在Nuxt.js应用中实现身份Auth 。 就本教程…

Drawerlayout和ToolBar进行整合

首先可以看一下效果 \ 上一篇文章我们使用的是Drawerlayout和Naviagtion实现了侧滑的效果,大家可以看下http://blog.csdn.net/qq_24675479/article/details/78743924。这个项目是基于上个项目来实现的 第一步&#xff1a;我们定义一下样式,因为我们默认的Toolbar标题和图标是…

vue使用jwt加密_如何使用Vue和Node构建轻量级发票应用程序:JWT身份验证和发送发票

vue使用jwt加密介绍 (Introduction) In the previous parts of the series, we looked at how to create the User Interface of our Invoicing Application that allowed users to create and view existing invoices. In this final part of the series, you will set up per…