-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup_nds.sh
executable file
·92 lines (82 loc) · 2.64 KB
/
backup_nds.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/sh
# path: /home/klassiker/.local/share/repos/shell/backup_nds.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/shell
# date: 2023-11-26T15:24:26+0100
# auth can be something like sudo -A, doas -- or nothing,
# depending on configuration requirements
auth="${EXEC_AS_USER:-sudo}"
backup_path="${1:-.}"
labels="DSONEI R4-SDHC R4I-GOLD R4I-SDHC"
# config (rsync option --dry-run for testing)
rsync_options="-aAXvh --delete \
--exclude='*/gb/' \
--exclude='*/gba/' \
--exclude='*/gbc/' \
--exclude='*/gen/' \
--exclude='*/gg/' \
--exclude='*/nds/' \
--exclude='*/nes/' \
--exclude='*/ngp/' \
--exclude='*/sms/' \
--exclude='*/snes/'"
rsync_saves_options="-aAXvh --delete\
--include='*/' \
--include='*.[Ss][Aa][Vv]' \
--include='*.[Ss][Rr][Mm]' \
--exclude='*'"
find_roms_options="-type f \
! -iname '*.sav' \
! -iname '*.srm'"
# helper functions
rom_list() {
for rom_folder in "$@"; do
roms="$mnt/$rom_folder"
[ -d "$roms" ] \
&& printf ":: create rom list for %s\n" \
"$rom_folder" \
&& eval "find $roms $find_roms_options" \
| sed "s#^$roms/##g" \
| sort > "$backup_path/$label/$rom_folder/list_$rom_folder"
done
}
backup() {
for label in $labels; do
unset mnt
# mount
[ -h "/dev/disk/by-label/$label" ] \
&& printf ":: check file system on %s\n" \
"$label" \
&& $auth fsck.fat "/dev/disk/by-label/$label" \
&& mnt="/tmp/$label" \
&& printf ":: create and mount backup folder %s\n" \
"$mnt" \
&& mkdir -p "$mnt" \
&& $auth mount \
-t vfat \
"/dev/disk/by-label/$label" \
"$mnt"
# backup
[ -d "$mnt" ] \
&& printf ":: create folder and backup %s to %s\n" \
"$mnt" \
"$backup_path/$label" \
&& eval "rsync $rsync_options $mnt $backup_path" \
&& eval "rsync $rsync_saves_options $mnt $backup_path" \
&& rom_list gb gba gbc gen gg nds nes ngp sms snes \
# unmount
[ -d "$mnt" ] \
&& printf ":: unmount and delete mount folder %s\n" \
"$mnt" \
&& $auth umount "$mnt" \
&& find "$mnt" -empty -type d -delete \
&& return 0
done
}
# main
backup \
&& exit 0
printf ":: please connect one of the following devices to backup from:\n"
for label in $labels; do
printf " -> %s\n" "$label"
done