Bash Scripts and stuff.

SlartibartfastSlartibartfast Global Moderator-__-
edited April 2011 in Tech & Games
I this thread we post our bash scripts.

Link to this thread when referring to a script.
This will help keep the *nix forum organised.

general instructions for running scripts:

> make exectuable with chmod +x /path/to/script.sh
> run with /path/to/./script.sh

Comments

  • SlartibartfastSlartibartfast Global Moderator -__-
    edited January 2011
    An earlier thread here got me thinking about how to wipe free space under Linux. It debatable whether or not it's really necessary with current data usage trends but i have worked out the best way to do it i think.
    It's turns out it's reasonable trivial; I've modified some instructions on found on a forum (lost the link)

    Here's the script that does it:
    Quote:
    #!/bin/bash
    # creates a file (zero.file) the size of the free space and fills it with random character and deletes it.
    # change /dev/random to /dev/zero for 0 characters (changing this makes it faster but less secure)
    # 35GB free space took me about 2.5 hours each 'pass'
    # 'cat: no space on disk error' (or similar) is normal (file is deleted after that)
    # change c=10 to whatever many passes you want so, c=15 , c=50 and so on
    for (( c=5; c>0; c-- ))
    do
    echo "$c passes left"
    echo "creating random files"
    dd if=/dev/random of=zero.small.file bs=1024 count=102400;
    cat /dev/random > zero.file;
    rm -f zero.small.file;
    shred -n 1 -z -v -u zero.file;
    done
    

    How it works:
    read the comments. essentially what it does is write two random files, one file is 100meg the other the size of your remaining free diskspace.
    It then deletes both; the 100meg file is removed with rm (to quickly free some space for the OS to write logs and other stuff) and the bigger file is removed with shred.

    Things to understand:
    with the use of the shred utility, each pass is actually going twice. The first is randomising the free space and then the other is randomising the randomised file, replacing it all with zeros and deleting it.

    Don't copy stuff onto your disk while this is running. Assume the most disk space you have is 100meg

    How to run:
    It's a bash script.
    > save in home directory as a file called 'wipespace'
    > set to executable with chmod +x ~/wipespace
    > run with ./wipespace

    It will slow your system as it runs. Honestly this is as good as the most expensive commercial apps out there.

    you may want to boot into a lower runlevel with: 'sudo telinit 3' before running the script, to get back to a higher run level: 'sudo telinit 5' (or reboot).
    If you want to run it without supervision you must boot into a lower runlevel or else your DE will pause the script (at 90% fill for safety) until you click ignore
  • SlartibartfastSlartibartfast Global Moderator -__-
    edited January 2011
    Optionally records desktop mic and webcam

    YOU MUST must have ffmpeg and mplayer installed.
    Ideally use the resolution of your desktop, ffmpeg freaks out with a weird resolution.
    #!/bin/bash
    # destop recorder 
    # captures screen and optionally superimposes webcam. 
    
    function camtest() {
    echo "webcam video may the the wrong way but..."
    echo "is your webcam feed displayed on the desktop? [y/n]"; read camworking 
    if [[ $camworking == "n" || $camworking == "N" ]]
    then
    kill -9 $mplayerPID
    echo "**************************************************************************"
    echo "fixing your cam is beyond this script. webcam recording has been disabled."
    echo "**************************************************************************"
    record
    else
    echo "is your webcam feed upside down? [y/n]"; read camside
    fi
    if [[ $camside == "N" || $camside == "n" ]]
    then
    record
    else
    echo "flipping..."
    kill -9 $mplayerPID
    mplayer -cache 128 -tv driver=v4l2:width=176:height=177 -vo xv tv:// -noborder -geometry "95%:93%" -ontop -flip &>/dev/null &  
    mplayerPID=$!;
    echo "flipped"
    record
    fi
    }
    
    function record() {
    echo "would you like to record your mic? [y/N]"; read m
    echo "With what x resolution would you like to record the desktop :"; read x
    echo "With what is y resoluton would you like to record the desktop:"; read y
    echo ""
    echo ""
    echo ""
    echo "******************************************************************"
    echo " All recordings will be automatically saved in your home directoty"
    echo "******************************************************************"
    echo " Do you want to start Recording?[y/N]:"; read g
    
    if [[ $g == "Y" || $g == "y" ]] && [[ $m == "y" || $m == "Y" ]]
    then 
    ffmpeg -f alsa -i pulse -f x11grab -r 30 -s "$x"x"$y" -i :0.0 -acodec pcm_s16le -vcodec libx264 -vpre lossless_ultrafast -threads 0 ~/output-$(date +"%Y%m%d%H%M%S").mkv 
    elif [[ $g == "Y" || $g == "y" ]]  && [[ $m == "n" || $m == "N" ]]
    then
    ffmpeg -f x11grab -r 30 -s "$x"x"$y" -i :0.0  -vcodec libx264 -vpre lossless_ultrafast -threads 0 ~/output-$(date +"%Y%m%d%H%M%S").mkv 
    else
    echo "exiting.."
    fi
    
    if [[ $c == "Y" || $c == "y" ]] 
    then 
    kill -9 $mplayerPID
    fi
    
    }
    
    echo "********************************************************"
    echo "Desktop recorder, NOT CURRENTLY recording"
    echo "********************************************************"
    echo " "
    echo " "
    echo "would you like to capture your webcam in the session [y/n]:"; read c
    if [[ $c == "y" || $c == "Y" ]] 
    then
    mplayer -cache 128 -tv driver=v4l2:width=176:height=177 -vo xv tv:// -noborder -geometry "95%:93%" -ontop &>/dev/null & 
    mplayerPID=$!
    camtest
    else echo "Webcam recording off.."
    record
    fi 
    

    How to run:
    It's a bash script.
    > save in home directory as a file called 'recorder'
    > set to executable with chmod +x ~/recorder
    > run with ./recorder
  • BaconPieBaconPie Regular
    edited April 2011
    Here is my backup script. You just type 'backup' and it backs up the default directory. If you give it an argument it will back up that directory instead, for example:
    $ backup
    # backup /etc
    

    It just copies all the files to another directory (on my external HDD). If a file hasn't been modified then it is ignored. Also, it auto mounts the backup point so I only have to switch my external HDD on, it backs up, then I turn it off. :)

    You will need to modify it, for it to work with your system.

    I run this daily with a cron tab. If I had my external switched on 24/7 then I could even run it hourly.
    #!/bin/bash
    #: Description : A simple backup script
    #: Author      : BaconPie
    
    ## Default backup directory
    defaultDirectoryToBackup="/home/tom"
    
    ## Default backup device
    backupDevice="/media/ExternalHD"
    backupTo="$backupDevice/backup"
    
    ## Lock file to ensure only one running copy at a time
    backupLock="/tmp/backuplocktom"
    
    ###############################################################################
    function backupTheDirectory()
    {
      backupFrom=$1
      extraopts=$2
      
      printf "\n------------------------------------------------------------\n"
      printf "Backing up %s\n" $backupFrom
      
      ## excludes file
      ## *.txt, *~ etc. Onle one "pattern" per line
      excludes=/home/tom/.backupFiles/places.exclude
    
      ## For testing. Only displays what rsync would do and does no actual copying.
      #opts="-n --update --archive -vv --exclude-from=$EXCLUDES --stats --progress"
      
      ## Does copy. Still gives a verbose display of what it is doing.
      ## -h is --human-readable
      opts="--archive --update -v --exclude-from=$excludes --stats --progress
            --human-readable"
      
      ## Actually do the backup
      rsync $opts $extraopts $backupFrom $backupTo
    }
    ###############################################################################
    
    ## Start default backup (main)
    
    ## Test if a backup is not already in process
    if [ -e $backupLock ]
    then
      ## If there is, print it's PID so that we can kill it
      printf "There is already a backup in progess (PID: %s)\n" $(cat $backupLock)
      exit 1
    fi
    
    ## Test if the backup device is mounted
    grep -q "$backupDevice" /etc/mtab
    if [ $? != 0 ]
    then
      printf "%s is not mounted (according to mtab)\n" $backupDevice
      
      devicePreviousState="unmounted"
      
      ## We should try to mount the device and do the backup anyway
      printf "Trying to mount..."
      mount $backupDevice
      if [ $? != 0 ]
      then
        printf "\n" ## mount with produce an adequate fail message
        exit 1
      fi
      printf "Success!\n"
    fi
    
    ###############################################################################
    ## We have passed all tests, run the backup
    
    ## Tell the user we are starting
    printf "\nBackup started: %s\n" "$(date)"
    
    ## Enable the lock
    echo $$ > $backupLock
    
    ## Call the function with the directories you want to back up
    ## Add the second argument for any extra options you want to give to rsync
    ## If we are given an argument then backup that directory otherwise do the
    ## default one
    if [[ $1 != "" ]]
    then
      backupTheDirectory "$1"
    else
      ## Default backup
      backupTheDirectory "$defaultDirectoryToBackup"
    fi
    
    ## Remove the lock
    rm $backupLock
    
    ## Tell the user we have finished
    printf "\nBackup completed: %s\n" "$(date)"
    ###############################################################################
    
    ## Return the device to it's previous state
    if [[ $devicePreviousState == "unmounted" ]]
    then
      printf "\nUnmounting..."
      umount $backupDevice
      printf "Done!\n"
    fi
    
    ## Exit cleanly
    exit 0
    

    If I had a big enough hard disk then I'd make it backup to a new directory every hour and hard link files that haven't changed. Just like Time Machine for OSX (which is awesome by the way).

    Feel free to suggest improvements, I'm always looking to make things better. :thumbsup:
Sign In or Register to comment.