Topic: Backing Up the Databases

Hi Robert

BACKING UP DATABASES
At my previous host I backed up my MySQL databases in the cPanel under "Files>Backups>Partial Backups>Download a MySQL Database Backup". Using this backup method I could very quickly and simply backup all my database files one right after another... boom .... boom .... boom, etc., very quick and easy. Restoring databases is almost as simple. Just hit the Browse  button right there and upload and restore each database.  I really like this method of backup and restore. No fuss, no hassle.

PROBLEM:
However, when I try to use the above backup method, the download file is only 20 bytes, instead of the several megabytes of the full-size database backup file. I have the same result on all the database files I try to back up. Is there some way to get this backup method working?

rob wrote:

Stephen,
We do have some guides on using the command line to backup and restore MySQL databases, located here:
http://wiki.ocssolutions.com/Importing_ … _Databases

I have not used command line instructions very much, if at all. I am still trying to figure out what this page means. I'm going have to read it more carefully and try to figure out what the instructions mean. For one thing, where do I enter those command line commands?

phpMyAdmin
I have used phpMyAdmin to export and import files to and from my computer, (although I'm not certain which all options/checkboxes I'm supposed to select.) and also to copy the contents of one database to another. (A great way to make a copy of the files to use for the test site). When trying to export a database as a backup, there are some many checkboxes to choose from, I'm not to sure which to choose. I'm still trying to study this out.
I did find this article How to backup your Mysql database with phpMyAdmin. This tutorial shows different checkboxes that are marked in the phpMyAdmin export window than what comes up when I go to the Export window in my phpMyAmin Window. So I'm a little confused as to which checkboxes should be chosen.

I really like my original version of backing up the databases because there are no checkboxes and no special keyboard commands. I really like just being able to click on a link and then start downloading the database files.

