-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathnginx_log_backup.sh
114 lines (84 loc) · 2.68 KB
/
nginx_log_backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
# Backup Log Files for linux applications such as nginx, php, mysql and so on.
# Crontab Usage: 00 01 * * * /mydata/backups/bak_list/nginx_log_backup.sh >/dev/null 2>&1
# (c) 2015 vfhky https://typecodes.com/linux/applogsbackup.html
# https://github.com/vfhky/mylnmp/blob/master/nginx_log_backup.sh
# Basic command.
TARCMD="tar -zcf"
MVCMD="\mv -f"
FINDCMD="find"
# Number of days to keep
NUMDAYS=40
# Path of the logs you want backup.You can set it as the nginx log path or the mysql log path and so on.
Source_Log_Dir=/var/log/nginx
# Set the file types you want backup accoding to the suffix of log files.
Source_Log_Files=`ls ${Source_Log_Dir}/*.log`
# Set the backup path.
Backup_Dirs=${Source_Log_Dir}
# Previous date format: e.g 20150505
Previous_Date=`date -d "-1 days" +%Y%m%d`
# The target backup dir.
Backup_Dir=${Backup_Dirs}/${Previous_Date}
# Path of the log generated by this shell script automatically.
Shell_Log=${Backup_Dir}/process.log
# Run command functions.
function ERROR() {
echo >/dev/null && echo "[`date +%H:%M:%S:%N`][error] $@" >> ${Shell_Log}
exit 1
}
function NOTICE() {
echo >/dev/null && echo "[`date +%H:%M:%S:%N`][notice] $@" >> ${Shell_Log}
}
function RUNCMD() {
echo "[`date +%H:%M:%S:%N`][notice] $@" >> ${Shell_Log}
eval $@
}
function REMOVE_DIR_BY_DATE()
{
for subdir in `ls $@`;
do
if [ "${subdir}" \< "${dt}" ];
then
rm -rf $RemoveDir/$subdir >/dev/null
echo "The directory $RemoveDir/$subdir has been removed."
fi
done
}
# Check the days user input.You can delete the codes below generally.
if [[ ! $NUMDAYS =~ ^[0-9]+$ ]]; then
ERROR "Invalid number of days[$NUMDAYS]!"
elif [ "$NUMDAYS" -eq "0" ]; then
ERROR "Number of days must be greater than zero!"
fi
# Lock down permissions.
umask 077
# Create the backup log dir and a shell log.
mkdir -p $Backup_Dir
touch $Shell_Log
NOTICE "[1]Start backup."
NOTICE "[2]Start compress the log files using the tar command."
RUNCMD "cd ${Source_Log_Dir} && ${TARCMD} ${Previous_Date}.tar.gz *.log"
RC=$?
if [ $RC -gt 0 ]; then
ERROR "Creat the backup package failed!"
fi
NOTICE "[3]Start move the compress file to backup dir."
RUNCMD "${MVCMD} ${Previous_Date}.tar.gz ${Backup_Dir}/"
RC=$?
if [ $RC -gt 0 ]; then
ERROR "Move the compress file failed!"
fi
NOTICE "[4]Start empty every log file."
for file in ${Source_Log_Files}
do
RUNCMD ">$file"
RC=$?
if [ $RC -gt 0 ]; then
ERROR "Empty every log file failed!"
fi
done
NOTICE "[5]Removing older folders than $NUMDAYS days."
# ls ${Source_Log_Dir} | grep '^[0-9]\{8\}$' | xargs rm -rf
RUNCMD "$FINDCMD ${Source_Log_Dir} -type d -name [0-9]*\[0-9] -mtime +$NUMDAYS | xargs rm -rf"
NOTICE "[6]End backup."
# exit 0