-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_www.sh
executable file
·108 lines (92 loc) · 2.37 KB
/
backup_www.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
#!/bin/bash
# author: Georg Wallisch
# contact: [email protected]
# copyright: Copyright © 2018 by Georg Wallisch
# credits: Georg Wallisch
# date: 2018/07/31
# deprecated: False
# email: [email protected]
# license: GPL
# maintainer: Georg Wallisch
# status: beta
SRC_PATH=/var/www
BACKUP_SUFFIX=tar.gz
BACKUPS=5
USAGE="USAGE: sudo $0 /path/to/backup"
echo -e "\n**************************"
echo -e "* WWW Backup Script v1.3 *"
echo -e "**************************\n"
echo -e "Starting backup on $(date)\n"
if [ $(id -u) -ne 0 ]; then
echo -e "WWW Backup script must be run as root."
echo -e "${USAGE}\n"
exit 1
fi
if [ "$1" == '' ]; then
echo -e "Backup path not specified."
echo -e "${USAGE}\n"
exit 1
fi
BACKUP_PATH=$1
shift
echo -e "Backup path is set to ${BACKUP_PATH}"
if [ ! -d "$BACKUP_PATH" ]; then
echo "${BACKUP_PATH} does not exist, creating.."
mkdir -p $BACKUP_PATH
if [ ! -d "$BACKUP_PATH" ]; then
echo "Could not create ${BACKUP_PATH}!"
exit 1
fi
fi
if [ -d "$1" ]; then
SRC_PATH=$1
shift
echo "Source path is set to ${SRC_PATH}"
else
echo "Source path is ${SRC_PATH}"
fi
BACKUP_PREFIX=$(readlink -f $SRC_PATH)
BACKUP_PREFIX=${BACKUP_PREFIX:1}
BACKUP_PREFIX=${BACKUP_PREFIX////_}
echo -e "Using Prefix ${BACKUP_PREFIX}"
echo -e "Packing and compressing ${SRC_PATH} into ${BACKUP_PATH}/${FILE}"
FILE="${BACKUP_PREFIX}.0.${BACKUP_SUFFIX}"
tar -czf $BACKUP_PATH/$FILE $SRC_PATH
if [ $? -ne 0 ]; then
echo -e "Error packing ${SRC_PATH}!"
exit 1
else
counter=$BACKUPS
PREVFILE=$BACKUP_PREFIX.$counter.$BACKUP_SUFFIX
if [ -f $BACKUP_PATH/$PREVFILE ]; then
if [ -w $BACKUP_PATH/$PREVFILE ]; then
echo -e "Removing oldest file $PREVFILE"
rm -f $BACKUP_PATH/$PREVFILE
if [ $? -ne 0 ]; then
echo -e "Error while removing oldest file $PREVFILE!"
fi
else
echo -e "Cannot remove oldest file $PREVFILE! No write permission!"
fi
fi
((counter--))
until [ $counter -lt 0 ]
do
FILE=$BACKUP_PREFIX.$counter.$BACKUP_SUFFIX
if [ -f $BACKUP_PATH/$FILE ]; then
if [ -w $BACKUP_PATH/$FILE ]; then
echo -e "Moving $FILE to $PREVFILE"
mv -n $BACKUP_PATH/$FILE $BACKUP_PATH/$PREVFILE
if [ $? -ne 0 ]; then
echo -e "Error while moving $FILE to $PREVFILE!"
fi
else
echo -e "Cannot move $FILE to $PREVFILE! No write permission!"
fi
fi
PREVFILE=$FILE
((counter--))
done
fi
echo -e "Finished backup by $(date)!\n"
exit 0