📺 Develpreneur YouTube Episode

Video + transcript

Database Dump With Python

2023-10-31 •Youtube

Detailed Notes

We continue the series of tutorials as we build out a backup system that drops archive files into Amazon's S3 and related storage. This tutorial focuses on getting a database backup using a Python script.

You can learn more through our online classes at https://school.develpreneur.com and register for free. Registration will add you to our email list, and you will periodically receive coupons for courses and notifications of the latest releases.

Transcript Text
[Music]
well hello and welcome back we are
continuing with our little um backup
anator orever we're going to call it our
little backup script and we have uh
currently we we worked our way up to if
you look couple of the previous episodes
uh we did a couple things where we were
like we're storing something off and uh
we've done storing and retrieving
creating a bucket in Amazon's S3 we've
done zipping up files this episode I
want to focus on using python to back up
a
database now we're going to do the uh
this is for my SQL and this is going to
be the brw force kind of approach and
basically what we're going to do is we
are going to create a little function
I'm just going to call it database dump
and we're going to use uh from
subprocess we're going to use P open and
all we're going to do is we're going to
actually use our normal uh dump type of
command so normally if you don't know
you can normally dump dump a database by
doing
MySQL I'm sorry my SQL dump and then
you're going to do dasu and it's going
to be username and then there's going to
be- p means it's going to ask you for a
password and then you tell it the
database name so it' be DB name and if
you normally what's going to do it's
just going to kick out the SQL so what
you probably want to do is send it to
redirect it to somewhere like backup.
SQL so if I do this it's going to say
enter your
password this is not anything real so
it's going to say hey it doesn't exist
and so this is something you may run
into because when we're running our
script we're not going to get that
ability to ask for a password instead we
need to give it one and so a way we do
that is we create a thing called
my.cnf and we're going to put that I'm
sorry
my.cnf and we're going to put it in our
home folder and all that is is a text
file and it's going to
look like this is we're going to have a
section and it's going to call it's
called MySQL dump because we're going to
say hey when we call MySQL dump for
MySQL and this will work for Maria D
Maria DB as well well then we're going
to give it the username and the password
that we want to use in order to do that
now here um and so we've got a dash user
I think we can actually get rid of this
completely let me check that because I
have this entry built for my database
user in my uh
my.cnf I think we can do it without that
so if I do my SQL
dump uh let's just run it uh
because there's not a whole lot to
whoops there's not a whole lot to this
script and especially if I spell python
right and don't add a typo in there um
all we do is we're going to come in and
we're going to give it a database name
and we're going to give it an output
file so if you come up here database
name and output file and then here I'm
just throwing those in so my database
name and I'm going to go out to my
output file and notice that it's uh so
it'll be in the current folder I could
put more in into this and uh I've just
giving it a whole thing so I've got this
or SQL extension I've got this data base
so now if I run it let's see if I do it
without a
user cool and so if I do go look here uh
let me actually
remove what did I call it shorty. whoops
shorty. SQL let me run that again so I
don't even need to give it a user there
we go and if I
do wow if I catch shorty. SQL then here
is my database and you see it's like a
big old dump and so it starts with whole
bunch of checks and stuff like that
what's database type and then for each
table it's going to drop it if exist and
it's going to create the table and then
it's going to enter all the data so and
it's very straightforward so if I wanted
to replace this I could go into my
SQL and I could do use
shorty and if I do Source uh what I call
shorty. SQL then it's going to run
through and now I've just replaced my
database now this is the reason we're
doing that we've got two things that we
have values value in doing this now one
of
them is for this is for this specific uh
script now one of the things you may be
thinking is like well hey what if and
this is a a business use that's got me
thinking about this what what if I've
got an app and I've got it out on a
server somewhere and I want to grab I
want to have the ability to allow
somebody to grab a backup of the
database and do something with it well
in Jango we can actually make that
pretty straight forward we've seen how
we can do it in our scripts so we can
just in a python script do something
this simple it's really you know you can
set this up in a couple of minutes and
boom you have a database backup now if I
want to do it in Jango what I would do
is in the URLs I'm going to create a URL
like for example dump and then I'm going
to do I'm GNA give it an ID so I'm gonna
have a URL that looks something like
this actually it's going to be in well
it's actually not be slash up but you
know basically whatever
my path is and I'm going to give it an
ID because what I want to do is within
the and let me just keep that there for
now so then this is what it's going to
look like in the The View code is I'm
going to come in from the request I'm
going to take that ID I want to make
sure not anybody can do it so this is
like the you know the simple very easy
way to secure it and and that secures in
quotes but it's basically it says hey if
they don't give me the right ID then I'm
not going to be able to I'm not going to
do anything I'm just going to send them
back to whatever the homepage is which
is this I'm going to redirect otherwise
I'm going to come in and basically the
same thing that we've got here this
little uh DB dump of change it just a
little bit now in this case I'm going to
have a hard code I'm going to have
within my code my database name and my
output file um I could pass those as
parameters if I wanted to and then what
I really need to do now since I don't
need this is I'm going to do a MySQL
dump and then it's the same thing here
it's just the DB name the out file now
in this case what I'm going to do is I'm
going to turn right
around I'm going to open that file I
just create that SQL file I just created
created and then I'm going to send it
back to them so it's going to end up
downloading the SQL
file that we just told it to it so it's
going to download the backup the whole
database STP now
obviously this could be a problem at
times because it could be a huge file uh
it's also you know obviously it's a a
security risk uh the fact that you have
in your website a way to do that but you
can lock that down various different
ways uh you can use a password you can
have it so they have to be logged in
that they have to be you know at an
admin or something like that a lot of
different ways you can do it so I'm
giving you a shotgun be careful that you
don't blow your you know your toes off
or something like that now this is going
to be very useful for us with our backup
piece because we're going to have two
things we're going to be looking at when
we're doing the backup we want to you
know we'll have like step
one is back up and store the
database and then we're going to have a
step
two which is going to be back up the
files and store uh let's actually
say zip and store them and so our next
uh in the next session that's one of the
things we're going to talk about is like
all right now that we've we've got those
pieces together we have in our backup uh
we've got a file but what we're going to
do is we're going to add something to
scroll through a directory pull all
those files put them into a zip file uh
which is what we looked at before is
basically zipping that file so there's
one file one one big zip file that would
be necessarily all of our backup folders
and the files within them we're going to
take that we're going to shove that out
and we're also going to have the ability
to do a a database if we want to or
actually we could technically do
multiple databases we can even do
multiple databases zip those up and have
a single file that we're going to store
out in S3 so we're going to talk about
that next time there's several ways we
can go with this right now I just wanted
to make sure that we're good with
database dump dumping your database from
python is super super simple and
actually some of this like the database
name once you've got
yourm
my.cnf file built you could actually
pull this database from the settings in
um in D Jango that setting the Jango
settings file you know we use that
database all over the place so you would
have you could easily set this up so
that it never actually has like I do
here uh it never actually uses database
name it instead would use the one uh
whatever the settings database name is
so and you could also do that so it
could flip from uh like settings to set
up so you could do it from production or
database or or development or testing or
whichever uh configuration you're
running
from keep this one sort of short there
you go that is how you dump your mySQL
database or Maria dat DB database out to
a file so that you can you know copy
that somewhere else back it up send it
to a buddy whatever it is you want to do
go out there and have yourself a great
day a great week and we will talk to you
next time well hello this is Rob from
develop andur building better developers
and we have a mastermind class that will
be starting or actually several
Mastermind classes starting in 2024
check us out at Tech technology
Mastermind
2024 we've got all the information you
need including some pricing early bird
discounts up to 50% off but those are
things that you're going to have to act
soon there is an application process so
go ahead and sign up today thanks a
[Music]
lot
Transcript Segments
0