PUTTY
I have downloaded and installed puTTY, but am still struggling with it. It seems such a complicated way (that I haven't yet been able to figure out just to do a simple database backup. I'm trying to learn how to use puTTy and I'm trying to follow the instructions in you Wiki. So far I haven't yet got it figured out.

MY PURPOSE
Since I can't get my preferred method of database-backup to work, I'm experimenting with your other two methods that you suggested,  trying out both PuTTy and the phpMyAdmin to find some way that will be relatively easy to back up all my databases each time. Perhaps there's a way to easily or automatically backup that databases that I don't yet know about.

rob wrote:

Downloading and uploading large files can be a pain as well, but using tools like Tar and Zip might make this easier:
http://wiki.ocssolutions.com/Using_Tar_ … e_Websites

I am very familiar with ftp using Filezilla and have used it quite a bit. I have usually used Filezilla to download individual files, archived folders, and the website Full Backup.

Best Wishes,
Stephen

Last edited by StephenW (Dec 24th, 2009 03:28:36 am)

Re: Backing Up the Databases

Hi,
  Just wanted to let you know that I have finally figured out how to export and download the database files using puTTY and Filezilla. Since I haven't use Putty or SSH before, my first attempts were kind of tedious, (like wanting to pull my hair out  big_smile   ) but I'm finally learned enough to at least save my databases. So, although my present method is tedious, I'm feeling much better that I know how to save my databases.

I'm also trying to make the process a little more automatic, such as automatic login for PuTTY.  I've been looking through the articles on the OcsSolutions Wiki, but have also been searching the Internet for answers.
I found this article: Automatic login to ssh with Putty, which sounds promising, but I haven't had time to try it out yet.

If it is possible, I'd also like to learn how to have PuTTy automatically export the databases into the home directory. One thing that has been helpful is that I learned that I can paste the commands into PuTTy (using right-mouse-click). So, I created a file for each database that has all the commands to export it. Then I can just do a copy>paste to enter the backup commands. This is at least a step in the right direction to make it easier.

Just for reference, here is another useful article: Backing Up/Restoring MySQL Databases that I may refer to in the future.

Once I learn how to set up as much as possible to be automatic, this new way of backing up databases might be OK.

Best Wishes,
Stephen

Last edited by StephenW (Dec 24th, 2009 12:14:04 pm)

Re: Backing Up the Databases

StephenW wrote:

I'm also trying to make the process a little more automatic, such as automatic login for PuTTY.  I've been looking through the articles on the OcsSolutions Wiki, but have also been searching the Internet for answers.
I found this article: Automatic login to ssh with Putty, which sounds promising, but I haven't had time to try it out yet.

SSH keys are definitely the way to go here.  Please see:

http://wiki.ocssolutions.com/Generating_an_SSH_key

for information on how to generate an SSH key.  With it, you can automatically log in.

StephenW wrote:

If it is possible, I'd also like to learn how to have PuTTy automatically export the databases into the home directory. One thing that has been helpful is that I learned that I can paste the commands into PuTTy (using right-mouse-click). So, I created a file for each database that has all the commands to export it. Then I can just do a copy>paste to enter the backup commands. This is at least a step in the right direction to make it easier.

PuTTY doesn't really do this itself, but you can create as script to do this and set it up on a cronjob to run automatically.  Your shell script would look something like:

#!/bin/sh

mysqldump -e -u yourcpuser yourdbname > ~/db-backups/yourdbname.sql
mysqldump -e -u yourcpuser yourdbname2 > ~/db-backups/yourdbname2.sql

Replace yourcpuser with your cPanel/FTP/SSH username, and yourdbname with your database name.  yourdbname2 represents another database name, and you can have as many as you want there.

Save the script as ~/bin/db-backup or similar and give it executable permissions:

chmod 0755 ~/bin/db-backup

Then make sure you have a ~/.my.cnf file in your home directory with the right permissions.  Its format is:

[client]
user = yourcpuser
pass = yourpass

then make sure the file has the right permissions:

chmod 0600 ~/.my.cnf

Test your backup script.  It should run fine.  Then, once it does, you can add it for nightly execution by running:

crontab -e

It will open up an editor.  On the last line, add:

00 9 * * * /home/youruser/bin/db-backup

(replace youruser with your cPanel/FTP/SSH user)

This does execute it at 9 AM, but since the servers use UTC/GMT, this is 3 AM for CST.

Re: Backing Up the Databases

rob wrote:

SSH keys are definitely the way to go here.  Please see:

http://wiki.ocssolutions.com/Generating_an_SSH_key

for information on how to generate an SSH key.  With it, you can automatically log in.

Thanks for the info Rob. I've already figured how to "create" a SSH key. Am still in process of trying to learn how to use it. My first attempts at using it with PuTTy have failed. I will try it again .... and again .... and again as I have time.

rob wrote:

PuTTY doesn't really do this itself, but you can create as script to do this and set it up on a cronjob to run automatically.  Your shell script would look something like:

#!/bin/sh

mysqldump -e -u yourcpuser yourdbname > ~/db-backups/yourdbname.sql
mysqldump -e -u yourcpuser yourdbname2 > ~/db-backups/yourdbname2.sql

Replace yourcpuser with your cPanel/FTP/SSH username, and yourdbname with your database name.  yourdbname2 represents another database name, and you can have as many as you want there.

I think the bottom two lines of script look straightforward enough. (Except I don't know what that "#!/bin/sh" means.   With your explanation I can make those changes to represent my database info.

HOWEVER, I'm very new at using scripts. I don't know what to do with them, how to create, etc. Could you give me baby steps, or direct me to a page that tells me what to do with them.
OK, for example. This much I think I under stand. I copy your code into a text editor and change the info to represent my own databases. Then, once I get all that code finished, then what do I do with it? Do I use it on my computer with puTTy, Or do I go into cPanel someplace, or into the home directory of the file section? In the cPanel, under Security,  I see the  SSH/Shell Access buttons, but there is nothing in there for writing or saving scripts. Do I go into the files home directory? In your sample above I see you start with code:

#!/bin/sh 

Does the "/bin/sh" mean that I am supposed to go into the files section of cPanel and create a folder in the home directory to represent /bin/sh, and then create a file inside those folders.

Since I've never used shell scripts, I'm really clueless here about what to do with them. Please point me in the right direction. While I'm waiting I will continue to search the Internet for someplace with some answers about shell scripts. To start with I used Google to search  for  >shell scripts site:ocssolutions.com< and found nothing.

rob wrote:

Save the script as ~/bin/db-backup or similar and give it executable permissions:

chmod 0755 ~/bin/db-backup

What does each part of "~/bin/db-backup" mean? For example:
What does the ~ mean?
What does the "/bin/db-backup" mean. Is this a file directory path? If so, where is it?

I've seen the chmod command before, but didn't know what it meant. In looking it on I found this:

The chmod command (abbreviated from change mode) is a shell command and C language function in Unix and Unix-like environments. When executed, it can change file system modes of files and directories. The modes include permissions and special modes. chmod on Answers.com

So, I'm guess that I would change a file or directory to the 0755 permissions.
I usually change file or folder permissions by going into the file directory, right-click on the mouse, and change the permissions from the context menu. I can also change file permissions using Filezilla by also right-clicking on the file or folder and using the context menu to change the permissions. I haven't used scripts before to do that, so that is all new to me.


rob wrote:

Then make sure you have a ~/.my.cnf file in your home directory with the right permissions.  Its format is:

[client]
user = yourcpuser
pass = yourpass

So, from this......

 ~/.my.cnf 

..... I assume that I should create a file in the home directory named ".my.cnf", meaning that the file starts with a period, then has another period after the "my"?

rob wrote:

then make sure the file has the right permissions:

chmod 0600 ~/.my.cnf

Once I figure you where to enter the scripts, and what the other text means, I can do this. I know that I can give it the 0600 file permissions using Filezilla or by going inside the cPanel files area.

Note: I had a thought/question. All these codes that you have been giving me. Am I supposed to be entering them into puTTy? this reveals my limited knowledge about codes.

rob wrote:

Test your backup script.  It should run fine.  Then, once it does, you can add it for nightly execution by running:

crontab -e

Do I enter this into PuTTy?

rob wrote:

It will open up an editor.  On the last line, add:

00 9 * * * /home/youruser/bin/db-backup

(replace youruser with your cPanel/FTP/SSH user)

If my guess is right, I also enter this into PuTTy?

Sorry to be such a pain. Since I haven't used shell scripts/commands before, I had a bunch of questions.

Thanks for your helpfulness and your patience.

Best Wishes,
Stephen

Last edited by StephenW (Dec 27th, 2009 11:14:15 am)

Re: Backing Up the Databases

rob wrote:

PuTTY doesn't really do this itself, but you can create as script to do this and set it up on a cronjob to run automatically.  Your shell script would look something like:

#!/bin/sh

mysqldump -e -u yourcpuser yourdbname > ~/db-backups/yourdbname.sql
mysqldump -e -u yourcpuser yourdbname2 > ~/db-backups/yourdbname2.sql

Replace yourcpuser with your cPanel/FTP/SSH username, and yourdbname with your database name.  yourdbname2 represents another database name, and you can have as many as you want there.

Hi Rob.
I tried out your above code and couldn't get it to work. (Maybe I just wasn't doing something right.) I did a lot of experimenting as well as a lot of searching on the internet. I came up with this code that works when I copy it into Putty.

mysqldump -u databaseusername -pdatabasepassword databasename > ~/db-backups/dumpfilename1.sql

It's a slight variation of the code I found on this page. Via SSH / MySQL CLI command interface: Dumping a database into a file:

When I paste the code into putty, it copies the database into the directory "~/db-backups". (I created the folder named "db-backups" in the home directory before I started.)

So, looking at your example, adding the "#!/bin/sh" to the top of the file, it would look like this?

#!/bin/sh
mysqldump -u databaseusername -pdatabasepassword databasename1 > ~/db-backups/dumpfilename1.sql
mysqldump -u databaseusername -pdatabasepassword databasename2 > ~/db-backups/dumpfilename2.sql

(the -p goes (without a space) in front of the databasepassword).
The dumpfilename can be the same or different than the databasename.

I have about 7 or more databases. The next thing I'd like to figure out is to put the code to backup all the databases into a file (I'm guessing somewhere in the home directory) that will be executed when it is accessed by cron. When I create that file, I guess that I'm supposed to put this code "#!/bin/sh" at the top of the file, as in the above example.  What does "#!/bin/sh" mean? What does it do in the file?

