The full Raspberry Pi Workshop in step-by-step format can be found here http://coreelec.io/piworkshop In this section we explore how you can write a useful backup script to copy files to a safe location

Transcript

Okay now it's time to write a more useful script, what we're going to write is I'm going to call it an archiving script, what this script will do is take a single argument which is the file path for a file or directory, so a location, it's going to take whatever is in that location and copy it to a predetermined location, that we're going to hard program essentially. Let's get started.

In the chapter three directory, on the Raspberry Pi, once again I'm going to create a new file and I'm going to call this Achi short for archive I want to call it arc, because there's already command called arc, and that has to do with the-the architecture of the processor that we use, so I'm going to call it Archie if I can spell it correctly. There we go. So there's a new empty script and of course once again the #! line and we'll leave a comment to tell us what the script is doing, "Archive the input file to a predetermined location". Ok, so this is going to make use of a couple of things we've already seen; arguments and variables. So we can set up a variable for where we want this file, this archive to be maintained, so what we can do is just put in the comment to tell us what it is the archive location and I'm going to create a variable called archive if that makes sense. Now I can set this variable to a file path, just by entering a file path, so here I'm going to enter /home/pi/archives, that means in our home directory we're going to have an archive directory, that's fine. Note that I have used the absolute references the absolute file path, this is really important to do with this kind of script especially and we'll come to that later but just just know that there's a reason why we're using the absolute path; I wouldn't use the tilde character for instance, that that's not going to work for us down the track. Now we've got the archive location that we've just set as kind of like a user programmed variable kind of like hard-coded now what we can do is make the destination if it doesn't already exist so we're going to make the archive and we do that, of course, with our make directory and we can access the variable archive, now this would normally work with of course if we already have the directories where we don't want to throw any errors or have any funny business so we can use an option to make directory which is -p and that is going to, well first of all if our archive if we specify a very long file path with lots of folders that don't actually exist yet it will create these parent directories along the way until it gets to the end one so that that p will make the entire file path and create all directories if they don't yet exist but it will also elegantly handle if the directory already exists so we can run that every time and it'll only create the directory on the first run.

That's alright now we can copy the input file, so remember we're taking in an argument, which is a location, we're going to copy the input file and this uses a command called CP for copy, so we're going to copy, we're going to access the first argument, and we're going to put it in archive. So that is the syntax for the copy command it copies from and to so we're copying from whatever we input and we're copying it to the archive, of course, we are accessing the variable archive that we created up here. So as we change this it'll change the behavior of the script which is nice and convenient now there's one other thing again, another option that we have to include which is the -r, and that stands for recursive, so copy is usually reserved for copying files but if we put in a -r as an option this recursive copy looks inside subdirectories and copies their contents then copies the parent directory so if that doesn't make sense just bear in mind that that -r is for copying everything inside a directory so in a directory and subdirectories.

Now this is fine just as it is, but we can include a bit of a safety mechanism perhaps, which is actually checking if an argument was even entered, we could run our archive script Achi and without an input it would run and probably some errors would happen, so we want it to handle this gracefully, and we can do that by checking that we've got exactly one argument passed in. So I'm going to do that right at the top here because that should happen first and I'm going to put in a comment saying check for exactly one argument look at me is the same logic that we did in the previous section we're going to say If and we-we can access that number of arguments, which is $# and then we can say is not equal to 1, so this is true if we ever receive a number of arguments that is not one which means there's an error so we can say then echo and we can kind of prompt the user with a helpful bit of feedback on how to use the-the program so we can say echo usage and we can access the-the name of the command which is the dollar sign Zero or the 0th argument, and we can say usage that argument and file. So this is going to prompt the user or give them a hint as to how to use the program correctly and then-then we're going to do something called exiting. So this is going to exit our program, and the one is going to indicate an error and I'll just come back to I'll finish the if statement, so this if statement is checking if it's received the correct number of or rather checking if it's not received the correct number of arguments if it hadn't received the correct number of arguments in issues an exit one now exit one means an error or in fact exit anything other than zero means an error exit 0 is the only condition that feeds back to the shell and says "Hey, yeah this program ran just fine", this is going to exit the program immediately and not run any of our other code which would be maybe a problem because we didn't actually have the right number of arguments. So this should be ready to run now so I'm going to save this with CTRL+S, and I'm already in my chapter 3 directory so I can just say chmod u+x and the script is called achi, so ls -l and everything looks in order there so I'm going to just bring this over and I'm going to open another window, so here's my home directory and when I run this achi script we should see the archive directory being created but I have to actually tell the script to what to back up so I'm just going to choose the-the chapter 2 directory here which has a few Python scripts from chapter 2. So yeah I'd like to back that up, let's use the achi script to back that up so that is going to be ./ because we're already in the chapter 3 directory, achi and I'm going to back up the chapter 2, so the way I can do that is to say /home/pi/chapter 2/ so we're backing up the entire directory and whatever was inside it. Now when I strike enter we've just seen this archive directory appear and if we go inside it we've got a copy of chapter 2 and inside that are all the scripts that we are interested in backing up so that is a very basic way to automate otherwise maybe a bit more of a tedious task like archiving. We're going to expand this in the next video so we're going to create a copy and just expand its functionality to make it a little bit more interesting.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.