[Music]

18.72

well hello and welcome back we are

21.88

continuing with our little um backup

24.16

anator orever we're going to call it our

25.8

little backup script and we have uh

28.76

currently we we worked our way up to if

30.92

you look couple of the previous episodes

33.719

uh we did a couple things where we were

35

like we're storing something off and uh

37.44

we've done storing and retrieving

39.44

creating a bucket in Amazon's S3 we've

42.399

done zipping up files this episode I

45.039

want to focus on using python to back up

48.399

a

49.559

database now we're going to do the uh

54.28

this is for my SQL and this is going to

55.96

be the brw force kind of approach and

58.719

basically what we're going to do is we

60.559

are going to create a little function

62.64

I'm just going to call it database dump

64.96

and we're going to use uh from

66.36

subprocess we're going to use P open and

69.28

all we're going to do is we're going to

70.24

actually use our normal uh dump type of

73.4

command so normally if you don't know

75.439

you can normally dump dump a database by

77.52

doing

78.479

MySQL I'm sorry my SQL dump and then

83.36

you're going to do dasu and it's going

84.799

to be username and then there's going to

86.64

be- p means it's going to ask you for a

88.479

password and then you tell it the

90.32

database name so it' be DB name and if

93.439

you normally what's going to do it's

94.84

just going to kick out the SQL so what

