Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8374

Beginners • Re: SD Card "Cloner"

$
0
0
i had a similar desire some time ago, and wrote this script.
SEE the notes at end of script to see how to get it to autorun.

Code:

#! /bin/bash# code to copy all files on a SINGLE USB drive (only one drive mounted at /media/$USER)# to a folder of todays date (mm-dd-yyyy) in the users HOME folder### creates a log file: "copyusb.txt" in users HOME folder##  TerribleTed Nov 2022# UPDATED Nov 27 2022 : Changed all "$USERNAME" to "$USER" (oops)###get volume name from /media/$USERmyusb=$(ls /media/$USER )# if myusb is not empty....#if [ "$myusb" != "" ] ; thenecho Usb drive $myusb found#set log file name (in users home folder)logfile="$HOME/copyusb.txt"date>>$logfile# set mydate to todays datemydate=$(date +"%m-%d-%Y")echo Copying files FROM: $myusb   TO: $mydate.....>>$logfile# copy the files from the USB to the folder# make the destination folder, even if it already existsmkdir -p $HOME/$mydate# use the find command to only get files, copy themfind "/media/$USER/$myusb/" -maxdepth 1 -type f -exec cp -vt "$HOME/$mydate/" {} + >>$logfile 2>%1# write dashes to the copyusb.txt log file as session delimiterecho "-------------"  >>$logfile#unmount the drive (so drive is only copied once)umount -v "/media/$USER/$myusb"elseecho No USB drive found !fi# from: https://blog.jasonantman.com/2009/11/running-a-script-on-usb-drive-insertion/#So, you want to run a specific script on insertion of a USB drive. Here’s how to use udev to do it:#Create /etc/udev/rules.d/99-usbhook.rules:#ACTION=="add",KERNEL=="sd*", SUBSYSTEMS=="usb", ATTRS{product}=="Mass Storage", RUN+="/root/bin/usbhook %k"#This will run “/root/bin/usbook”, passing it the device name as an argument, every time a USB Mass Storage device is plugged in.#run udevcontrol reload_rulesudevcontrol reload_rules#Create your usbhook script.#Enjoy

Statistics: Posted by terribleted — Fri Jan 09, 2026 2:57 pm



Viewing all articles
Browse latest Browse all 8374

Trending Articles