zoukankan      html  css  js  c++  java
  • 写一些脚本的心得总结系列第1篇 ---- 创建分表

    前段时间一直在写一些跑数据的脚本,今天刚好有时间总结一下。

    一般来说,分成2种,一种是写在单个文件中的任务,数据库驱动之类的直接include进来。运行的时候:
    cd /path/to/project
    /usr/local/php5.x/php task.php
    另外一种是套在框架内部的某个contorller文件里面,调用方式可以参考CI的方式。比如:
    cd /path/to/project
    /usr/local/php5.x/php index.php tempscript methodName param1 param2

    我写过的脚本任务大概又分以下几种:
    1.每日创建分表的(所谓分表分库的)
    2.历史数据迁移到分表的。(以前单表几十G的表,需要做优化分表)
    3.同步数据到其他表的。
    4.从一种DB迁移数据到另外一种DB的。(比如从mssql->mysql,或反之)
    5.从数据库同步到redis的。
    6.改读缓存文件的。

    先说第一个:每日创建分表。
    要能接收开始时间参数,和一次性生成天数。
    在创建的时候,先判断CREATE TABLE IF NOT EXISTS(假设我们用的是mysql)
    用for循环去遍历生成天分表,失败或成功给出提示输出。默认最好是一次性生成2天。有时间交叉,即使今天生成明日的分表失败,第二天执行的时候还会重复判断和创建。
    写一个shell以完成每日定时任务如下:

    #!/bin/bash

    cd /usr/local/nginx/html/project_path/
    /usr/local/php-5.4.25/bin/php index.php backstage task createLogTableByDate

    #curl "http://192.168.1.234:5555/backstage/task/createLogTableByDate"

    由于项目中有多个任务要执行,所以就不一一去用crontab -e来设置了,直接在/etc/crontab里面写如下的:
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    HOME=/

    # For details see man 4 crontabs

    # Example of job definition:
    # .---------------- minute (0 - 59)
    # | .------------- hour (0 - 23)
    # | | .---------- day of month (1 - 3o)
    # | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
    # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    # | | | | |
    # * * * * * user-name command to be executed

    #每隔5分钟执行一次脚本
    #*/5 * * * * root /root/shell/sycSomethingToRedis.sh
    #*/5 * * * * root /root/shell/syncUsers.sh

    #某平台
    */1 * * * * root /root/shell/SomeProjectClient.sh
    */1 * * * * root /root/shell/SomeProjectScript.sh
    00 00 * * * root /root/shell/SomeProjectScriptCreateTable.sh

    #每隔2两小时执行一次脚本
    #0 */2 * * * root /root/shell/statisData.sh

    #每天23点执行一次脚本
    #* 23 * * * root /root/shell/createAdDetailTable.sh
    #20 15 * * * root /root/shell/sinaK.sh

    #每天00:10执行一次创建某日志表的脚本
    02 00 * * * root /root/shell/createSomeLogsTable.sh

    11 00 * * * root /root/shell/statis.sh

  • 相关阅读:
    Bessie Goes Moo
    小P的强力值
    Max Flow
    约瑟夫
    Moocryption
    杂务
    排行榜
    银行贷款
    轰炸
    Contaminated Milk
  • 原文地址:https://www.cnblogs.com/freephp/p/5093473.html
Copyright © 2011-2022 走看看