Linux运维脚本

1.备份redis

#! /bin/bash

# Others vars
BIN_DIR="/usr/local/bin"
SRC_DIR="/home/okcomic/tools/redis-3.2.5"
BCK_DIR="/home/project/backup/redis"
# DATE=`date +%F-%T`
DATE=`date +%F`
APP_NAME='project_'

# TODO
$BIN_DIR/redis-cli -h 10.26.92.51 SAVE
cp $SRC_DIR/dump.rdb $BCK_DIR/$APP_NAME$DATE.rdb

2.备份nginx日志

#! /bin/bash

# Others vars
BIN_DIR="/usr/local/bin"
SRC_DIR="/usr/local/openresty/nginx/logs/"
DST_DIR="/home/okcomic/backup/nginx/logs"
# DATE=`date +%F-%T`
DATE=`date +%F`
BACK_NAME='logs_'$DATE.tar.gz

# TODO
cd $DST_DIR
#压缩备份文件
tar -zcPf $BACK_NAME $SRC_DIR
#清空源文件
for i in `find $SRC_DIR -name "*.log"`; do echo >$i; done

3.数据库备份

#!/bin/sh
# File: /home/project/script/mysql_backup.sh
# Database info
DB_NAME="mydb"
DB_USER="root"
DB_PASS="********"

# Others vars
BIN_DIR="/usr/bin"
BCK_DIR="/home/project/backup/mysql"
DATE=`date +%F`

# TODO
$BIN_DIR/mysqldump --opt -u$DB_USER -p$DB_PASS $DB_NAME | gzip > $BCK_DIR/${DB_NAME}_$DATE.gz

4.删除30天以前的文件

find /home/project/backup/redis -mtime +30 -name "*.rdb" -exec rm -rf {} \;

5.定时任务

#每天23:59备份redis
59 23 * * * /home/project/script/redis_backup.sh >/dev/null 2>&1
#每月4日2点删除redis备份
* 2 4 * * /home/project/script/auto_del_backup.sh >/dev/null 2>&1
#每月2日2点删除nignx日志备份
* 2 2 * * /home/project/script/nginx_log_backup.sh >/dev/null 2>&1