The full Raspberry Pi Workshop in step-by-step format can be found here http://coreelec.io/piworkshop In this video we explore how you can add certain commands to the backup script from the previous video to make it far more efficient

Transcript

Okay so now we're ready to modify our archive script on our Raspberry Pi and we're going to add a feature to it, we're going to modify Achi to create new directories with today's date on them when it's creating copies. This way rather than having all of our copies being thrown into one big directory it'll be filled with lots of sub directories that are titled with the day's date they were created, they will be nice and convenient for creating a history of archives. So we're obviously going to need to access date information to do this and we can do this with the command date and we can see that we get a whole bunch of information about the date and the time and the time zone but what we really want to do is look at the manual for date, to see how it works and get it to return exactly the information that we want that is the year the month and the day formatted nicely so we can just grab that information and create a directory with it. So this will be the useful skill if you're researching commands for other scripts that you're writing it's a good way to see how the command can be used properly and see what its features are so it can do a lot of the heavy lifting for you just like this will.

So to access the manual for a command we can type in man (for manual) and then the command itself and here we are in the manual, so we can see up the top here we have the synopsis and this is how the command can be used very briefly so we have date we've got some options or we've got some format whatever that is now these are in square brackets and that means that you don't have to enter anything into them and in fact we didn't enter anything before we just entered date and ran it. But we do have the option to enter some options or to enter whatever format is so I mean option that that's what these letters followed by a dash are and they can be quite useful as we've seen for other commands like copy but this format is kind of giving us a hint that's how you can format the output. So let's scroll down to where that starts, and indeed yes format controls the output so we can control how the output of date is returned by entering some code into the format section and we do that by entering what looks like a percent symbol, so we want, let's say the year, then a dash, the month, and a dash, then the day, and that's what we're going to call our archive directories. We do the year first so that if you sort by name everything still winds up in the right order. So I mean it makes intuitive sense that year is down here with y, so we have %Y for year then of course month should be M but we can see that we use a lowercase M for month it gives uppercase is a minute and then day is D and we have a lowercase d for day. So why lowercase M lowercase D? Backup the top either we see that format needs to be preceded by a plus symbol, so let's give this a go, we can type in date and then we can enter the format which was plus y dash sorry not why it was + % y dash % month which is lower case M dash percent lower case D for day and that has returned exactly what we would want to call our archive directory for this day, so this is this is a command that we're going to run our scripts somewhere when we're creating the directory.

So let's, before we jump into that though when your scripting and you want to do something like if you want to run a command you do it in a special way and I'll show you how. So let's say we create a variable called date and we can do all these things in the shell just like in the script because a script is just shell commands so we can create a variable called date and rather than setting it equal to that command when we want to run a piece of text as a command and give its output something we wrap it in these back tics, the backtick character is next to the one key shared the key with the tilde. So we just enter that same text + % y - % month - % day and then another back tic and this is how we can pass the output of the command date into a variable that we're calling date all in uppercase if I run that and then echo date, we have the same thing. So this is the command that we're going to use in our script.

So let's create the script, rather than create it from scratch I'm going to copy the achi script and just modify it because it's already most of the way there so if we ls, remember we've got this achi script already so I'm going to create a copy of achi and I'm going to call it tachi for time archive, that makes as much sense as anything. So we can see there's tachi and it's copied the permissions as well which is handy we don't have to use the chmod command. Now I'm going to open this up in Nano so that we can modify it and that will give us the nice colour highlighting. So here we are in nano and when we set the archive location is what I'm interested in modifying so I'm just going to put in that command that we just ran before date = date + % y - % month - % day and I made it a typo there I believe that's correct so now I've got a variable called date and what we can do now is append this archives path with whatever comes out of date, out of the variable date, so we could have put that command on the end of the path but I think this is a bit more readable it kind of shows whoever's reading this what it's meant to be. So that would be just about it, except that we have this exit one up here just to get us ready for the next video I'm going to put at the bottom of this file exit 0 to indicate that this script ran just fine with no problems; remember zero being the only exit code that means no error.

Now there's a bit of a technicality here but I'm going to change the copy command, copy is a bit clunky, it literally takes an entire copy of everything and creates the copy wherever you tell it to but what I what I think makes more sense for maybe as this archive directory grows and becomes bigger we can use a program or a command that's a little more elegant which is rsync and this is like a copy but it's more of a synchronizing so it checks to see if it actually needs to copy a file before it does it say if you run the script twice in a day and files haven't changed it won't need to copy them it can also copy over the network so you can use this to copy to a remote directory which is definitely recommended for actually creating archives not really that good idea making an archive on the same machine that you work on, but this is just to teach us a few tricks about scripting.

So we're going to use rsync, we're going to leave these two arguments the same because it works just the same as-as CP it does from and to so that's from the input to that archive directory and we're going to just put in a few more options -avr, so what these mean is they're really just affecting how that script is going to run, of course, recursive we want to copy directories, the v is verbose this means that the command will return lots of information to tell you what it's doing and that's just nice to see how things are going especially if you're copying a lot of files and A I believe keeps the permissions so it keeps permissions for who can execute what etc.

So that should be our completed and modified script, so I'm going to save that and if we ls we can see tachi is there we're just going to bring this in. So here's our home directory and we're going to test the script just like we did before I'm going to run from this directory tachi and I'm going to pass to it /home/pi/chapter 2, so let's run that and see what happens. Okay, we that looks quite positive we've got sending the file list it sends a list of files which are the contents of the chapter 2 directory and we have some status about some speeds now if we go into archives we can see the directory with today's date and of course there's the old directory from the previous section but we can see that within today's date we have chapter two and all the files in Chapter two now of course this is this is the benefit of using rsync instead of copy, if I run the command again we can see that no files were sent it didn't have to send these files because it recognized but all the files that were already there were identical there was no need to create a copy because there were no modifications made and that's one of the reasons why rsync is a bit more elegant than cp. That completes this section on creating a more useful script in the next section we're going to look at how we can automate this script and other scripts.

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.