-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
executable file
·135 lines (121 loc) · 3.86 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
#!/usr/bin/env bash
# shellcheck shell=bash
# shellcheck disable=SC1036,SC1056,SC1072,SC1073,SC1009
source ./bin/loaders.sh
# @function ProgressBar
# @description Show a progress bar animation by rendering a progress bar between a start and end value
function ProgressBar {
let _progress=(${1}*100/${2}*100)/100
let _done=(${_progress}*4)/10
let _left=40-$_done
_fill=$(printf "%${_done}s")
_empty=$(printf "%${_left}s")
printf "\rProgress : [${_fill// /#}${_empty// /-}] ${_progress}%%"
}
# @function backup_npm
# @description Backup the list of globally installed npm packages
function backup_npm {
_start=1
_end=$(npm list -g --depth 0 -p 2>/dev/null | sed -e '1d' | wc -l)
npm list -g --depth 0 -p -l 2>/dev/null | sed -e '1d' | \
while read i
do
app="${i#*:}"
app_info=$(npm view "$app" 2>/dev/null | sed -n '3 p')
echo "\"$app::$app_info\"" >> backup/default.npm.list.sh
ProgressBar ${_start} ${_end}
((_start=_start+1))
done
echo ""
}
# @function backup_pip
# @description Backup the list of globally installed pip packages
function backup_pip {
_start=1
_end=$(pip list --user 2>/dev/null | sed -e '1,2d' | wc -l)
pip list --user 2>/dev/null | sed -e '1,2d' | \
while read i
do
_app=($i)
app=${_app[0]}
_app_info=$(pip show "$app" 2>/dev/null | sed -n '3 p')
app_info=${_app_info#"Summary: "}
echo "\"$app::$app_info\"" >> backup/default.pip.list.sh
ProgressBar ${_start} ${_end}
((_start=_start+1))
done
echo ""
}
# @function backup_brew
# @description Backup the list of brew formulaes
function backup_brew {
_start=1
_end=$(brew leaves | wc -l)
brew leaves | sed | \
while read i
do
app=$i
app_info=$(brew info "$app" 2>/dev/null | sed -n '2 p')
echo "\"$app::$app_info\"" >> backup/default.brew.list.sh
ProgressBar ${_start} ${_end}
((_start=_start+1))
done
echo ""
}
# @function backup_casks
# @description Backup the list of brew cask software
function backup_cask {
_start=1
_end=$(brew list --casks | wc -l)
brew list --casks | sed | \
while read i
do
app=$i
app_description_line=$(brew info --cask "$app" 2>/dev/null | awk '/^==> Description/ { print NR;}')
((app_info_line=app_description_line+1))
app_info=$(brew info --cask "$app" 2>/dev/null | sed -n "$app_info_line p")
if [ "$app_info" = "None" ]; then
echo "\"$app::\"" >> backup/default.cask.list.sh
else
echo "\"$app::$app_info\"" >> backup/default.cask.list.sh
fi
ProgressBar ${_start} ${_end}
((_start=_start+1))
done
echo ""
}
# @function backup_mas
# @description Backup the list of mas software
function backup_mas {
_start=1
_end=$(mas list | wc -l)
mas list | sed | \
while read i
do
_app=($i)
app=${_app[0]}
app_info=$(mas info "$app" 2>/dev/null | sed -n '1 p')
echo "\"$app::$app_info\"" >> ./backup/default.mas.list.sh
ProgressBar ${_start} ${_end}
((_start=_start+1))
done
echo ""
}
softwareList="pip mas npm cask brew"
software=($softwareList)
# Create the backup directory if doesn't exist silently
mkdir -p backup
printf "\n%s\n" "Backing up the machine list of installed $softwareList software"
for _software in "${software[@]}"; do
printf "\n%s\n" "Backing-up ${_software}"
_list_name="./backup/default.$_software.list.sh"
touch $_list_name
cat >$_list_name <<EOL
# @Name: Default
# @List: ${_software}List
export ${_software}List=(
EOL
backup_function="backup_$_software"
$backup_function
echo ")" >> $_list_name
done;