Edit: I found these links: #!/bin/sh and What does the line: #!/usr/bin/sh do?.

Best Wishes,
Stephen

Last edited by StephenW (Dec 27th, 2009 10:49:51 am)

Re: Backing Up the Databases

Hi Rob,
  While I still want to create an automatic script to backup the databases, I'm at least another step in making it easier. As I mentioned before, I (currently) have 7+ databases that I want backed up. I copied the info for ALL 7 datebases......

mysqldump -u databaseusername -pdatabasepassword databasename1 > ~/db-backups/dumpfilename1.sql
mysqldump -u databaseusername -pdatabasepassword databasename2 > ~/db-backups/dumpfilename2.sql
mysqldump -u databaseusername3 -pdatabasepassword databasename3 > ~/db-backups/dumpfilename3.sql
mysqldump -u databaseusername4 -pdatabasepassword databasename3 > ~/db-backups/dumpfilename4.sql
mysqldump -u databaseusername5 -pdatabasepassword databasename3 > ~/db-backups/dumpfilename5.sql
mysqldump -u databaseusername6 -pdatabasepassword databasename3 > ~/db-backups/dumpfilename6.sql
mysqldump -u databaseusername7 -pdatabasepassword databasename3 > ~/db-backups/dumpfilename7.sql

.......... and pasted them ALL into the terminal screen of PuTTy at the same time. I then went into cPanel to the home directory and found that it had worked. All the databases had been exported to the ~/db-backups folder.

