Saturday, October 5, 2013

noob tools 1

A very rough preview of what is to come: noob version 0.1. I've been working on it a little bit at a time. Backing up SD card using dd remains the longest activity. Make sure the filename is noob01.sh or fix it on the install case. Or else, it won't install.


#!/bin/bash

#echo "$0"

case "$1" in
halt)
echo "Halting"
sync
sleep 3
sudo halt
;;

reboot)
echo "Rebooting"
sync
sleep 3
sudo reboot
;;

wallpaper)
echo "Choosing random jpg as wallpaper"
PIC=$(ls *.jpg | shuf -n1)
pcmanfm -w $PIC
echo $PIC
;;

blank)
echo "Enable Screensaver"
sudo xset s blank      #Enable blanking
sudo xset s on         #Enable screensaver
sudo xset +dpms        #Enable Energy Saving
;;

noblank)
echo "Disable Screensaver"
sudo xset s noblank   #Disable blanking
sudo xset s off       #Disable screensaver
sudo xset -dpms       #Disable Energy Saving
;;

update)
echo "Updating package list"
sudo apt-get update
#sudo apt-get upgrade
##rpi-update   ###DANGEROUS### Do not use!!!
;;

pkglist)
echo "Available packages"
apt-cache pkgnames    #Show all package names
;;

pkgondev)
echo "Installed packages"
#dpkg --get-selections  #Show installed packages
dpkg --get-selections | grep -o -e ^[[:graph:]]*
;;

backup)
echo "Backing up directories to /media/SD/"
echo
echo "Warning: This will copy the files from internal SD card"
echo "to external USB Flashdrive/SD Card. rsync is used to mirror"
echo "the files. Make sure you choose the right directory."
echo
echo "The directories are:"
for i in {0..99}; do sdlist[i]=0; done
j=0

for i in `ls /media`; do
  sdlist[$j]="/media/$i"
  ((++j))
done

for i in {0..99}; do
  if [ -d ${sdlist[$i]} ]; then
    echo $i ${sdlist[$i]}
  fi
done

echo -n "Please choose a directory: "
read sdint

sleep 1
echo "Warning: You are about to overwrite the content of "
echo ${sdlist[$sdint]}
echo "Continue (y/n)? "
read prompt

case $prompt in
y | Y)
echo "rsync --delete -axv /boot ${sdlist[$sdint]}/boot"
sleep 5
sudo rsync --delete -axv /boot ${sdlist[$sdint]}/boot
sleep 5
echo "rsync --delete -axv / ${sdlist[$sdint]}"
sleep 5
sudo rsync --delete -axv / ${sdlist[$sdint]}
sleep 5
sync
sync
echo "Done!"
sleep 2
;;
*)
echo "Terminating operation..."
sleep 2
;;
esac
;;

restore)
echo "Restoring up directories from /media/SD/"
echo
echo "Warning: This will copy the files from external USB "
echo "Flashdrive/SD Card to internal SD card. rsync is used to"
echo "mirror the files. Make sure you choose the right directory."
echo
echo "The directories are:"
for i in {0..99}; do sdlist[i]=0; done
j=0

for i in `ls /media`; do
  sdlist[$j]="/media/$i"
  ((++j))
done

for i in {0..99}; do
  if [ -d ${sdlist[$i]} ]; then
    echo $i ${sdlist[$i]}
  fi
done

echo -n "Please choose a directory: "
read sdint

sleep 1
echo "Warning: You are about to copy the content of "
echo ${sdlist[$sdint]}
echo "Continue (y/n)? "
read prompt

case $prompt in
y | Y)
echo "rsync --delete -axv ${sdlist[$sdint]}/boot /boot"
sleep 5
sudo rsync --delete -axv ${sdlist[$sdint]}/boot /boot
sleep 5
echo "rsync --delete -axv ${sdlist[$sdint]} /"
sleep 5
sudo rsync --delete -axv ${sdlist[$sdint]} /
sleep 5
sync
sync
echo "Done!"
sleep 2
;;
*)
echo "Terminating operation..."
sleep 2
;;
esac