96.159

you probably want to do is send it to

98.84

redirect it to somewhere like backup.

100.84

SQL so if I do this it's going to say

102.28

enter your

103.52

password this is not anything real so

105.68

it's going to say hey it doesn't exist

108.2

and so this is something you may run

110.64

into because when we're running our

112.96

script we're not going to get that

115.2

ability to ask for a password instead we

119.039

need to give it one and so a way we do

122.079

that is we create a thing called

126.759

my.cnf and we're going to put that I'm

128.72

sorry

130

my.cnf and we're going to put it in our

131.92

home folder and all that is is a text

135.319

file and it's going to

137.959

look like this is we're going to have a

140.239

section and it's going to call it's

141.92

called MySQL dump because we're going to

143.72

say hey when we call MySQL dump for

146.36

MySQL and this will work for Maria D

148.64

Maria DB as well well then we're going

151.4

to give it the username and the password

154.04

that we want to use in order to do that

157.48

now here um and so we've got a dash user

160.72

I think we can actually get rid of this

162.319

completely let me check that because I

163.92

have this entry built for my database

167.08

user in my uh

171.28

my.cnf I think we can do it without that

173.92

so if I do my SQL

176.2

dump uh let's just run it uh

180.72

because there's not a whole lot to

186.36

whoops there's not a whole lot to this

188.48

script and especially if I spell python

190.84

right and don't add a typo in there um

193.319

all we do is we're going to come in and

194.64

we're going to give it a database name

196.239

and we're going to give it an output

197.519

file so if you come up here database

199

name and output file and then here I'm

201.76

just throwing those in so my database

204.08

name and I'm going to go out to my

205.08

output file and notice that it's uh so

207.159

it'll be in the current folder I could

209

put more in into this and uh I've just

211.879

giving it a whole thing so I've got this

213.64

or SQL extension I've got this data base

218.48

so now if I run it let's see if I do it

220.439

without a

221.519

user cool and so if I do go look here uh

225.64

let me actually

227.28

remove what did I call it shorty. whoops

230.439

shorty. SQL let me run that again so I

233.599

don't even need to give it a user there

235.84

we go and if I

238.319

do wow if I catch shorty. SQL then here

243.519

is my database and you see it's like a

245.28

big old dump and so it starts with whole

247.959

bunch of checks and stuff like that

249.36

what's database type and then for each

251.28

table it's going to drop it if exist and

253.12

it's going to create the table and then

254.439

it's going to enter all the data so and

256.72

it's very straightforward so if I wanted

259.519

to replace this I could go into my

267.12

SQL and I could do use

271.24

shorty and if I do Source uh what I call

275.8

shorty. SQL then it's going to run

277.88

through and now I've just replaced my

279.56

database now this is the reason we're

281.96

doing that we've got two things that we

283.52

have values value in doing this now one

286.52

of

287.68

them is for this is for this specific uh

291.16

script now one of the things you may be

293

thinking is like well hey what if and

295.72

this is a a business use that's got me

297.759

thinking about this what what if I've

299.96

got an app and I've got it out on a

302.639

server somewhere and I want to grab I

305.479

want to have the ability to allow

306.84

somebody to grab a backup of the

309.96

database and do something with it well

312.32

in Jango we can actually make that

314.919

pretty straight forward we've seen how

316.24

we can do it in our scripts so we can

317.6

just in a python script do something

320.6

this simple it's really you know you can

322.68

set this up in a couple of minutes and

324.36

boom you have a database backup now if I

327.68

want to do it in Jango what I would do

330.8

is in the URLs I'm going to create a URL

333.08

like for example dump and then I'm going

335.16

to do I'm GNA give it an ID so I'm gonna

337.36

have a URL that looks something like

340.08

this actually it's going to be in well

343.319

it's actually not be slash up but you

344.8

know basically whatever

346.36

my path is and I'm going to give it an

348.72

ID because what I want to do is within

351.28

the and let me just keep that there for

354.24

now so then this is what it's going to

355.639

look like in the The View code is I'm

359.24

going to come in from the request I'm

360.24

going to take that ID I want to make

361.96

sure not anybody can do it so this is

364.52

like the you know the simple very easy

368.16

way to secure it and and that secures in