(Just a note, I finally learned the the ~/ refers to the home directory.)

Now, the next thing I need to do is figure out how to create a script (What extension does a script have and where to I physically put the script?) Then I'll use the information you wrote in a previous message to set up a cronjob to automatically backup the databases.

Best Wishes,
Stephen

Last edited by StephenW (Dec 27th, 2009 08:58:46 pm)

Re: Backing Up the Databases

rob wrote:

......then make sure the file has the right permissions:

chmod 0600 ~/.my.cnf

Test your backup script.  It should run fine........

One thing I forgot to ask: How do I test the backup script? Do I somehow test it using PuTTy, or in cPanel, or..... Where do I test the script and what commands do I use?

Best Wishes,
Stephen

Re: Backing Up the Databases

Below is what I've done up to this point:
I did all the following inside the cPanel Files directories:

Script File
I created a folder in the home directory named "bin"
I created the script file in the ~/bin folder and named it "MySQL-db-backups.sh", and then pasted this script into it. (I pasted my actual database names and passwords into the actual file instead of those shown here)

#!/bin/sh
mysqldump -u databaseusername -pdatabasepassword databasename1 > ~/db-backups/dumpfilename1.sql
mysqldump -u databaseusername -pdatabasepassword databasename2 > ~/db-backups/dumpfilename2.sql
mysqldump -u databaseusername3 -pdatabasepassword databasename3 > ~/db-backups/dumpfilename3.sql
mysqldump -u databaseusername4 -pdatabasepassword databasename3 > ~/db-backups/dumpfilename4.sql
mysqldump -u databaseusername5 -pdatabasepassword databasename3 > ~/db-backups/dumpfilename5.sql
mysqldump -u databaseusername6 -pdatabasepassword databasename3 > ~/db-backups/dumpfilename6.sql
mysqldump -u databaseusername7 -pdatabasepassword databasename3 > ~/db-backups/dumpfilename7.sql

I gave the "MySQL-db-backups.sh" file permissions of 0755

Created a folder in the home directory of ~/db-backups  with permissions of 0755.

My.Cnf File
Then I created a ~/.my.cnf in my home directory and entered this info into it.

[client]
user =   databaseuser
password = databasepassword

I gave this file permissions of 0600

Cron Job Test
Since I didn't know how else to test the script, I set up this temporary  cronjob to run every 10 minutes. (which is a short enough time to see if it will actually work.)

*/10      *      *      *      *       wget ~bin/MySQL-db-backups.sh >/dev/null

As I waited about 30 minutes (as I wrote out this message), I kept checking the ~/db-backups folder (where the backups should be going). However, the folder is still empty, so obviously I'm doing something wrong.  I then deleted that cron job.

What am I doing wrong? What am I missing?

Best wishes,
Stephen

