Skip to content

Instantly share code, notes, and snippets.

@muyesh
Created June 5, 2011 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muyesh/1008941 to your computer and use it in GitHub Desktop.
Save muyesh/1008941 to your computer and use it in GitHub Desktop.
ディレクトリを任意のS3へバックアップするスクリプト
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tarfile,gzip,os
from time import time,strftime
from boto.s3.connection import S3Connection
from boto.s3.key import Key
class S3Backup:
bucket=None
TMPDIR='/tmp/'
def __init__(self, key, secret, bucketName):
con=S3Connection(key,secret)
self.bucket=con.get_bucket(bucketName)
def _archive(self, sourceDir, fileName):
tarGzFilePath=self.TMPDIR+fileName
tar=tarfile.open(tarGzFilePath,"w:gz")
tar.add(sourceDir,self._parseFileName(sourceDir))
tar.close()
return tarGzFilePath
def _parseFileName(self, path):
fileList=path.split('/')
return fileList[len(fileList)-1]
def _upload(self,filePath):
k=Key(self.bucket)
k.key=self._parseFileName(filePath)
k.set_contents_from_filename(filePath)
def _rotate(self, count):
fileList=[]
rs=self.bucket.list()
for key in rs:
fileList.append((key.name,key))
fileList.reverse()
# remove key
for key in [keyTuple[1] for keyTuple in fileList[count:]]:
self.bucket.delete_key(key)
def backup(self,sourceDir, prefix, rotate=10):
# Create TarGz
backupFileName=prefix+strftime('%Y%m%d%H%M')+'.tar.gz'
tarGzFilePath=self._archive(sourceDir,backupFileName)
# upload
self._upload(tarGzFilePath)
# rotate
self._rotate(rotate)
# remove file
os.remove(tarGzFilePath)
# Main
import sys
# S3Backup.py accessKey secret bucketName targetPath
if __name__ == '__main__':
if len(sys.argv)<5:
print 'Usage: ./S3Backup.py accessKey secret bucketName targetPath'
exit(1)
key=sys.argv[1]
secret=sys.argv[2]
bucket=sys.argv[3]
targetDir=sys.argv[4]
rotateCount=7 if len(sys.argv)<6 else int(sys.argv[5])
s3 = S3Backup(key,secret,bucket)
s3.backup(targetDir,'backup_',rotateCount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment