删除备份脚本

这个脚本的主要功能就是读取配置文件,然后根据需求保留备份的个数(备份的文件是以时间格式命名的),删除最老的备份,以下是脚本内容:

#!/bin/bash
KEEP_NUM=2
for i in `cat /opt/sh/delete_backup.conf`
do
    CURRENT_NUM=`ls -l ${i} | grep -v total | wc -l`
    for((j=${CURRENT_NUM};j>${KEEP_NUM};j--))
    do
        FILE_NAME=`ls ${i} | sort -n | head -n 1`
        echo "删除的目录是: ${i}/${FILE_NAME}"
        cd ${i} && [ -d ${FILE_NAME} ] && rm ${FILE_NAME} -rf && sleep 1
    done
done

delete_backup.conf的内容是删除的目录

/opt/sh/testdir/a
/opt/sh/testdir/b

nodejs学习系列(一)

这儿的开发环境是windows。需要先安装好node 安装好vscode 安装好 “git bash”。下面是一个简单的示例

  • 打开git bash 创建目录,并启动vscode 打开创建好的项目
cd d:
mkdir code
cd code
mkdir study-api
code.cmd study-api
  • 以下命令是在vscode终端中运行的
npm init ## 初始化项目,填入相应的信息即可
npm install -g cnpm --registry=https://registry.npm.taobao.org  ## 安装阿里的cnpm来加速安装
cnpm.cmd install koa --save  ## 安装koa, --save的参数是表示写入到package.json中
cnpm.cmd install nodemon --save-dev  ## 安装自动重启的工具
  • 完成后的“package.json”的文件内容如下:
{
  "name": "study-api",
  "version": "1.0.0",
  "description": "学习 RESTful API",
  "main": "index.js",
  "scripts": {
    "start": "nodemon index.js"
  },
  "author": "author_name",
  "license": "ISC",
  "dependencies": {
    "koa": "^2.7.0"
  },
  "devDependencies": {
    "nodemon": "^1.19.1"
  }
}
  • 编写一个”index.js”文件,文件如下:
const Koa = require('koa');
const app = new Koa;

app.use((ctx) =>{
    ctx.body = 'hello 我的媳妇';
})

app.listen(3000);
  • 启动项目
npm start

centos7初始化脚本

#!/bin/bash
#update soft
yum -y update
yum install wget rsync bzip2 lsof telnet htop screen tree vim gcc make net-tools lrzsz psmisc hwloc gsmartcontrol chrony -y

#时间设置 
timedatectl set-local-rtc 1
timedatectl set-timezone Asia/Shanghai
systemctl start chronyd.service
systemctl enable chronyd.service

#设置最大打开文件描述符数
echo "ulimit -SHn 102400" >> /etc/rc.local
cat >> /etc/security/limits.conf <<EOF
*           soft   nofile       65535
*           hard   nofile       65535
EOF

#禁用selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0

#停止不必要的服务
systemctl disable --now rpcbind
systemctl disable --now rpcbind.socket
systemctl disable --now postfix.service
systemctl disable --now firewalld.service
systemctl disable --now firewalld
systemctl disable --now dnsmasq
systemctl disable --now NetworkManager

#set ssh
sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
systemctl  restart sshd.service

#内核参数优化
cat >> /etc/sysctl.conf <<EOF
vm.overcommit_memory = 1
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_abort_on_overflow = 0
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.ipv4.tcp_syncookies = 1
EOF
/sbin/sysctl -p

# profile 修改
echo "export HISTTIMEFORMAT=\"%F %T \"" >> /etc/profile
echo "" >> /etc/profile
echo "## 自定义别名" >> /etc/profile
echo "alias c=clear" >> /etc/profile
echo "alias vi=vim" >> /etc/profile
echo "alias dsh='du -hsx * | sort -rh | head -n 10'" >> /etc/profile
sed -i 's/HISTSIZE=1000/HISTSIZE=10000/g' /etc/profile
source /etc/profile

NAS安装迅雷进行离线下载

nas使用迅雷总的给我的使用感觉不好,如果种子不好的话,速度提不上来。个人感觉实际意义不是很大

  1. 在套件中心安装 docker 组件
  2. 打开刚刚安装好的 docker 组件,在注册表中搜索”yinheli/docker-thunder-xware”,并安装
  3. 创建一个文件夹”/docker/download/xunlei”。
  4. 在“映像”列表里选择刚刚下载好的镜像点击“启动”,并把第三步创建的文件夹挂载到”/TDdownload”下面
  5. 在端口设置这儿添加一个端口映射,我容器的端口和映射的端口都是9000
  6. 启动容器之后,点击刚刚启动那个容器的”详情”,然后再选择”日志”,在日志中找到”THE ACTIVE CODE IS”后面的激活码
  7. 登录 “http://yuancheng.xunlei.com ” ,并登录你自己的账号和密码,输入你的激活码并点击”绑定”
  8. 然后就可以愉快的使用了,使用的时候是在“http://yuancheng.xunlei.com”这个网址中添加种子等信息