Last edited by StephenW (Dec 27th, 2009 09:41:51 pm)

Re: Backing Up the Databases

Hi Rob,
I'm still trying to get this to work:

To see what I might have done wrong, and to see if I could set up a cron job using Putty, I decided to use the code you supplied, and then to try out some modifications
First I connected with PuTTy and entered this code

crontab -e

Then from the next window I entered this code:

10 * * * * /home/winterss/bin/db-backup

That didn't work.

A Successful Test on PuTTy
After some trial and error, I finally came up with this code. I pasted this into Putty....

~/bin/MySQL-db-backups.sh

.... and it executed my "MySQL-db-backups.sh" script file, I saw that it had worked. (I check in the db-backups folder and found that it made a backup of all my MySQL databases.) Now I'll set up a cron job and test that out.

I created this cron job to test it out. I think that this is set to run every 5 minutes, so that I don't have to wait as long to see if it works.

5      *      *      *      *       ~/bin/MySQL-db-backups.sh

That didn't work, so I created another cron job:

SUCCESS!!!
I set up this cron job to run every 5 minutes.....

*/5      *      *      *      *       ~/bin/MySQL-db-backups.sh

.... and found that it exported the copies into the ~/db-backups folder as it was supposed to. I let it run twice and then changed it to run once a day.

This is what the final cron job looks like.

0      21      *      *      *       ~/bin/MySQL-db-backups.sh

Am I correct in assuming that it will just overwrite the backup files each day?

This has been a looong day. I spent some hours figuring this out. (probably someone who knew what they were doing could have done it in only a few minutes.). Anyway, I feel good that I got this step finished.

Now, the next questions:
1.) Is it possible to have each database backup file have the date appended to the end of the name (so that the backups don't overwrite the existing backups), and then to have it automatically delete the previous backups that are more than x-days old (so that the backups don't fill up the disk space)?
2.) Is it possible to have those backup files automatically downloaded and backed up on my computer.

Bests Wishes,
Stephen

Best Wishes,
Stephen

Last edited by StephenW (Dec 28th, 2009 12:35:40 am)

Re: Backing Up the Databases

The ~/.my.cnf File

StephenW wrote:

My.Cnf File
Then I created a ~/.my.cnf in my home directory and entered this info into it.

[client]
user =   databaseuser
password = databasepassword

I gave this file permissions of 0600

With the way that I have cron set up, I don't see that this ~/.my.cnf file is of any use. Do I really need this file?

Best Wishes,
Stephen

Re: Backing Up the Databases

Lastest Report

Hi Robert,
  Just a note to let you know that I finally have Putty with the SSH Keys set up and working.
I have set automatic login (without having to enter the passphrase each time) by setting that up in Pageant. So now I can just start Putty and it automatically logs in.
I've also learned to use Putty PSFTP. It also automatically logs in without having to enter a password or passphrase. I've learned that I can just FTP all the MySQL backup files from the website home/db-backups directory to my computer by just changing directories ~/db-backups and then entering "mget *.sql" into the  Putty PSFTP terminal window.

RECAP
So, as a recap: All the dabases are automatically backed up into the ~/db-backups directory each night. (I might end up changing that to once every few days or once a week. But I decide that later.)

Then, all I have to do is run the PuTTy PSFTP program, when it logs into my website home directory, change directory to  ~/db-backups and enter "mget *.sql" and all the databases are downloaded into the My Documents folder.

Although it was quite a bit of work and quite a challenge for me, this new way is so much simpler and quicker  than my old way to backup the databases.

Robert, thank you so very much for helping me to get transferred to you servers and helping me to get started in this new way of doing things. In the end I can see that this will be much simpler to keep the website files backed up.

WISH LIST
Here are some things I still hope to do.

  • I still want to change the download directory that PSFTP downloads to on my computer.

  • I'd like to figure out a way to automatically have the backup files downloaded onto my computer. I just found a website that might be able to help me figure this out:  Creating Unattended FTP Scripts. I still have to read it over and possibly do some experimenting.

  • I'd also like to figure out how to have the date appended to the MySQL backup files when they are created on the server.

  • I will probably set up ways to back up the other files in my account (such as the various Drupal and image files)

  • I may have others things to add here as I remember them.


