forked from jsanchezL/SugarDockerized
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
executable file
·92 lines (79 loc) · 3.31 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
#!/bin/bash
# Enrico Simonetti
# enricosimonetti.com
if [ -z $1 ]
then
echo Provide the backup suffix as script parameters
else
# check if the stack is running
running=`docker ps | grep sugar-mysql | wc -l`
# check if rsync is installed
if [ `command -v rsync | grep rsync | wc -l` -eq 0 ]
then
echo Please install \"rsync\" before running the backup command
exit 1
fi
if [ $running -gt 0 ]
then
# running
# enter the repo's root directory
REPO="$( dirname ${BASH_SOURCE[0]} )/../"
cd $REPO
BACKUP_DIR="backups/backup_$1"
echo Backing up sugar to \"$BACKUP_DIR\"
# if it is our repo, and the source exists, and the destination does not
if [ -f '.gitignore' ] && [ -d 'data' ] && [ ! -d $BACKUP_DIR ] && [ -d 'data/app/sugar' ]
then
# checking db name if it is indeed sugar
DB_NAME=`cat data/app/sugar/config.php | grep db_name | awk '{print $3}' | sed 's/[^[:alnum:]]//g'`
if [ $DB_NAME == 'sugar' ]
then
mkdir -p $BACKUP_DIR
sudo rsync -a data/app/sugar $BACKUP_DIR/
if [ -d $BACKUP_DIR/sugar ]
then
echo Application files backed up on $BACKUP_DIR/sugar
else
echo Application files NOT backed up!!!
echo Please discard the current backup
fi
#mysqldump -h docker.local -u root -proot --order-by-primary --single-transaction -Q --opt --skip-extended-insert sugar > $BACKUP_DIR/sugar.sql
# running mysqldump on the mysql container instead
docker exec -it sugar-mysql mysqldump -h localhost -u root -proot --order-by-primary --single-transaction -Q --opt --skip-extended-insert sugar | grep -v "mysqldump: \[Warning\]" > $BACKUP_DIR/sugar.sql
if [ \( -f $BACKUP_DIR/sugar.sql \) -a \( "$?" -eq 0 \) ]
then
echo Database backed up on $BACKUP_DIR/sugar.sql
if hash tar 2>/dev/null; then
tar -zcvf $BACKUP_DIR/sugar.sql.tgz $BACKUP_DIR/sugar.sql
fi
if [ -f $BACKUP_DIR/sugar.sql.tgz ]
then
echo Database compressed on $BACKUP_DIR/sugar.sql.tgz
rm $BACKUP_DIR/sugar.sql
fi
else
echo Database NOT backed up!!! Please check that the \"sugar\" database exists!
echo Please discard the current backup
fi
else
echo For the backup and restore process to work, the database name should be \"sugar\"
echo Backup aborted
fi
else
if [ ! -d 'data' ]
then
echo \"data\" cannot be empty, the command needs to be executed from within the clone of the repository
fi
if [ ! -d 'data/app/sugar' ]
then
echo \"data/app/sugar\" cannot be empty
fi
if [ -d $BACKUP_DIR ]
then
echo $BACKUP_DIR exists already
fi
fi
else
echo The stack is not running, please start the stack first
fi
fi