forked from Stuffy/backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
151 lines (128 loc) · 3.55 KB
/
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
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
#!/bin/bash
#########################
######TO BE MODIFIED#####
# What folders should be backup'd?
declare -a what_to_backup=('path1' 'path2');
# Path where the backup and the backup folders should be stored
BACKUP_PATH="/root/backup/"
# Name of the folders to store the backups in
SQL_FOLDERNAME="db"
DATA_FOLDERNAME=""
BACKUP_MYSQL="1"
# MySql login information
MYSQL_USER=""
MYSQL_PASSWORD=""
### FTP server Setup ###
SEND_FTP="1"
FTPD="FTP FOLDER"
FTPU="FTP USERNAME"
FTPP="FTP PASSWORD"
FTPS="FTP HOST"
######DO NOT MAKE MODIFICATION BELOW#####
#########################################
### Binaries ###
TAR="$(which tar)"
GZIP="$(which gzip)"
SFTP="$(which sftp)"
SCP="$(which scp)"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
# File backup name segments
HOSTNAME=$(hostname -s)
NOW=$(date +"%Y-%m-%d")
# TODO: implement verbosity
# override & verbosity
for flag in "$@"
do
if [ "$flag" == "-o" ]; then
FLAG_OVERWRITE="1"
fi
if [ "$flag" == "-v" ]; then
FLAG_VERBOSE="1"
fi
done
# Check freediskspace
FREE_DISCSPACE=`df / -h | awk '{ print $4 }' | tail -n 1 | cut -d "G" -f1`
# If discspace is lower than 1 GB, abort
if [[ $FREE_DISCSPACE -lt 1 ]]; then
echo "Discspace is low! ( Less than 15 GB free. ) Backup aborted."
exit
fi
# Create directory structure
if [ ! -d "$BACKUP_PATH/$NOW" ]; then
mkdir $BACKUP_PATH/$NOW > /dev/null
if [ $? -gt 0 ]; then
echo "Error while creating folder $BACKUP_PATH/$NOW. Aborting."
exit
fi
fi
if [ ! -d "$BACKUP_PATH/$NOW/$SQL_FOLDERNAME" ]; then
mkdir $BACKUP_PATH/$NOW/$SQL_FOLDERNAME > /dev/null
if [ $? -gt 0 ]; then
echo "Error while creating folder $BACKUP_PATH/$NOW/$SQL_FOLDERNAME. Aborting."
exit
fi
fi
if [ ! -d "$BACKUP_PATH/$NOW/$DATA_FOLDERNAME" ]; then
mkdir $BACKUP_PATH/$NOW/$DATA_FOLDERNAME
if [ $? -gt 0 ]; then
echo "Error while creating folder $BACKUP_PATH/$NOW/$DATA_FOLDERNAME. Aborting."
exit
fi
fi
# Backup mysql
cd $BACKUP_PATH/$NOW/$SQL_FOLDERNAME/
if [ "$BACKUP_MYSQL" == "1" ]; then
if [ ! -f ./database_backup.$NOW.tgz ] || [ "$FLAG_OVERWRITE" == "1" ]; then
### Get all databases name ###
DBS="$($MYSQL -u $MYSQL_USER -h $MYSQL_HOST -p$MYSQL_PASSWORD -Bse 'show databases')"
for db in $DBS
do
### Backup dbs in individual files ###
FILE=$BACKUP_PATH/$NOW/$SQL_FOLDERNAME/$db-$NOW.sql.gz
echo $db; $MYSQLDUMP --opt --add-drop-table --allow-keywords -q -c -u $MYSQL_USER -h $MYSQL_HOST -p$MYSQL_PASSWORD $db | $GZIP -9 > $FILE
done
else
echo "Database backup for date $NOW already exists, skipping."
fi
fi
# Backup files
cd $BACKUP_PATH/$NOW/$DATA_FOLDERNAME/
DIRECTORY=$NOW
cd $BACKUP_PATH/$NOW/
for i in "${what_to_backup[@]}"
do
SPLIT=$(echo $i | tr "/" "\n")
for x in $SPLIT
do
LAST=$x
done
if [ -d "$i" ]; then
if [ ! -f ./$LAST.$NOW.tgz ] || [ "$FLAG_OVERWRITE" == "1" ]; then
echo "Copying files from $i for packing..."
cp -r $i ./
echo "Done. Packing files..."
tar -zcvf $LAST.$NOW.tgz ./$LAST >> $NOW.content.log
echo "Done. Removing unpacked files..."
rm -rf ./$LAST
echo "Done. Folder backupped in $LAST.$NOW.tgz..."
else
echo "File $LAST.$NOW.tgz exists, skipping."
fi
else
echo "Backup location $i wasn't found. Skipping."
fi
done
### Compress all backups in one nice file to upload ###
ARCHIVE=$BACKUP_PATH/$HOSTNAME-$NOW.tar.gz
ARCHIVED=$NOW
cd $BACKUP_PATH/
$TAR -cvf $ARCHIVE $ARCHIVED
if [ "$SEND_FTP" == "1" ]; then
### Dump backups using FTP ###
cd $BACKUP_PATH
DUMPFILE=$HOSTNAME-$NOW.tar.gz
$SCP $DUMPFILE $FTPU@$FTPS:$FTPD/
fi
### Delete the backup dir and keep archive ###
rm -rf $ARCHIVED