Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
# Copyright 2013 Percona LLC and/or its affiliates
set -e
if [ -z "$3" ]; then
echo "rename_db <server> <database> <new_database>"
exit 1
fi
db_exists=`mysql -h $1 -e "show databases like '$3'" -sss`
if [ -n "$db_exists" ]; then
echo "ERROR: New database already exists $3"
/**
* 对日期进行格式化,
* @param date 要格式化的日期
* @param format 进行格式化的模式字符串
* 支持的模式字母有:
* y:年,
* M:年中的月份(1-12),
* d:月份中的天(1-31),
* h:小时(0-23),
* m:分(0-59),
@mickhan
mickhan / gist:5829436
Created June 21, 2013 07:09
nginx /etc/init.d 脚本
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
@mickhan
mickhan / gist:5829386
Created June 21, 2013 06:59
在shell脚本中修改crontab
crontab -l > cron.tmp
echo "0 0 */1 * * /path/to/script.sh" >> cron.tmp
crontab cron.tmp
rm -f cron.tmp
@mickhan
mickhan / gist:5720727
Created June 6, 2013 10:52
tornado下载文件
filename = "%s_attack_report.xls" % file_to_download
f = open(filename, "r")
self.set_header('Content-Type', 'text/csv')
self.set_header('Content-Disposition', 'attachment; filename=' + filename + '')
self.write(f.read())
@mickhan
mickhan / gist:5711515
Created June 5, 2013 03:59
记录文件日志,每天midnight创建新文件
import config
import logging
from logging.handlers import TimedRotatingFileHandler
logger = logging.getLogger(config.LOG_NAME)
logfile = '%s/%s' % (config.LOG_BASE_PATH, config.LOG_FILENAME)
fileTimeHandler = TimedRotatingFileHandler(logfile, "midnight", 1)
fileTimeHandler.suffix = "%Y%m%d.log" # 设置切分后日志文件名格式
@mickhan
mickhan / gist:5683459
Last active December 17, 2015 22:39
jquery ajax
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: "/ajax_url",
type: "POST",
data: {
"dataname": data
},
dataType: "json",
success:function(data_info){
@mickhan
mickhan / gist:5676751
Created May 30, 2013 09:26
python pidfile
import os
import sys
pid = str(os.getpid())
pidfile = "/tmp/pidfilename.pid"
if os.path.isfile(pidfile):
print "%s already exists, exiting" % pidfile
sys.exit()
else:
def txt2html(txt):
'''将txt以行为单位加上<li></li>标签'''
def escape(txt):
'''将txt文本中的空格、&、<、>、(")、(')转化成对应的的字符实体,以方便在html上显示'''
txt = txt.replace('&','&#38;')
txt = txt.replace(' ','&#160;')
txt = txt.replace('<','&#60;')
txt = txt.replace('>','&#62;')
txt = txt.replace('"','&#34;')
txt = txt.replace('\'','&#39;')
@mickhan
mickhan / redis-ve
Created January 31, 2013 04:01 — forked from pterk/redis-ve
#!/bin/bash
VERSION="2.6.2"
ARCHIVE=redis-${VERSION}.tar.gz
if [ -z "${VIRTUAL_ENV}" ]; then
echo "Please activate a virtualenv first";
exit
fi