Last active
June 19, 2023 13:09
-
-
Save stefanherdy/f6a909a8e955564d63add1fb4dca0dda to your computer and use it in GitHub Desktop.
Copy all csv-files from the data folder to the last added usb-device. Implemented as a cron job, this can be used for automated data logging/ data backup. We used this script in the remote areas of the Amazon Rain Forest to do regular backups of our sensor stations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script is the main script to automatically mount | |
# a USB device to the data folder and copy the required data | |
# Data folder: /your/data/folder/path/ (the folder where the data is stored) | |
# Target folder: /media/usb/ (the mounted USB folder, where the data is copied to) | |
#exec >> /home/your_directory/backup-sync.log 2>&1 | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
NC='\033[0m' | |
echo 'Copy microclimate data to USB device' | |
echo 'Date/Time:' | |
date | |
# Load USB device data path | |
echo 'Searching for latest USB device' | |
d=$(lsblk --noheadings --raw --output rm,tran,type,path --sort path | awk '/^1 part/ {d=$3} END {print d}') | |
echo 'USB device path:' | |
echo $d | |
# Mount the USB device to the folder /media/usb | |
echo 'Mounting USB device to path...' | |
mount -v $d /media/usb | |
echo 'Mounted path:' | |
m=$(lsblk --noheadings --raw --output rm,tran,type,path,mountpoint --sort path | awk '/^1 part/ {m=$4} END {print m}') | |
echo $m | |
# Check if the mounting point /media/usb exists | |
if [[ $m = /media/usb ]] | |
then | |
printf ${GREEN}'USB device successfully mounted\n' | |
printf ${NC} | |
else | |
printf ${RED}'USB-device not successfully mounted to path /media/usb\n' | |
printf ${NC} | |
fi | |
# List all block devices | |
lsblk | |
# Transfer all csv-Files from /your/data/folder/path/ to the mounted USB device folder | |
if [[ $m = /media/usb ]] | |
then | |
echo 'Tranferring data' | |
cp -v /your/data/folder/path/*.csv /media/usb/ | |
printf ${GREEN}'Data transfer was successful\n' | |
printf ${GREEN}'Transfer time:' | date | |
printf ${NC} | |
else | |
printf ${RED}'Data transfer stopped! No USB device detected! Please insert USB device\n' | |
printf ${NC} | |
fi | |
sleep 3 | |
# Unmount the USB device after data transfer | |
echo 'Unmounting again..' | |
umount -v -f /media/usb | |
lsblk | |
echo 'Process Done! You can now deconnect your USB device' | |
date |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment