12.07.2015 Views

Steady Hand Fun With The Raspberry Pi - Adrirobot

Steady Hand Fun With The Raspberry Pi - Adrirobot

Steady Hand Fun With The Raspberry Pi - Adrirobot

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Remember, if the list of files is long, you canpipe the result through the "less" commandbash backup | lessThis turns on (+) the executable (x)permission for this file. To be correct (but notneeded in this simple example) the first line ofthe script should containYou can move through the list with up/downarrow keys or PageUp/PageDown keys,press the q key to quit.<strong>The</strong> last step is to make this script anexecutable file. This will allow you to run thescript without putting "bash" before the name.To make the file executable, enter thecommand#!/bin/bashThis tells the operating system which programshould be used to launch this script. You canlaunch the script by typing the command./backupchmod +x backupA more complete exampleHere is a more complete version of the script. You may note that it is a lot morecomplex because some extras have been added.<strong>The</strong> names of the directories are now in variables so that they can be easily changed if you wish.<strong>The</strong> script uses an "if" statement to check if the backup directory already exists.<strong>The</strong> cp command uses the --update option to avoid copying files that have not changed recently.#!/bin/bash# Simple program to make a backup of files on the <strong>Raspberry</strong> <strong>Pi</strong># Set the location of my files and the backup locationmydirectory="/home/pi"safeplace="/backup"echo "Starting backup of files from $mydirectory to $safeplace"# Check if the backup directory existsif [ ! ­d $safeplace ]thenfi# Does not exist, need to make a new directoryecho "Making new directory $safeplace"sudo mkdir $safeplace# Change owner of this directory to the user pisudo chown pi $safeplace# Copy all the files in my directory# recursive means keep drilling down into directories to find files# update means only copy files that have been changed since the last backup# verbose means I want to be told what is happeningcp ­­recursive ­­update ­­verbose $mydirectory $safeplaceecho "Backup is finished"19

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!