-
Notifications
You must be signed in to change notification settings - Fork 6
/
back_me_up.sh
executable file
·174 lines (146 loc) · 4.18 KB
/
back_me_up.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
#!/bin/bash
# A script to back up user files.
# MIT License - Copyright (c) 2021 Nicolás Castellán
# THE SOFTWARE IS PROVIDED "AS IS"
# Read the included LICENSE file for more information
DRIVE="Data & BackUps" # Destination drive
KEEP=3 # Maximum number of backups to keep
##############################################################################
USAGE_MSG() {
printf "\e[01mThis script creates a backup of your files in a secondary drive
\e[00mOptions:
\e[01m-m\e[00m) Backup .minecraft folder.
\e[01m-v\e[00m) Backup virtual machines.
\e[01m-k\e[00m) Override numbers of backups to keep.
\e[01m-d\e[00m) Override backup drive.
\e[01m-h\e[00m) Display a usage message.\n"
}
EXTENDED_MSG() {
printf "\nDefaults:
- Backup drive : \"%s\"
- Backups to keep: %i
To change the defaults, edit the variables \e[35m\$DRIVE\e[00m and \e[35m\$KEEP\e[00m at
the top of the script.
Examples:
./$(basename "$0")
./$(basename "$0") -m -k 4
./$(basename "$0") -m -v -k 4 -d \"Storage drive\"\n" "$DRIVE" "$KEEP"
}
backup_minecraft=no
backup_vms=no
# Process arguments
while [ -n "$1" ]; do
case "$1" in
-m) backup_minecraft=yes ;;
-v) backup_vms=yes ;;
-k) KEEP="$2"; shift ;;
-d) DRIVE="$2"; shift ;;
-h | --help)
USAGE_MSG >&2
EXTENDED_MSG >&2
exit 0
;;
*)
printf "\e[31mUnknown option:\e[00m %s\n" $1 >&2
USAGE_MSG >&2
exit 1
;;
esac; shift; done
unset USAGE_MSG EXTENDED_MSG
# The destination folder
destination="/media/$USER/$DRIVE"
# The list of things to back up
LIST+=("Desktop")
LIST+=("Documents")
LIST+=("Development")
LIST+=("Templates")
LIST+=("Pictures")
LIST+=("Music")
LIST+=("Videos")
LIST+=("GIMP")
LIST+=(".mydock")
LIST+=(".bashrc")
LIST+=(".zshrc")
LIST+=(".zsh_aliases")
LIST+=(".vimrc")
LIST+=(".clang-format")
[ "$backup_minecraft" = "yes" ] && LIST+=(".minecraft")
[ "$backup_vms" = "yes" ] && LIST+=(".vms" "VirtualBox VMs")
# The pretty animation to display
Animate() {
CICLE=('|' '/' '-' '\')
while true; do
for i in ${CICLE[@]}; do
printf "$1 %s\r" $i
sleep 0.2
done
done
}
# Testing if the drive is connected.
if [ ! -d "$destination" ]; then
printf "\e[31mERROR: \"${DRIVE}\" drive not found.\e[00m\n" >&2
exit 1
fi
printf "Backing up in \e[36m%s\e[00m\n" "$DRIVE"
# Create global destination folder
destination="$destination/Backings"
mkdir -p "$destination"
# Set the final destination folder name
TODAY=$(date +"%Y-%m-%d-%H-%M")
# Testing if the user is backing up again too soon.
if [ -d "$destination/$TODAY" ]; then
printf "\e[31mERROR: You have to wait to make another backup.\e[00m\n" >&2
exit 1
fi
# Remove old backups
let found=0
for i in $(ls "$destination"); do
let found++
done
# You must always keep at least one backup
[ $KEEP -lt 1 ] && let KEEP=1
if [ $found -ge $KEEP ]; then
Animate "Removing \e[31mold backups\e[00m" & PID=$!
let to_delete="found - KEEP + 1"
for i in $(ls "$destination"); do
[ $to_delete -gt 0 ] && rm -rf "$destination/$i"
let to_delete--
done
kill $PID; printf "Removing \e[31mold backups\e[00m, \e[32mDone\e[00m\n"
fi
# Create final destination folder
destination="$destination/$TODAY"
mkdir -p "$destination"
# Make the backup
cd $HOME
helper_file="$HOME/.tmp_helper.txt"
IFS="$(echo -en "\n\b")"
for i in ${LIST[@]}; do
FOUND=no
if [ -f "$i" ] || ( [ -d "$i" ] && [ -n "$(ls -A "$i")" ] ); then
FOUND=yes
fi
[ "$FOUND" = "no" ] && continue
case "$i" in
Development)
Animate "Copying \e[33m$i\e[00m" & PID=$!
printf "node_modules\nobj\nout\nbuild\nbin" > "$helper_file"
rsync -L -r --exclude-from "$helper_file" "$i" "$destination"
kill $PID; printf "Copying \e[33m%s\e[00m, \e[32mDone\e[00m\n" "$i"
;;
.minecraft) # Copy only relevant files
Animate "Copying \e[33m$i\e[00m" & PID=$!
printf "assets\nlibraries\nlogs\nmods\nversions\nlauncher.jar\nlauncher.pack.lzma\nusercache.json" > "$helper_file"
rsync -r --exclude-from "$helper_file" "$i" "$destination"
kill $PID; printf "Copying \e[33m%s\e[00m, \e[32mDone\e[00m\n" "$i"
;;
*)
Animate "Copying \e[33m$i\e[00m" & PID=$!
rsync -L -r "$i" "$destination"
kill $PID; printf "Copying \e[33m%s\e[00m, \e[32mDone\e[00m\n" "$i"
;;
esac
done
rm "$helper_file"
exit 0
# Thanks for downloading, and enjoy!