Skip to content

Instantly share code, notes, and snippets.

@quinnjn
Created January 28, 2014 03:36
Show Gist options
  • Save quinnjn/8661884 to your computer and use it in GitHub Desktop.
Save quinnjn/8661884 to your computer and use it in GitHub Desktop.
I need a script for my Raspbery Pi that backs up my GO PRO videos to my external hard drive when both are plugged in. The Pi will be mounted on my motorcycle and this allows me to just plug in both sd card and external hard drive and auto backs up without any input from me.
#!/usr/bin/python
import subprocess
from pprint import pprint
import sys
import errno
from time import gmtime, strftime
import os
MOUNT_FOLDER = 'media' #Where the OS mounts the drives.
MIN_SD_CARD_SIZE = 0 #Minimum SD card size to look for.
MAX_SD_CARD_SIZE = 35000000 #Max SD card size to look for, which is a 32GB card for me.
FILE_TYPES = ['MP4'] #The file types to look for on the sd card.
TIMESTAMP_FORMAT = "%Y%m%d-%H%M%S" #The timestamp format to create a folder with on the TO drive.
def get_media_drives():
global MOUNT_FOLDER
rough_drives = subprocess.check_output('df').split("\n")
drives = {}
for rdrive in rough_drives:
if MOUNT_FOLDER in rdrive:
items = rdrive.split()
drives[items[5]] = items[3]
return drives
def get_biggest_drive_name(drives):
biggestDrive = 0
biggestDriveName = ""
for name in drives.keys():
if(drives[name] > biggestDrive):
biggestDrive = drives[name]
biggestDriveName = name
return biggestDriveName
def get_sd_drive_name(drives):
global MIN_SD_CARD_SIZE
global MAX_SD_CARD_SIZE
for name in drives.keys():
if MIN_SD_CARD_SIZE < int(drives[name]) < MAX_SD_CARD_SIZE:
return name
return ''
def videos_exist(loc):
global FILE_TYPES
for root, folder, files in os.walk(loc):
for file in files:
for type in FILE_TYPES:
if file.upper().endswith(type.upper()):
return True
return False
drives = get_media_drives()
storageLoc = get_biggest_drive_name(drives)
sdCardLoc = get_sd_drive_name(drives)
newFolder = strftime(TIMESTAMP_FORMAT, gmtime())
#pprint(drives)
#print storageLoc
#print sdCardLoc
if(videos_exist(sdCardLoc) and storageLoc and sdCardLoc):
print "rsync -av --progress --remove-source-files -zvh %s/*/*.MP4 %s/%s/" % (sdCardLoc, storageLoc, newFolder)
else:
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment