-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup
executable file
·109 lines (80 loc) · 2.87 KB
/
backup
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
#!/bin/bash
# BEGIN CONFIGURATION ==========================================================
BACKUP_DIR="/root/backups" # The directory in which you want backups placed
DROPBOX_UPLOADER="./dropbox_uploader.sh"
DROPBOX_UPLOADER_CFG="./.dropbox_uploader"
MYSQL_HOST="localhost"
MYSQL_USER="root"
MYSQL_PASS="root"
MYSQL_BACKUP_DIR="$BACKUP_DIR/db/"
SITES_DIR="/usr/share/nginx/www"
SITES_BACKUP_DIR="$BACKUP_DIR/site/"
# You probably won't have to change these
THE_DATE="$(date '+%Y-%m-%d')"
MYSQL_PATH="$(which mysql)"
MYSQLDUMP_PATH="$(which mysqldump)"
TAR_PATH="$(which tar)"
# Check if Dropbox Uploader exists
if [ ! -f $DROPBOX_UPLOADER ]; then
echo -e ""
echo -e "\e[31mError! Dropbox uploader not exists! Download it from:\e[0m"
echo -e "https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh"
echo -e ""
exit
fi
if [ ! -f $DROPBOX_UPLOADER_CFG ]; then
echo -e ""
echo -e "\e[31mError! Run Dropbox Uploader configuration first. Run this command:\e[0m"
echo -e "./dropbox_uploder.sh"
echo -e ""
exit
fi
# Remove backup folders
rm -rf $BACKUP_DIR/*
rm -rf $SITES_DIR/*.gz
# Re-creating backup folders
mkdir -p "$SITES_BACKUP_DIR"
mkdir -p "$MYSQL_BACKUP_DIR"
echo ""
echo ""
# Get a list of mysql databases and dump them one by one
DBS="$($MYSQL_PATH -h $MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS -Bse 'show databases')"
echo -e "--------------------------------------------"
echo -e " DATABASE "
echo -e "--------------------------------------------"
for db in $DBS
do
if [[ $db != "information_schema" && $db != "mysql" && $db != "performance_schema" ]]
then
echo -e "Database: \e[92m$db\e[0m"
$MYSQLDUMP_PATH --opt --skip-add-locks -h $MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS $db | gzip > $MYSQL_BACKUP_DIR$db\_$THE_DATE.sql.gz
echo -e "Dropbox: \e[94m$db\e[0m"
$DROPBOX_UPLOADER upload $MYSQL_BACKUP_DIR$db\_$THE_DATE.sql.gz $THE_DATE/$db\_$THE_DATE.sql.gz
echo ""
fi
done
echo -e "--------------------------------------------"
echo -e " WEBSITE "
echo -e "--------------------------------------------"
cd "$SITES_DIR"
for d in *
do
# Make sure it's a folder, not a file
if [ -d "$d" ]; then
echo -e "Compress: \e[33m$d\e[0m"
# Copy the whole folder to backup
cp -R "$SITES_DIR/$d" "$BACKUP_DIR/$d"
cd $BACKUP_DIR/$d
$TAR_PATH -czf $SITES_BACKUP_DIR/$d\_$THE_DATE.tgz ./ -C /
echo -e "Dropbox: \e[94m"$d\_$THE_DATE.tgz"\e[0m"
$DROPBOX_UPLOADER upload $SITES_BACKUP_DIR/$d\_$THE_DATE.tgz $THE_DATE/$d\_$THE_DATE.tgz
rm -rf $BACKUP_DIR/$d\_$THE_DATE.tgz
rm -rf $BACKUP_DIR/$d
cd $SITES_DIR
echo -e "\e[96mDone!\e[0m"
echo ""
echo ""
fi
done
cd "$BACKUP_DIR"
echo "All done!"