;;


help | -h | --help)
echo "Help File"
echo
echo "Noob Tools is a collection of bash shell scripts, wrapped in simple command line for ease of use. The user is encouraged to examine the corresponding man pages for further info."
echo
echo "To install this as a command use:"
echo "  sudo ln -s /home/pi/noob.sh /usr/bin/noob"
echo "where /home/pi/noob.sh is the name of the script."
echo
echo "This script is work in progress. Currently pre-pre-alpha."
;;

install)
echo "Installing noob on /usr/local/bin/noob"
sudo ln -s /home/pi/noob01.sh /usr/local/bin/noob
;;

uninstall)
echo "Removing noob from /usr/local/bin/noob"
sudo rm /usr/local/bin/noob
;;

where)
echo "The location of noob is"
which noob
;;

print)
#For printing A4 paper size in US letter size
lp -o scaling=96 $2
;;

clone)
echo "Disk Dumping to SD Card"
echo
if [[ $2 == '' ]]; then
  path="/dev/sda"
else
  path=$2
fi
echo "This will erase existing data on $path"
read -p "OK to delete SD card on $path? [y/n] " REPLY
  if [[ $REPLY == 'y' || $REPLY == 'Y' ]]; then
     echo "Copying /dev/mmcblk0 to $path"
     sleep 10
     sudo dd if=/dev/mmcblk0 of=$path & pid=$!
     sleep 10

     while [ `ls /proc | grep $pid` ]; do
       sudo kill -USR1 $pid
       sleep 15
     done
     
     sync
     sync
     echo "Finished!"
  else
     echo "Terminating process"
  fi
;;

faq)
echo "Raspberry Pi Mini FAQ"
echo
echo "dd clone operation may fail due to cards of differing size. If the "
echo "target card is smaller than the original, the operation will fail with"
echo "insufficient size error message."
echo
echo "You may circumvent the problem by creating a second Raspbian system"
echo "and backup your current system onto that."
echo
echo "To terminate Xserver. Logout, and do Ctrl-Alt-F1"
echo
echo "Wallpaper seems to be changeable from home root directory"
;;

source)
cat `which noob`
;;

credit | credits)
echo "Noob Tools ver. 0.1"
echo "Oct 4, 2013"
echo "by Harry M. Hardjono"
echo
echo "Specifically written for use on Raspberry Pi"
echo
;;

*)
echo "noob tools: a collection of easy to use commands suitable for beginners."
echo "Written by Harry M. Hardjono"
echo "Sep 30, 2013"
echo
echo "Usage:"
echo "noob help             :Show help screen"
echo "noob faq              :Mini FAQ"
echo "noob install          :Install noob on system"
echo "noob uninstall        :Uninstall noob on system"
echo "noob where            :Shows installed path if any"
echo "noob update           :Update system (packagelist (network required))"
echo "noob halt             :Safely shutdown device"
echo "noob reboot           :Safely reboot device"
echo "noob wallpaper        :Random jpg as wallpaper"
echo "noob blank            :Enable screen blanking"
echo "noob noblank          :Disable screen blanking"
echo "noob pkglist          :List all available packages"
echo "noob pkgondev         :List all installed packages"
echo
echo "noob print (filename) :Print slighly smaller print"
echo "noob clone (/dev/sd?) :Duplicate main SD to external SD"
echo "noob backup           :Mirror files from internal SD to /media"
echo "noob restore          :Mirror files from /media to internal SD"
echo
echo "noob source           :Source code for noob"
echo "noob credit           :Credits for noob"
;;
esac

exit 0
                 
#Todo
#noob faq             frequently asked question
#adduser user group   auto create group
#userusbon user       mount user directory to USB
#userusboff user      mount user directory to SD
#userusbusb user      copy user directory from USB to SD
#userusbsd user       copy user directory from SD to USB
#tarback drive USB    dated tarball backup
#tarrestore drive USB dated tarball restore
#format USB size      format USB for ext4 (use dd tar xz of various size 4MB 8MB 16MB 32MB)


No comments:

Post a Comment