-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_backuplogs.sh
More file actions
76 lines (61 loc) · 1.64 KB
/
15_backuplogs.sh
File metadata and controls
76 lines (61 loc) · 1.64 KB
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
#!/bin/bash
R="\e[31m"
G="\e[32m"
Y="\e[33m"
N="\e[0m"
SOURCE_DIR=$1
DEST_DIR=$2
DAYS=${3:-14} # If user is not providing number of days, we are taking 14 as default
LOGS_FOLDER="/home/ec2-user/shell_script.logs"
LOG_FILE=$(echo $0 | awk -F "/" '{print $NF}' | cut -d "." -f1)
TIMESTAMP=$(date +%Y-%m-%d-%H-%M-%S)
LOG_FILE_NAME="$LOGS_FOLDER/$LOG_FILE-$TIMESTAMP.log"
VALIDATE() {
if [ $1 -ne 0 ]
then
echo -e "$2 ....$R Failure $N"
exit 1
else
echo -e "$2 ....$G SUCCESS $N"
fi
}
USAGE(){
echo -e "$R USAGE:: $N sh 15_backuplogs.sh <SOURCE_DIR> <DIST_DIR> <DAYS(Optional)>"
exit 1
}
mkdir -p /home/ec2-user/shellscript-logs/
if [ $# -lt 2 ]
then
USAGE
fi
if [ ! -d $SOURCE_DIR ]
then
echo -e "$SOURCE_DIR Does not exists ... Please check"
exit 1
else if [ ! -d $DEST_DIR ]
then
echo -e "$DEST_DIR Does not exists ... Please check"
exit 1
fi
echo "Script started executing at: $TIMESTAMP" &>>$LOG_FILE_NAME
FILES=$(find $SOURCE_DIR -name ".log" +mtime +$DAYS)
echo "Files are : $FILES"
if [ -n $FILES ]
then
echo "Files are: $FILES"
ZIP_FILE="$DEST_DIR/app-logs-$TIMESTAMP.zip"
find $SOURCE_DIR -name ".log" -mtime +$DAYS | zip -@ " $ZIP_FILE "
if [ -f "$ZIP_FILE" ]
then
echo -e "Successfully created zip file for files older than $DAYS"
while read -r filepath
do
echo "Deleting file: $filepath" &>>$LOG_FILE_NAME
rm -rf $filepath
echo "Deleted file: $filepath"
done <<< $FILES
else
echo -e "$R ERROR :: $N Failed to create ZIP file "
else
echo "No files are found older than $DAYS "
fi