-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitSync.sh
244 lines (218 loc) · 4.77 KB
/
gitSync.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/bash
myGit='https://github.com/lp177/42.git'
set -e
###Personal informations (you need to set him):
#Dir wanted for Sync
dirSync="$HOME/Sync"
#Dir wanted for save config files
dirCfg="$dirSync/Cfg"
#Dir target for temporary storage
tmpSync="$HOME/.gitSync/ExtSync"
#Dir tmp for default path when not precise
tmp="$dirSync/.tmp"
#path for gitSync files
gitSyncPath="$HOME/.gitSync"
#Make one save of last archive for configFile/Guest files with _ prefixe.
#Set at 0 for disable
archiver=1
#Path to cp guest config previous infect him
preserveGuest="$dirSync/.preserveGuest"
#interval in second into two auto sync (after launch cmd gitAutoSync)
interval_auto_sync=60
# Target Cfg
favorites=(
"$HOME/.zshrc"
"$HOME/.vimrc"
"$HOME/.vim"
"$HOME/.atom"
"$HOME/.gitSync"
"$HOME/.*.sh"
)
# Colors
red="\033[0;31m"
green="\033[1;32m"
nc="\033[0m"
###Routines:
#Command sh execute previous the save on git with gitSync alias (for save various scattered files/folders)
function myCp
{
if [ -d "$saveFolder/$target" ]
then
echo "Rsync $1"
rsync -a "$1/" --delete --exclude '.git' "$saveFolder/$target/"
elif [ -f "" ]; then
cat "$1" > "$saveFolder/$target"
else
rm -rf "$saveFolder/$target"
cp -R "$1" "$saveFolder/$target"
fi
}
function extractItem
{
if [ -z "$1" ] || ( [ ! -f "$1" ] && [ ! -d "$1" ] ); then
echo "$red$1 not found.$nc"
return 0
fi
echo "Previous base:$1"
local target="$(basename $1)"
echo "After base:$target"
if [ -z "$2" ]
then
local $saveFolder="$tmp"
else
local $saveFolder="$2"
fi
if [ $archiver -eq 1 ] && ( [ -f "$saveFolder/$target" ] || [ -d "$saveFolder/$target" ] )
then
myCp "$saveFolder/$target" "$saveFolder/_$target"
fi
myCp "$1" > "$saveFolder/$target"
if [ -d "$1" ] || [ -f "$1" ]
then
echo "$green$target extracted.$nc"
else
echo "$red$target not extracted.$nc"
fi
}
function saveMyCfg
{
for conf in $favorites
do
extractItem "$conf" "$dirCfg"
done
}
function getCfg
{
for conf in $favorites
do
extractItem "$dirCfg/$(basename $conf)" "$HOME"
done
}
function extractGuest
{
for conf in $favorites
do
echo "extractGuest take $conf"
extractItem "$conf" "$preserveGuest"
done
}
# Reinitialise configuration of guest from $preserveGuest folder
function uninfect
{
echo 'Do you want uninfect ?\n'
read -p 'Reset guest cfg in $preserveGuest folder (y/n):' -n 1 -r
echo
if [[ "$REPLY" =~ ^[Yy]$ ]]
then
for conf in $favorites
do
extractItem "$preserveGuest/$(basename $conf)" "$HOME"
done
fi
}
# Load your cfg on guest device and preserve guest cfg in $preserveGuest folder
function infect
{
echo 'Do you want infect ?'
echo 'Get your cfg on guest and preserve guest cfg in $preserveGuest folder (y/n):'
REPLY=''
read
echo
if [[ "$REPLY" =~ ^[Yy]$ ]]
then
extractGuest
getCfg
source "$HOME/.zshrc"
echo "Done."
else
echo "Have a good day."
fi
}
function updateRemote
{
cd $tmpSync
find */ -name .git | sed 's/\/\//\//' | xargs git rm -rf --ignore-unmatch
find */ -name .git | sed 's/\/\//\//' | xargs rm -rf
rm -rf .git
git init
git remote add origin $myGit
git add ./*
git commit -am 'Update `date`'
git push -f origin master
cd -
}
###
###Alias:
alias gitSync="
echo 'In progress...'
saveMyCfg
rsync -a $dirSync/ --delete --exclude '.git' $tmpSync/
updateRemote
"
#Launch auto gitSync all interval_auto_sync seconde(s) (not require cron)
alias gitSyncAuto="
echo \"Start at: \";date;echo \"\n\"
gitSync
echo \"\n\nEnd at: \"
date
echo \"Pending next interval ... ($((interval_auto_sync / 60))mn)\"
sleep $interval_auto_sync
clear
source $gitSyncPath/cronErsatz
"
#Get your git depot to local $dirSync
# /!\ Delete the holdest $dirSync & $tmpSync path given at the top of this file
alias gitSyncTake="
rm -rf $tmpSync $dirSync
git clone $myGit $tmpSync
cp -R $tmpSync $dirSync
rm -rf $dirSync/.git
infect
"
#/!\ Delete all trace of gitSync
alias gitSyncClean="
uninfect
ssh-keygen -R $myGit
rm -rf $HOME/.ssh/known_hosts.old
$uninfect
rm -rf $tmpSync $dirSync
gitSyncUninstall
exit
"
# Uninstaller
alias gitSyncUninstall="
$gitSyncPath/uninstaller $gitSyncPath $dirSync
"
###
###Create busy files if necessary
#set -x
function createFolder
{
if [ -z "$1" ] || [ -d "$1" ]; then
return 0
fi
mkdir $1
# &> /dev/null
if [ -d $1 ]; then
echo "$green Create Dir $(basename $1) at $1$nc"
else
echo "$red Fail to create Dir $(basename $1) at $1$nc"
fi
}
createFolder $dirSync
createFolder $tmpSync
createFolder $dirCfg
createFolder $preserveGuest
createFolder $tmp
if [ ! -f $gitSyncPath/cronErsatz ]
then
echo "source $gitSyncPath/gitSync.sh 2> /dev/null;\`gitAutoSync\`" > $gitSyncPath/cronErsatz
if [ -f $1 ]; then
echo "$green Create cronErsatz at $gitSyncPath/cronErsatz$nc"
else
echo "$red Fail to create cronErsatz at $gitSyncPath/cronErsatz$nc"
fi
fi
#set +x
###
set +e