-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-nextcloud.sh
203 lines (190 loc) · 7.59 KB
/
backup-nextcloud.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/bash
# Made By The Depart3d
# clear terminal
clear
# Show Welcome
VERSION='1.1.5'
echo "============================================="
echo "| Nextcloud Backup |"
echo "| Script |"
echo "| v${VERSION} |"
echo "============================================="
echo ""
# Script Colors
RED='\033[1;31m'
GREEN='\033[1;32m'
ORANGE='\033[1;33m'
NC='\033[0m' # No Color
NC_DIRECTORY=$1
NC_BACKUP_FOLDER=$2
PKG_REQUIREMENTS=("mysqldump" "zip" "rsync" "pv" "gzip")
# Abilit to add color to certain outputs
echoc () {
echo -e $1;
}
# check os before running script
case "$OSTYPE" in
darwin*)
echo -e "${RED}MacOS is not supported by default${NC}"
exit
;;
msys*)
echo -e "${RED}Windows is not supported by default${NC}"
exit
;;
cygwin*)
echo -e "${RED}Windows is not supported by default${NC}"
exit
;;
esac
# Must be root to run this script
if (( $EUID != 0 )); then
echoc ${RED}"Please run as root "${NC}
exit
fi
# Check what webserver is running Apache2/Nginx
# get the user thats running the webserver
if cat /etc/apache2/apache2.conf &> /dev/null; then
WEB_SERVER_USER=$(ps aux | grep -v root | grep -i apache | cut -d\ -f1 | sort | uniq)
elif cat /etc/nginx/nginx.conf &> /dev/null; then
WEB_SERVER_USER=$(ps aux | grep -v root | grep -i nginx | cut -d\ -f1 | sort | uniq)
fi
# Check where php is installed if not in the default location
if command -v /usr/bin/php &> /dev/null; then
PHP_LOCATION="/usr/bin/php"
else
PHP_LOCATION=$(which php)
fi
# Check if required pkgs are installed for script
echoc ${ORANGE}'Status'${NC}": Checking if required packages are installed"
for required_pkg in "${PKG_REQUIREMENTS[@]}"
do
if ! command -v $required_pkg &> /dev/null; then
echoc ${GREEN}'Status'${NC}": Installing $required_pkg"
apt-get install $required_pkg -y &> /dev/null
fi
done
echoc ${GREEN}'Status'${NC}": Required packages installed"
# Check if Backup Directory set
if test -z "$NC_BACKUP_FOLDER"; then
read -p 'Backup Folder Location: ' NC_BACKUP_FOLDER;
fi
echo -e ${GREEN}'Status'${NC}": Entered $NC_BACKUP_FOLDER"
if [ -d $NC_BACKUP_FOLDER/$(date +'%Y-%m-%d')/ ]; then
cd $NC_BACKUP_FOLDER/$(date +'%Y-%m-%d')/
echo -e ${GREEN}'Status'${NC}": Entered $NC_BACKUP_FOLDER/"$(date +'%Y-%m-%d')
else
mkdir -p $NC_BACKUP_FOLDER/$(date +'%Y-%m-%d')/$(date +'%HT%M')
cd $NC_BACKUP_FOLDER/$(date +'%Y-%m-%d')/$(date +'%HT%M')
fi
create_folder () {
cd $NC_BACKUP_FOLDER/$(date +'%Y-%m-%d')/$(date +'%HT%M')
echoc ${GREEN}'Status'${NC}": Entered $NC_BACKUP_FOLDER/"$(date +'%F')/$(date +'%HT%M')
echoc ${ORANGE}'Status'${NC}
if test -z "$NC_DIRECTORY"; then
read -p 'Enter valid nextcloud directory: ' NC_DIRECTORY;
fi
if [ -d $NC_DIRECTORY ]; then
# Enable Maintenance Mode
cd $NC_DIRECTORY
CHECK_IF_NC_FOLDER=$(php -r 'error_reporting(0);require("version.php"); echo $OC_VersionString;')
if test -z "$CHECK_IF_NC_FOLDER"; then
echoc ${RED}"Unable to detect valid nextcloud install"${NC}
exit
fi
cd $NC_BACKUP_FOLDER/$(date +'%Y-%m-%d')/$(date +'%HT%M')
sudo -u $WEB_SERVER_USER $PHP_LOCATION $NC_DIRECTORY/occ maintenance:mode --on
echoc ${GREEN}'Status'${NC}': '$NC_DIRECTORY' valid directory'
echoc ${GREEN}'Status'${NC}': Creating zip'
zip -X- -r wwwDirBackup-$(date +'%Y-%m-%d').zip $NC_DIRECTORY 2>&1 | pv -lep -s $(ls -Rl1 $NC_DIRECTORY | egrep -c '^[-/]') > /dev/null
echoc ${GREEN}'Status'${NC}': Zip file created'
echo ""
echo "============================================="
echo -e ${GREEN}
echo " Status: Directory Backup Complete"
echo -e ${NC}
echo "============================================="
echo ""
else
echoc ${RED}'Status'${NC}': Input is not a valid folder'
rm -r ../$(date +'%HT%M')
exit;
fi
echoc ${GREEN}'Status'${NC}': Dumping SQL'
NC_CONFIG_FILE="$NC_DIRECTORY/config/config.php"
if [ ! -e "$NC_CONFIG_FILE" ]; then
read -p "Enter Database HOST: " DBHOST
read -p "Enter Database Port: " DBPORT
read -p "Enter Database User: " DBUSER
read -p "Enter Database Name: " DBNAME
mysqldump -h $DBHOST -P $DBPORT -p -u $DBUSER --databases $DBNAME > $DBNAME.sql 2> database_backup.log
if [ "$?" -eq 0 ]; then
gzip $DBNAME.sql
echo ""
echo "============================================="
echo -e ${GREEN}
echo " Status: Backup Complete"
echo -e ${NC}
echo "============================================="
echo ""
rm -rf database_backup.log
# Disable Maintenance Mode
sudo -u $WEB_SERVER_USER $PHP_LOCATION $NC_DIRECTORY/occ maintenance:mode --off
else
echo ""
echo "============================================="
echo -e ${RED}
echo " Status: Database Backup Failed"
echo -e ${NC}
echo "============================================="
echo ""
echo -e "mysqldump encountered a problem look in ${RED}$NC_WORKING_FOLDER/database_backup.log${NC} for more information."
# Disable Maintenance Mode
sudo -u $WEB_SERVER_USER $PHP_LOCATION $NC_DIRECTORY/occ maintenance:mode --off
fi
else
DBNAME=$(grep -Po "(?<='dbname' => ').*(?=',)" $NC_CONFIG_FILE)
DBHOST=$(grep -Po "(?<='dbhost' => ').*(?=',)" $NC_CONFIG_FILE)
DBPORT=$(grep -Po "(?<='dbport' => ').*(?=',)" $NC_CONFIG_FILE)
DBPASSWD=$(grep -Po "(?<='dbpassword' => ').*(?=',)" $NC_CONFIG_FILE)
DBUSER=$(grep -Po "(?<='dbuser' => ').*(?=',)" $NC_CONFIG_FILE)
if test -z "$DBPORT"; then
DBPORT='3306'
fi
mysqldump -h $DBHOST -P $DBPORT --password="$DBPASSWD" -u $DBUSER --databases $DBNAME > $DBNAME.sql 2> database_backup.log
if [ "$?" -eq 0 ]; then
gzip $DBNAME.sql
echo ""
echo "============================================="
echo -e ${GREEN}
echo " Status: Backup Complete"
echo -e ${NC}
echo "============================================="
echo ""
echoc -e ${GREEN} "Backup Folder"${NC}": $NC_WORKING_FOLDER"
rm -rf database_backup.log
# Disable Maintenance Mode
sudo -u $WEB_SERVER_USER $PHP_LOCATION $NC_DIRECTORY/occ maintenance:mode --off
else
echo ""
echo "============================================="
echo -e ${RED}
echo " Status: Database Backup Failed"
echo -e ${NC}
echo "============================================="
echo ""
echo -e "mysqldump encountered a problem look in ${RED}$NC_WORKING_FOLDER/database_backup.log${NC} for more information."
# Disable Maintenance Mode
sudo -u $WEB_SERVER_USER $PHP_LOCATION $NC_DIRECTORY/occ maintenance:mode --off
fi
fi
}
if [ -d $NC_BACKUP_FOLDER/$(date +'%F')/$(date +'%HT%M')/ ]; then
NC_WORKING_FOLDER="$NC_BACKUP_FOLDER/$(date +'%Y-%m-%d')/$(date +'%HT%M')"
create_folder
else
NC_WORKING_FOLDER="$NC_BACKUP_FOLDER/$(date +'%Y-%m-%d')/$(date +'%HT%M')"
echoc ${GREEN}'Status'${NC}": Creating New folder: $NC_BACKUP_FOLDER/$(date +'%F')/$(date +'%HT%M')"
mkdir $(date +'%HT%M')
create_folder
fi