mysql - a bash script to dump a database -
how write bash script dump database , restore it. there should 2 arguments. first 1 name of db going dumped , 1 name of db in going restore dump data.
i got python script take dump , upload s3. think better bash script:
import datetime import subprocess, tarfile, os, s3, tempfile #mysql mysql_user = "xxxx" mysql_pass = "xxx" mysql_db = "xxxxx" mysql_host = "localhost" mysql_dump = "mysqldump" aws_access_key_id = "xxxxxxxxxxxx" aws_secret_access_key = "yyyyyyyyyyyyyyyyyyyy" bucket_name = "bucket" folder = "backup/" keep = 5 ext_time = datetime.datetime.strftime(datetime.datetime.now(), '%y-%m-%dt%h:%m') print "start mysqldump..." proc1 = subprocess.popen(mysql_dump + " --no-create-info -u %s -p%s -x --databases %s" % (mysql_user, mysql_pass, mysql_db), shell=true,stdout=subprocess.pipe,stderr=subprocess.stdout) t1 = tempfile.namedtemporaryfile() t1.write(proc1.communicate()[0]) tar = tarfile.open( (os.path.join(os.curdir, mysql_db + "_%s.tar.gz" % (ext_time))), "w|gz") tar.add(t1.name, mysql_db + "_data.sql") t1.close() tar.close() print "uploading s3..." conn = s3.awsauthconnection( aws_access_key_id, aws_secret_access_key ) tardata = open(os.path.join(os.curdir, mysql_db + "_%s.tar.gz" % ext_time) , "rb").read() response = conn.put(bucket_name, folder + mysql_db + "_%s.tar.gz" % ext_time, s3.s3object(tardata)) if response.http_response.status == 200 : print "sucessfully uploaded archive amazon s3" else: print "uploading database dump amazon s3 not successful" os.remove(os.path.join(os.curdir, mysql_db + "_%s.tar.gz" % (ext_time)))
Comments
Post a Comment