372.24

quotes but it's basically it says hey if

373.72

they don't give me the right ID then I'm

375.08

not going to be able to I'm not going to

376.36

do anything I'm just going to send them

378.16

back to whatever the homepage is which

379.759

is this I'm going to redirect otherwise

382.56

I'm going to come in and basically the

385.199

same thing that we've got here this

386.72

little uh DB dump of change it just a

390.319

little bit now in this case I'm going to

393.479

have a hard code I'm going to have

394.68

within my code my database name and my

396.599

output file um I could pass those as

399.52

parameters if I wanted to and then what

402.68

I really need to do now since I don't

404

need this is I'm going to do a MySQL

406.16

dump and then it's the same thing here

408.28

it's just the DB name the out file now

411.16

in this case what I'm going to do is I'm

413.639

going to turn right

415.16

around I'm going to open that file I

417.599

just create that SQL file I just created

419.4

created and then I'm going to send it

421.319

back to them so it's going to end up

422.879

downloading the SQL

425.56

file that we just told it to it so it's

428.12

going to download the backup the whole

430.24

database STP now

432.039

obviously this could be a problem at

434.16

times because it could be a huge file uh

436.919

it's also you know obviously it's a a

439.08

security risk uh the fact that you have

441.879

in your website a way to do that but you

444.96

can lock that down various different

447.639

ways uh you can use a password you can

450.24

have it so they have to be logged in

452.16

that they have to be you know at an

453.639

admin or something like that a lot of

455.72

different ways you can do it so I'm

458.199

giving you a shotgun be careful that you

459.8

don't blow your you know your toes off

461.68

or something like that now this is going

464.199

to be very useful for us with our backup

467.319

piece because we're going to have two

469.199

things we're going to be looking at when

470.96

we're doing the backup we want to you

473.919

know we'll have like step

477.56

one is back up and store the

482.84

database and then we're going to have a

484.599

step

486.08

two which is going to be back up the

490.68

files and store uh let's actually

494.36

say zip and store them and so our next

499.039

uh in the next session that's one of the

500.44

things we're going to talk about is like

501.599

all right now that we've we've got those

503.919

pieces together we have in our backup uh

508.84

we've got a file but what we're going to

510.96

do is we're going to add something to

513.76

scroll through a directory pull all

515.839

those files put them into a zip file uh

518.959

which is what we looked at before is

520.32

basically zipping that file so there's

522.08

one file one one big zip file that would

524.68

be necessarily all of our backup folders

527.48

and the files within them we're going to

529.24

take that we're going to shove that out

530.72

and we're also going to have the ability

531.88

to do a a database if we want to or

534.64

actually we could technically do

535.839

multiple databases we can even do

537.72

multiple databases zip those up and have

540.519

a single file that we're going to store

542.24

out in S3 so we're going to talk about

543.92

that next time there's several ways we

545.76

can go with this right now I just wanted

547.64

to make sure that we're good with

549.36

database dump dumping your database from

552

python is super super simple and

554.64

actually some of this like the database

556.399

name once you've got

558.2

yourm

559.92

my.cnf file built you could actually

563.32

pull this database from the settings in

567.36

um in D Jango that setting the Jango

570

settings file you know we use that

571.76

database all over the place so you would

573.32

have you could easily set this up so

575.24

that it never actually has like I do

578.16

here uh it never actually uses database

580.64

name it instead would use the one uh

582.68

whatever the settings database name is

584.88

so and you could also do that so it

586.2

could flip from uh like settings to set

588.88

up so you could do it from production or

590.44

database or or development or testing or

593.56

whichever uh configuration you're

595.68

running

596.6

from keep this one sort of short there

599.399

you go that is how you dump your mySQL

602.04

database or Maria dat DB database out to

605.76

a file so that you can you know copy

607.92

that somewhere else back it up send it

609.959

to a buddy whatever it is you want to do

613

go out there and have yourself a great

614.24

day a great week and we will talk to you

617.48

next time well hello this is Rob from

620

develop andur building better developers

622.04

and we have a mastermind class that will

624.16

be starting or actually several

625.519

Mastermind classes starting in 2024

628.12

check us out at Tech technology

629.48

Mastermind

630.839

2024 we've got all the information you

632.92

need including some pricing early bird

635.079

discounts up to 50% off but those are

637.76

things that you're going to have to act

639.36

soon there is an application process so

642.32

go ahead and sign up today thanks a

646.66

[Music]

658.04

lot