Best Wishes,
Stephen

Last edited by StephenW (Dec 28th, 2009 04:14:24 pm)

Re: Backing Up the Databases

StephenW wrote:

WISH LIST
Here are some things I still hope to do.

  • I'd also like to figure out how to have the date appended to the MySQL backup files when they are created on the server.

Just a note: I found some of the things on my wish list:
-->On this web page Compressing mysqldump output I found a way to compress the output of the MySqlDump files.  I have already added this to my backup script.
-->On this web page: Backing up MySQL on the crontab with a shell script is a way to append the date to the MySqlDump backup files. The output file looks like this: "FileName_�Dec-29-09�.sql". However, before I use this, I need to find a script that will delete the saved files after x-days, otherwise the backup files could fill up the disk space.


Although thebelow message didn't give the script, the idea given here (of totally automatic database backups, compression (& my date added to file name), and automatic download to my computer) is something like what I had in mind, with perhaps some variations:
Looking for MySQL Backup program

Shady wrote:

If you have shell access you could write a simple script (or get someone to write it for you) like the one I have written which does the following:

mysqldump (as mentioned above)
gzip (to compress)
ftp (copy backup offsite)

This script is scheduled using cron the run every 12 hours on my machine.




Stephen

Last edited by StephenW (Dec 29th, 2009 01:16:41 pm)

Re: Backing Up the Databases

I don't have time to examine it closely right now, but I think I found some info about making a script to delete old backup files here:  4.5.4. mysqldump — A Database Backup Program

I don't know if I got all the necessary code from that page (If necessary, I'll look more later), but here is a sample of the code I found:

# DO NOT DELETE AUTOMATICALLY FOR NOW, MAYBE LATER

DELETE_EXPIRED_AUTOMATICALLY="TRUE"

# DELETE EXPIRED BACKUPS THAT ARE MORE THAN
# expire_minutes=$(( 1 * 30 )) # 30 minutes old
# expire_minutes=$(( 60 * 24 )) # 1 day old
# expire_minutes=$(( 60 * 24 * 7 )) # 7 days old
# expire_minutes=$(( 60 * 24 * 30 )) # 30 days old

expire_minutes=$(( 60 * 24 * 7 )) # 7 days old

if [ $expire_minutes -gt 1440 ]; then
    expire_days=$(( $expire_minutes /1440 ))
else
    expire_days=0
fi

