forked from digidem/filehub-config
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathusb_backup.sh
141 lines (124 loc) · 5.01 KB
/
usb_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
136
137
138
139
140
141
# Delete existing modification from file, if it exists
sed -i '/#START_MOD/,/#END_MOD/d' /etc/udev/script/add_usb_storage.sh
# Delete exit from end of the file
sed -i '/^exit$/d' /etc/udev/script/add_usb_storage.sh
# Add call to usb backup script after drive mounts
cat <<'EOF' >> /etc/udev/script/add_usb_storage.sh
#START_MOD
# Run backup script
/etc/udev/script/usb_backup.sh &
exit
#END_MOD
EOF
cat <<'EOF' > /etc/udev/script/usb_backup.sh
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
# Kill an existing backup process if running
# (this can happen if you insert two disks one after the other)
if [ -e /tmp/backup.pid ]; then
kill $(cat /tmp/backup.pid)
killall rsync
sleep 1
fi
echo $$ > /tmp/backup.pid
SD_MOUNTPOINT=/data/UsbDisk1/Volume1
STORE_DIR=/sdcopies
BACKUP_DIR=/fotobackup
PHOTO_DIR="$STORE_DIR"/fotos
CONFIG_DIR="$STORE_DIR"/config
#MEDIA_REGEX=".*\.\(jpg\|gif\|png\|jpeg\|mov\|avi\|wav\|mp3\|aif\|wma\|wmv\|asx\|asf\|m4v\|mp4\|mpg\|3gp\|3g2\|crw\|cr2\|nef\|dng\|mdc\|orf\|sr2\|srf\|mts\|rw2\)"
# Check if an SD card is inserted (always mounted at the same mount point on the Rav Filehub)
check_sdcard() {
while read device mountpoint fstype remainder; do
if [ "$mountpoint" == "$SD_MOUNTPOINT" ]; then
# Get the UUID for the SD card. Create one if it doesn't already exist
local uuid_file
uuid_file="$SD_MOUNTPOINT"/sdname.txt
if [ -e $uuid_file ]; then
sd_uuid=`cat $uuid_file`
else
sd_uuid=`cat /proc/sys/kernel/random/uuid`
echo "$sd_uuid" > $uuid_file
fi
return 1
fi
done < /proc/mounts
return 0
}
# Check if a USB drive is attached which is initialize for storing monitoring data
check_storedrive() {
while read device mountpoint fstype remainder; do
if [ ${device:0:7} == "/dev/sd" -a -e "$mountpoint$CONFIG_DIR"/rsync ];then
# Add the config dir (containing rsync binary) to the PATH
export PATH="$mountpoint$CONFIG_DIR":$PATH
store_mountpoint="$mountpoint"
store_id=$(udevadm info -a -p $(udevadm info -q path -n ${device:0:8}) | grep -m 1 "ATTRS{serial}" | cut -d'"' -f2)
return 1
fi
done < /proc/mounts
return 0
}
check_backupdrive() {
while read device mountpoint fstype remainder; do
if [ ${device:0:7} == "/dev/sd" -a -e "$mountpoint$BACKUP_DIR" ];then
backup_mountpoint="$mountpoint"
local backupid_file
backup_id_file="$mountpoint$BACKUP_DIR"/.backup_id
if [ -e $backup_id_file ]; then
backup_id=`cat $backup_id_file`
elif [ $storedrive -eq 1 ]; then
backup_id="$store_id"
echo "$backup_id" > $backup_id_file
fi
return 1
fi
done < /proc/mounts
return 0
}
# If no SD card is inserted, just exit.
check_sdcard
sdcard=$?
check_storedrive
storedrive=$?
check_backupdrive
backupdrive=$?
# If both a valid store drive and SD card are mounted,
# copy the SD card contents to the store drive
if [ $sdcard -eq 1 -a $storedrive -eq 1 ];then
# Organize the photos in a folder for each SD card by UUID,
target_dir="$store_mountpoint$PHOTO_DIR"/"$sd_uuid"
log_dir="$store_mountpoint$STORE_DIR"/log
mkdir -p $target_dir
mkdir -p $log_dir
# Copy the files from the sd card to the target dir,
# Uses filename and size to check for duplicates
echo "$(date): Copying SD card $SD_MOUNTPOINT to $target_dir" >> "$log_dir"/usb_add_info
rsync -vrm --size-only --log-file $log_dir/rsync_log --exclude ".?*" \
$SD_MOUNTPOINT/DCIM \
$SD_MOUNTPOINT/PRIVATE \
$SD_MOUNTPOINT/MISC \
$SD_MOUNTPOINT/MP_ROOT \
$SD_MOUNTPOINT/AVF_INFO \
$target_dir >> "$log_dir"/rsync_stdout
fi
# If both a valid store drive and a matching backup drive are attached,
# backup the store drive to the backup drive
if [ $storedrive -eq 1 -a $backupdrive -eq 1 -a "$backup_id" == "$store_id" ]; then
source_dir="$store_mountpoint$STORE_DIR"
target_dir="$backup_mountpoint$BACKUP_DIR"
partial_dir="$store_mountpoint$PHOTO_DIR"/incoming/.partial
log_dir="$store_mountpoint"/log
echo "Backing up data store to $target_dir" >> "$log_dir"/usb_add_info
rsync -vrm --size-only --delete-during --exclude ".?*" --partial-dir "$partial_dir" --exclude "swapfile" --log-file "$log_dir"/rsync_log "$source_dir"/ "$target_dir"
if [ $? -eq 0 ]; then
echo "$(date): Backup complete" >> "$log_dir"/usb_add_info
else
echo "$(date): Backup failed" >> "$log_dir"/usb_add_info
fi
fi
# Write memory buffer to disk
sync
rm /tmp/backup.pid
exit
EOF
# Make executable
chmod +x /etc/udev/script/usb_backup.sh