awk脚本备份drupal

根据settings.php 里面的相关设置,去备份mysql.保留14天的。

#!/bin/bash

awk '
BEGIN{FS="[\" \047]+"
    www_prefix="/mnt/"
    bak_prefix="/mnt/backup/"
    path["test1"]="test1/sites/default/files"
    path["test2"]="test2/sites/default/files"
    bak=0    #备份文件方式 0 rsync 1 tar
    del=14   #删除多少以前的
}
!/^ *\/\// && /\/\*/{a=1}
!/^ *\/\// && /\*\/ *$/{a=2}
a==2 && /^ *\$databases/{
    b[1]=1
    next
    }
b[1] && a==2 && /^ *[^#]/ && !/^ *\/\// && /database|username|password|host/{
    b[$2]=$4
    if(length(b)==5){
        site=gensub(www_prefix"([^/]*)/.*","\\1",1,FILENAME)
        mysql = sprintf("mysqldump -u\"%s\" -p\"%s\" -h\"%s\" --database %s > %s%s/db/%s.sql",b["username"],b["password"],b["host"],b["database"],bak_prefix,site,strftime("%F",systime()))
        system(mysql)
        del_bak_mysql = sprintf("rm -f %s%s/db/%s.sql",bak_prefix,site,strftime("%F",systime()-86400*del))
        system(del_bak_mysql)
        if(bak){
            file_bak = sprintf("cd %s%s && tar -cf %s%s/file/%s.tar.gz .",www_prefix,site,bak_prefix,site,strftime("%F",systime()))
        }else{
            file_bak = sprintf("rsync -az %s%s/ %s%s/file",www_prefix,path[site],bak_prefix,site)
        }
        system(file_bak)
        delete b
    }
}' /mnt/test1/sites/default/settings.php /mnt/test2/sites/default/settings.php


下次还有新的drupal直接把settings.php文件添加到后面就和path(网站root路径)就可以了,


附:Ubuntu安装Drupal

你可能感兴趣的:(backup,awk,drupal)