....and I found a little more code on this page: 4.5.4. mysqldump — A Database Backup Program (Don't know if any of it will be useful, but I'll look at it when I have a little more time.

Posted by Julien GARINO on December 17 2009 7:57am

You always wanted to BACKUP your most important database somewhere in your Linux system, as well as send the dump by email, so that you can recover the entire content if the system crashes.
You can use these 2 scripts.

First Step:
-Install the mutt client that will transfer emails on the command-line : "apt-get install mutt" or "yum install mutt"
-Create the backup directory : "mkdir /home/backups"

Second Step:
- Copy these 2 scripts on your root directory or your user directory :

#!/bin/sh
# Script name : auto_mysql_dump.sh
# Backup the dbname database
dir=`date +%Y-%m-%d`
dbname=`mybase`

...... there is more code below this, but no need to copy that here.

Best Wishes,
Stephen

Last edited by StephenW (Dec 29th, 2009 11:26:35 am)

Re: Backing Up the Databases

StephenW wrote:

The ~/.my.cnf File

StephenW wrote:

My.Cnf File
Then I created a ~/.my.cnf in my home directory and entered this info into it.

[client]
user =   databaseuser
password = databasepassword

I gave this file permissions of 0600

With the way that I have cron set up, I don't see that this ~/.my.cnf file is of any use. Do I really need this file?

Best Wishes,
Stephen

Steve,

I strongly recommend using the ~/my.cnf file instead of putting the password in your script.  When doing this, you can use the 'mysql' command without having to enter the password either.  If you ever change your password you'll just have to change it in one place and not modify your script.  Plus this file is more secure since its permissions are only 0600.

Re: Backing Up the Databases

StephenW wrote:

Robert, thank you so very much for helping me to get transferred to you servers and helping me to get started in this new way of doing things. In the end I can see that this will be much simpler to keep the website files backed up.

No problem!  Glad to help.

StephenW wrote:

WISH LIST
Here are some things I still hope to do.

  • I still want to change the download directory that PSFTP downloads to on my computer.

  • I'd like to figure out a way to automatically have the backup files downloaded onto my computer. I just found a website that might be able to help me figure this out:  Creating Unattended FTP Scripts. I still have to read it over and possibly do some experimenting.

  • I'd also like to figure out how to have the date appended to the MySQL backup files when they are created on the server.

  • I will probably set up ways to back up the other files in my account (such as the various Drupal and image files)

  • I may have others things to add here as I remember them.

For automatic downloads to your computer, check out rsync.  It's not for Windows, so you'll have to use Cygwin.   Setting this up is beyond the scope of this post, but you can use SSH keys with that, so you won't have to enter a password, and rsync downloads only what has changed, saving bandwidth and time.

You can append the date using something like this:

mysqldump dbname > dbname1-`date +%m-%d-%Y`

Re: Backing Up the Databases

rob wrote:

I strongly recommend using the ~/my.cnf file instead of putting the password in your script.  When doing this, you can use the 'mysql' command without having to enter the password either.  If you ever change your password you'll just have to change it in one place and not modify your script.  Plus this file is more secure since its permissions are only 0600.

Hi Robert,
   I think you have convinced me that I shoul dus the my.cnf file, I don't know how to use it. Did you explain it already, or give me a link to it already and I just missed it? If you  haven't explained it, could you give me a link or tell me how to use it?  I'll look again through your messages, etc, when I get time. If I don't find anything, I'll do some more searching on the internet to see if I can find info about how to use a my.cnf file.

Thanks for you advice. Much appreciated.

Edit: I looked back in this message  thread at the info you gave about the my.cnf file. I don't understand how to use it. So I looked on the Internet and found this page: How do I create a .my.cnf MySQL preference file?. I'll read it over a little later. It looks promising.

Best Wishes,
Stephen

Last edited by StephenW (Dec 31st, 2009 12:30:31 am)

Re: Backing Up the Databases

Hi Rob,
   I've tried to use the a bunch of times to use a script with the .my.cnf file, but I must be doing something wrong.
I created a test script called "dbtest.sh"

#!/bin/sh
mysqldump -e -u databaseuser databasename1 > ~/db-backups/databasename1.sql
mysqldump -u databaseuser databasename2 > ~/db-backups/databasename2.sql

# mysqldump -e -u yourcpuser yourdbname > ~/db-backups/yourdbname.sql

# mysqldump -u USER -p DATABASE > filename.sql

(The bottom two (commented out) lines are for my reference, one line of instructions from you and one from another place.)

I created this .my.cnf file first.

[client]
user = databaseusername
password = databasepassword

When I still  permission denied errors, I change the .my.cnf to this:

[client]
user = databaseusername
password = databasepassword
host = Localhost

When I have more time, I'll go through it again. At this point I still just don't understand how it is supposed to work and what to do. I don't have any more time to work on it tonight.

More later,
Best Wishes,
Stephen

Last edited by StephenW (Dec 31st, 2009 02:09:36 am)

Re: Backing Up the Databases

The correct format of the file is:

[client]
user = cpaneluser
password = cpaneluserpw

Replace cpaneluser with your cPanel/FTP/SSH username, and cpaneluserpw with the password to that user.  To ensure it has secure permissions, run in shell:

chmod 0600 ~/.my.cnf

Once you've done that, you can test it my simply running:

mysql

Once you do that, you should see something similar to:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 546048
Server version: 5.0.87-community-log MySQL Community Edition (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

which means its working.  You can then run mysqldump commands without user and password parameters, like:

mysqldump dbname > dbname.sql

and it should work fine.

Re: Backing Up the Databases

Hi Robert,
   Thank you very much for your help. I followed your instructions in the previous message, corrected the .my.cnf file to include the cpanelusername and cpanelpassword, changed the database backup script to get rid of the username and password, and got the got the backup script to work by entering  "~/bin/MySQL-DatabaseBackups.sh*" into the PuTTy command line.
(*MySQL-DatabaseBackups.sh is the name of my backup script).

I have the script set up like this for each database.

mysqldump  cpaneluser_databasename | gzip > ~/db-backups/database-`date +%m-%d-%Y`.sql.gz

It creates a backup like this: databasename-01-01-2010.sql.gz for each database.
the gzipped file is about 1/5 to 1/8 of the regular .sql backup file. Does that sound about right, about the gzipped file being so much smaller than the .sql file?

I still have to do some tests on importing the gzipped backup files into some database files to make sure that they work.

Then corrected my entry in cron to use "~/bin/MySQL-DatabaseBackups.sh"

Using your instructions above, I've appended the date to the database backup name. I did a test run, via Putty, to verify that it worked, and it does. Next I've also change the cron job run to once a week, for now, so it won't fillup the disk space so rapidly.

0      0      *      *      0       ~/bin/MySQL-DatabaseBackups.sh

Do you know how to set up a script to automatically delete the old database backup copies when they get x-days old. (or however that might be done.)

Robert. Thank you so much for all the help that you've given me.

Best Wishes,
Stephen

Last edited by StephenW (Jan 1st, 2010 04:50:00 am)

Re: Backing Up the Databases

StephenW wrote:

The gzipped file is about 1/5 to 1/8 of the regular .sql backup file. Does that sound about right, about the gzipped file being so much smaller than the .sql file?

Yes.  SQL dump files are usually just text, so they compress very well.

StephenW wrote:

Do you know how to set up a script to automatically delete the old database backup copies when they get x-days old. (or however that might be done.)

You can use find and xargs like this:

cd ~/your-backups ; find -atime 5 | xargs rm

to delete files created more than 5 days ago.

StephenW wrote:

Thank you so much for all the help that you've given me.

Not a problem, we're here to help!

Re: Backing Up the Databases

rob wrote:
StephenW wrote:

Do you know how to set up a script to automatically delete the old database backup copies when they get x-days old. (or however that might be done.)

You can use find and xargs like this:

cd ~/your-backups ; find -atime 5 | xargs rm

to delete files created more than 5 days ago.

Hi Rob,
Thank you very much for giving me that script. It is a great beginning point!

I tested your script using PuTTy and found that it deleted my backups that were only 3 days old, which wasn't what I wanted. So I searched the Internet and found this page about FIND. I had to make a slight change to your script.  The "-" before atime means it would find any files less than 5 days old. I then change the script to be like this:

cd ~/db-backups ; find +atime 5 | xargs rm *.gz

The "*.gz" at the end means that it will find all the files that end in ".gz".

It tested that code again using PuTTy, and it worked fine. It deleted all the files ending in .gz that were older than 5 days.

Then I created a file in ~/bin that I named "RemoveOldMySQL-backups.sh". This is the contents of the file:

#!/bin/sh
# change directory to home/db-backups
# find all files ending in .gz that are over 30 days old and remove (delete) them
cd ~/db-backups ; find +atime 30 | xargs rm *.gz

This code should find and delete all .gz files that are older than 30 days.

I then set up a cron job.....

30      0      1,15      *      *       ~/bin/RemoveOldMySQL-backups.sh

... to activate tha "RemoveOldMySQL-backups.sh" file twice a month, on the 1st and on the 15th of each month

I'll just need to check on the backup files, on the website and on my computer, now and then to make sure they continue to work as they are supposed to work.


Now that the website is set up to automatically back up the MySQL databases, I just have to figure out a way to automate the download of the backup files to my computer.

Best Wishes,
Stephen

Last edited by StephenW (Jan 13th, 2010 01:50:57 pm)

Re: Backing Up the Databases

StephenW wrote:

Now that the website is set up to automatically back up the MySQL databases, I just have to figure out a way to automate the download of the backup files to my computer.

There's 2 ways to do this.

If you're using Linux or a Mac, you can use rsync to download files:

rsync -av cpuser@yoursite.com:/home/cpuser/backup/yourbackupfile.tar.gz /home/yourlocaluser/wherever

Just add a cronjob for this and you're done.

If you're using Windows, you have 2 options.  Install Cygwin to get access to the rsync and crontab commands (probably best method long term), or use a program like DeltaCopy.