-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbackup.sh
133 lines (91 loc) · 2.7 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
#!/bin/bash
#######################################################
# git-sitebackup
#######################################################
version="0.1"
#######################################################
# Banner
#######################################################
echo -e "\n git-sitebackup $version \n"
#######################################################
# sqldump database information
#######################################################
function dump_db {
dir=$1
dbname=$2
dbuser=$3
dbpass=$4
echo -e " .. sqldump'ing database:"
echo -e " user: $dbuser database: $dbname"
cd $dir
mysqldump --password=$dbpass --user=$dbuser --skip-extended-insert $dbname > dbcontent.sql
echo -e " done\n"
}
#######################################################
# Commit to Git Repo
#######################################################
function commit_changes {
dir=$1
echo -e " .. Committing to local git repository at $dir"
datestamp=$(date +"%Y-%m-%d")
timestamp=$(date +"%H:%M")
cd $dir
git add .
git commit -am "Backup created on $datestamp at $timestamp."
echo -e " done\n"
}
#######################################################
# Push out to Master
#######################################################
function push_to_remote {
dir=$1
branch=$2
echo -e " .. Pushing backup to $2 repo"
git push origin $2
echo -e " done\n"
}
#######################################################
# Cleanup
#######################################################
function cleanup {
echo -e " .. Cleaning up"
git gc
echo -e " done\n"
# Exit banner
echo -e " .. Full site backup complete\n"
}
#######################################################
# Grand Central... direct to the correct actions
# based on number of inputs
#######################################################
# only filepath present: do not backup dB and do not push to remote
if [ $# -eq 1 ] ; then
dir=$1
commit_changes $dir
# only filepath & remote branch present: do not backup dB, push to remote
elif [ $# -eq 2 ]; then
dir=$1
branch=$2
commit_changes $dir
push_to_remote $dir $branch
# filepath and dB info present: backup both, do not push to remote
elif [ $# -eq 4 ]; then
dir=$1
dbname=$2
dbuser=$3
dbpass=$4
dump_db $dir $dbname $dbuser $dbpass
commit_changes $dir
# all arguments present: backup files and db, push to remote
elif [ $# -eq 5 ]; then
dir=$1
dbname=$2
dbuser=$3
dbpass=$4
branch=$5
dump_db $dir $dbname $dbuser $dbpass
commit_changes $dir
push_to_remote $dir $branch
fi
#cleanup and exit
cleanup