结合redis和lua实现个性化域名登录

使用场景: 客户只要通过后台绑定了自己的域名,解析到我们服务器之后。就能通过绑定的域名类型访问到相应的对应资源。实现原理是:在绑定的时候程序会将绑定域名和类型写入到redis中,并永久保存,NGINX通过lua读取redis里面的数据,指定不同的root路径。

安装

  • 安装luajit
yum -y install lua lua-devel  ## 编译的时候会依赖这个
cd /opt && wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
tar zxvf LuaJIT-2.0.5.tar.gz && cd LuaJIT-2.0.5
make install PREFIX=/opt/luajit
export LUAJIT_LIB=/opt/luajit/lib   ## 编译nginx需要的变量
export LUAJIT_INC=/opt/luajit/include/luajit-2.0  ## 编译nginx需要的变量
  • 安装cjson库,用来解析json格式数据的
cd /opt && wget https://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz
tar zxvf lua-cjson-2.1.0.tar.gz && cd lua-cjson-2.1.0
##编辑Makefile修改以下几个参数
vim Makefile
PREFIX =            /opt/luajit
LUA_INCLUDE_DIR =   /opt/luajit/include/luajit-2.0
LUA_CMODULE_DIR =   /opt/luajit/lib
LUA_MODULE_DIR =    /opt/luajit/share/lua/5.1
LUA_BIN_DIR =       /opt/luajit/bin
##编译并安装
make && make install
  • 安装cmsgpack库,主要用来序列化和反序列化的
cd /opt && git clone https://github.com/antirez/lua-cmsgpack.git && cd /opt/lua-cmsgpack
mkdir build && cd build && cmake .. && make
  • nginx编译需要的模块
cd /opt && git clone https://github.com/simplresty/ngx_devel_kit.git
cd /opt && git clone https://github.com/openresty/lua-nginx-module.git
cd /opt && git clone https://github.com/openresty/lua-resty-redis.git
  • 解决一些模块和库文件依赖问题
cp /opt/lua-cmsgpack/build/cmsgpack.so /opt/luajit/lib/
cp /opt/lua-cmsgpack/build/cmsgpack.so /opt/luajit/lib/lua/5.1/
cp /opt/luajit/lib/cjson.so /opt/luajit/lib/lua/5.1/
echo "/usr/local/lib/" >> /etc/ld.so.conf.d/libc.conf
echo "/opt/luajit/lib" >> /etc/ld.so.conf.d/libc.conf
ldconfig
  • 编译nginx,复制编译好的nginx二进制文件替换原来的nginx(原来的nginx二进制文件做备份)
tar zxvf nginx-1.14.0.tar.gz && cd nginx-1.14.0
./configure --add-module=/opt/lua-nginx-module --add-module=/opt/ngx_devel_kit --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_dav_module --with-http_flv_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_stub_status_module --with-http_sub_module --with-http_random_index_module --with-http_degradation_module --with-http_secure_link_module --with-http_gzip_static_module --with-http_perl_module --with-pcre --with-debug --with-file-aio --with-mail --with-mail_ssl_module --with-stream --with-ld-opt=-Wl,-E
make -j2

下面是一个示例代码

  • 在nginx.conf中http下加入redis的支持: “lua_package_path “/opt/lua-resty-redis/lib/?.lua;;”;”
  • lua.conf的配置文件如下:
server {
    listen 80 default;
    server_name _;  ## 这儿做默认解析
    access_log  logs/lua.log;
    error_log  logs/lua.error.log;
    index index.html;

    location / {
        add_header 'Access-Control-Allow-Origin' '*';
        set $defaultroot /opt/web/default;
        access_by_lua_file '/opt/nginx/conf/vhost/lua.lua';
        root $defaultroot;
        try_files $uri $uri/ /index.html;
    }
  • lua.lua的配置文件如下:
local redis = require "resty.redis"
local cjson = require "cjson"
local cmsgpack  = require "cmsgpack" 

local red = redis:new()
red:set_timeout(1000)
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
   ngx.say("failed to connect: ", err)
   return
end
red:select(1)

local redisData = red:get("domain.key")
local getdata = cjson.decode(redisData);

for i,w in ipairs(getdata) do
     if ngx.var.host == w.domain
          then
          if w.type == "app1"
          then
               ngx.var.defaultroot = "/opt/web/app1"
          elseif w.type == "app2"
          then
               ngx.var.defaultroot = "/opt/web/app2"
          else
               ngx.var.defaultroot = "/opt/web/default"
          end
     end
end
red:close()
  • redis中取出来的json格式如下:
[
  {
    "domain":"domain1_name",
    "id":"123456",
    "type":"app1"
  },
  {
    "domain":"domain2_name",
    "id":"123457",
    "type":"app2"
  },
  {
    "domain":"domain3_name",
    "id":"123458",
    "type":"app3"
  }
]