 I thought of this really cool name remote control wordpress that I was trying to envision in my head what I could do to kind of visually show that and I thought like a really cool race car with like a wordpress logo on the front would be cool. I started to look at stock art and I said oh I'm going to buy that. I could pay money for that so but the idea is that yeah so I wanted to find a way to use WPC line and do a little bit more automation and here's what we're going to talk about today. Come on in. So I'll give you this like introduction about what it is that I'm talking about so that this kind of makes a little bit more sense. There is a lot of information so if there's something that doesn't make a whole lot of sense just stop me and say actually can you help me understand. So really the talk is based around WPC li local development using wordpress to have a remote connection into actually another server so that you can execute commands so using these aliases which make it easier to to have a remote connection. I'll look at some database commands and we'll finally get a bunch of scripts together and look at the complete script that actually does something functional. So my name is Edmund as you guys know I work at WP Engine. It's a digital experience platform for wordpress so we have lots of different ways to host wordpress whether it's shared or it's dedicated or enterprise so different configurations. I normally have a slide here that shows this is who I am this is what I do I figured I'd take it one step further and actually go into pictures because that's that's more visual. So this is something I really like is cycling and we took a trip to Amsterdam this is the WP Engine London office and what we were actually what we realized after this picture was taken the woman who took the picture was laughing at us because this animal is apparently not so friendly so I love snowboarding so this is a picture of me in the Alps and I also love music production so anytime I get I will be in the studio making some sorts of sounds and when I'm not doing all that I'm with these guys who are my kids they're going really fast on their vespas as you can tell all right so yeah so a sales engineer is really somebody who has to find solutions so I look and and see a problem and I have to find a way to overcome that and really define what the solution is and then find a way to get there and a lot of times that means thinking creatively and by that I mean it's maybe not the solution may not be within the application or it may not be within the platform that I'm using so there are times when I'll have to say actually I can't do that with this platform at all and I have to look at something else whether it's a third party or it's it's something completely different so the problem that I wanted to solve here was how do I take my database on a production website and copy down to my local environment and it's it's not a difficult problem to solve there's many ways to do this but I wanted to do this with WPCI and I wanted to do it in a way that was repeatable and in something that I could really script out do it once and then run it on a regular basis so the solution involves of course developing locally with WordPress creating aliases to access a WordPress site remotely using SSH to connect to these remotes and also using WPCI commands to actually get the database and do some database interactions I think people here are developers that's a good split and do you guys also work locally on your your development or is it all on servers so a good good amount so there's a couple of ways that you can work locally I've always well so I've actually done all of these so and in the beginning the easiest way to to do development was with map but that's kind of what made me say actually I can do this because it was easy to install it was an application you install it and you've got this whole server you can use virtual machines docker containers are really popular now as well if you're a complete maverick you could build a local server and risk your entire development environment changing as you update your OS and actually there's a lot of solutions from a host now so hosts are more commonly bringing a product that will do local development and actually bring the local environment up to their their hosting environment so I use dvb as my local development tool and one of the reasons why I like it is because it has a lot more things than just just a development environment so lots of different tools so uses virtual machine this virtual box as a virtual machine so it essentially is a a completely different system running on my laptop that uses a tool called vagrant for configuration and that actually does some provisioning and automation so one thing that that really struck me when I looked at this slide is like so vvb does anyone know what it stands for yeah it's on slide so very vagrant vagrants but you've got vvb you've got virtual box you've got vagrant so i wonder from onto something but we might never know so um you know virtual box is a hypervisor so essentially it's a it's a virtual machine manager it it takes a slice of your computer and it says i can actually run another system completely on there so you have the concept that so this laptop is my host and then a virtual machine that runs there would be a guest machine um you can have multiple machines so it's not just one i can have multiple operating systems it could be windows windows linux mac it can be something that's configured so you can get a distribution that's an operating system or something that's that's very specific to an application and you're also able to save state so you can terminate it you can stop it you can save it reboot when you're not using it so fairly uh fairly easy to to go back to where you started which which can be really really interesting and useful so that the hypervisor looks somewhat like this so if you imagine this is the operating system that you're running on the hypervisor is a layer a thin layer on top that does all of the allocation for memory and resources for virtual instances so on top of that hypervisor we can have these guest virtual machines and that's really what is what um what dvd is doing so i've got my mac os and it runs ubuntu on top of that so vagrant comes in as a tool that actually automates and builds out the system to our specifications so in the case of vvd it's actually doing some installation uh it uses a a base box so it's got a virtual machine that it starts with and that it it installs and configures on top of that so it does things to the virtual machine as well as the local machine in order to make that work wpcli of course uh command line tool allows you to do things in wordpress that you would have to actually click around for you'd have to go to dashboard and actually do operations with a mouse so uh allows you to also automate things that are repetitive so if you want to do something over and over again you just have to have a script that does that you can loop through a script and in that you can bundle commands together and you can save them run them at any time and the base of the system is bash so i don't think that we really talk too much about bash it's something that as a developer i used a little bit and it's been kind of like a utility but i never would say that i'm a bash expert and i think it's a it's a tool but it is uh it's really interesting to learn you can do a lot more with it than i probably do really it's a shell scripting language it's almost 30 years old so it's really common you'll find that on you know lots of different systems it can read command from me typing into a terminal or can actually read a command from a file and that's where we get into our scripting and then there is uh there's some simple things that you can do but there's also control structures so you can do if then statements and variables lots more but for the scope of this presentation we'll keep it to those two and you know throughout the presentation you'll see links here so i've always tried to cite information if you want to learn more about it so i said bdb is great because it's got a lot of stuff and this is some of the stuff that it has so it does the server it installs the local server it has database with php my admin for management and also some other tools including wp cli so this is really great starting point for development i don't have to really go and install other things configuration is is almost finished for me you can add additional WordPress sites quite easily and so this all runs on top of ubuntu there this is the virtual machine this is the base box that that's that's used by bbb and provisioned to be to have all of the software on it so ssh and configuration because we have this virtual machine it's essentially another system running on my laptop so there's no way for me to actually access it without using a remote connection it sounds kind of strange that you'd have to remote into something that's it's literally running on the same system but they're almost entirely separate so that's where ssh comes in so you'll have to use that to get a remote connection to run any shell scripts or any sort of commands using wp cli so ssh it can be secured by public private key authentication so i've got a key that's on my local system a private key which is matched to a public key which is on the remote system the benefit is so those keys are checked to make sure that they match and the benefit is that the the password is a limited thing so you won't have to enter a password which is really handy for scripting so this can be just run by an automated process if you need to connect and run a script so it looks something like this so this is to illustrate that you've got this client private key and a remote public key that gets checked and some additional information there if you want to dig deeper runs on ssh runs on port 22 and a typical ssh command would be to use ssh the username at hostname once you run that you can add options to the end and usually what i would do is point to the location of my private key so that dash i option would be at the end of this command and sometimes if you're connecting to a remote for the first time you'll get a message that says do you want to add this key to a list of hosts you say yes and that never happens again and actually saves to your known hosts file and it will make it easier to connect to in the future so i've given an example of a basic configuration file so we can make it easier to connect to ssh by using a configuration file and what i would typically do is take the hostname username and then the path to the the private key this is probably the minimum that you would need to to actually set this up but whatever i use as your name so let's say i called it edmund i would just say ssh edmund and that would go directly to don't connect directly directly to the remote server so as part of that configuration of what vvd does it uses vagrant vagrant has a whole set of commands and vagrant has an ssh configuration command which will actually just generate this simple you don't have to mess around with anything i would always maybe change the host to something that makes sense so again i would go host edmund or production or dev whatever you're you're using it for and then i take this and copy it into my ssh configuration file so this is something that you can save in one place a group of different connections and that makes it much easier one thing to note and i know this is potentially hard to see but this port can change so it is very possible and i spent a bunch of time trying to figure out why my connections didn't work when i would terminate and then reboot the virtual machine and the port changes so that's that's what i found hopefully that saves some time for all of you cool how many people are using wpc like all right yes you're in the right place what about aliases yes okay cool so typical command line interface you essentially just entering your commands here on on the command line it's not very beautiful and a lot of people will be scared of this but they're just words we can all read through them it's it's not really too difficult if you take some time to just focus on it so i think that aliases are you know just judging from count of hands there's there's only a few of you who actually have used them it's a little known feature and it's it's something that is pretty simple to set up which can save you a lot of time it's essentially just setting a shortcut to a server so i don't have to go through that whole ssh connection to do something i can just specify that i want to run a command on a remote it works with a very small configuration file it's a yaml file and one of the cool things is you can actually group together these remotes so if you imagine instead of running a command in the terminal on one word press instance i could run it on many instances i'll show you how that works because i think that's that's really cool so you can set aliases globally or they can be on a site by site basis so it could be at the server level or it could be literally for each site has different aliases and that makes a lot of sense because you probably would have different production for for each site yeah and then you know clearly that the benefit is time saved so i don't have to really log in sorry connect with ssh change directories run a command then exit and then do whatever else i was going to do so these remotes they they get set up with aliases to look like this so this would be my production site i've got the ssh connection that we specified in ssh config and i've got the path here for my dev environment and that's something that you'll need for vvv and grouping the aliases together very simply it can be done like that and if i ran a command on both that would actually run on both amazing um simple commands this is how you would use an alias so that the normal command that the structure for wpcli is wp command and sub-command so in this case we're doing the at dev um alias and that will run that command on the dev server so it usually has vvv and then production you might get totally different results depending on on what's installed there um so as this problem that we're trying to solve involves database commands we'll talk about a couple of database commands but actually not too many one of the really important things to know is that you can always get help so the help command takes a sub-command and will give you a nice display of all the things that you can actually do with that wpcli command and for db there's a couple of really useful things you can optimize you can look at the tables prefix actually use some repairs but in this case we're going to look at export and import so let's take a bunch of commands and tie them all together so i've started to take wpcli and mix it with a bit of shell scripting and this is not going to be too involved but the the idea here is that you can add some logic into your wpcli commands so in this case i'm going to my production server i'm looking to see if hello dally is installed so it's plugin is installed hello bash has a variable this question mark variable and essentially what that does is it will give me the value for the last connect right and in this case we want to check if the plugin is installed and if it's not installed go ahead install it so all this is doing here it's it's quite simply an if then statement so if the if the result is not equal to zero then install plugin because everyone wants to install hello dally right and then so what i've done is just to take the comments out just reduce it show that it's it's really simple and clear and actually for any bash script you're going to need this thing at the top called the shebang it's it's really just tells where this this file should be wrong what application should run it so we talked about variables and control structures i think you've seen the if thens the variables can be created very simply so i just say name equals edmund give it a string and then there's an echo command which actually will print that variable out to screen so echo name would print out edmund so it gets deeper we can take the output of a command and save that as a variable as well so i put the command in these brackets and i'm actually just saving the blog name as blog name and then again echoing it and then printing out the blog name so in our case i'll need to get the site URL from production and i'll need to replace that with the dev site URL so i use option get site URL and save that into a variable for both production and development so this is really the the coolest part i think and took a little while for me to figure this out even though it's it's pretty straightforward i'm going into production i'm exporting the database but the other stuff is a little bit tricky so that that hyphen it actually takes the the entire database the sequel dump and prints it out to the screen which doesn't really help you that much at all it's going to be a long running screen of sequel statements um the greater than symbol actually takes that output redirects it to a file and it's it's not a file on this production environment since i'm actually running it from a different uh system it's going to save it locally where i ran the command i mean that's brilliant so i don't have to go and download the file and then process it or do you know whatever else it's there already so my next step is to actually import that into my dev environment and very straightforward so i'm doing dev dv import and i'm giving it the path to the final there's a search and replace function this is is actually a totally different thing this is not part of the the database command but that's essentially what it does it will search for one string and replace with the other so got my production site url replacing it with the dev site url and what would what would coding be without some housekeeping so we uh we take the file that we downloaded and remove it simply the rm command get rid of it our last step so in order to run a script we need to make it executable and in this case we'll use a command called schmod and that essentially will go and add this executable permission to our file so we are get dv dot shell file we'll actually be able to be executed by typing dot slash get dv so put that all together for you guys it is here this is what it looks like in its form just like couple of lines that can be run to take your production database and a bit to your development this could be done maybe weekly i i don't know what best practices are for you guys i would say probably weekly makes a lot of sense because things can change on uh you know assuming that all of the work is being done on a staging server or another server and then push to production so you want to get the most current version of your production content i'll save that here on github as well so you can take a look download that and in conclusion so you can use aliases and wpc li with vvv it makes for a really great system i think that it is pretty straightforward and can make things a lot quicker and scripting can make things uh repeatable and make life a little bit easier by you to do things quickly with some pre planning of course i've just put a blog post on torque magazine so it is a two part series about what i've talked about today the first part is live and this is talking about aliases the second part goes into the script with the database export which i talked about that will be live tomorrowish and i've also added some links some reference uh essentially uh you know there's a lot a lot of information here so if you want to learn more about ssh that's here i've added an alternative to uh you know maybe you don't want to use wpc li for this at all because it actually is a process that other things will do so wp migrate is available you can do this in a plugin that'd be a lot simpler there's a reference for wpc li commands i've linked to a script tutorial so if you want to learn further about bash scripting you can go ahead and do that there and also you know of course you don't have to use bbb you can use another approach maybe forward looking would be to to check out uh wp local docker um so that's that's really what i came here to talk about i hope you guys enjoyed it and i've got some slides available on slideshare and you can reach out um you can get me at admin dot terminic at wpengine.com and on twitter at spice cadet any questions friend as well as capturing the whole thing the whole workplace is so copying the the files as well i would i would do it in a different way yeah so i would um i would probably be using github or or git repository and i would sync that way to just deploy the changes to to both when you fall down from production to the staging of development um you can concern about any like private data like users data that's inside the production database it might come down with it depends yeah yeah and then there's definitely so in the case of like word commerce if you're taking transactions yeah so you like cleanup before you put it on i haven't had to but i imagine that you would want to if it was sensitive information um yeah and some people will not want to use production in in a staging environment so it's perfectly valid but done much smaller stuff then yeah i was going to ask around that can you be selective about what tables you ignore what you do there i bet you could yeah so i think you could export and exclude certain tables yeah so and and there's you know you can use you know you can write custom queries and do that with wpcli so um and then you know there's other tools that will allow you to select so i i know for wpn's and there's a way to use our legacy staging which will allow you to get certain tables and not other ones so but but that doesn't really help you to get it to the local environment pass commands in with uh wpcli or if you if you do if you do it fairly often enough you can use your yaml files to specify what tables to look into and what i can think of anybody else well thank you again