I'm trying to get mysqldump to do backups to a .sql file automatically, I have being reading that I need to use cron jobs or Windows Task Scheduler; the problem is that I can't find anything online that shows me how to do it.
To do the backups I'm using cmd with the following commands:
mysqldump --user username --password=123 databtable > backup.sql
This command works perfectly, it does create the .sql file but how do I automate it in such a way that it does the backup every certain time.
Hopefully you can help me and thank you so much!
You should use the schtasks command in Windows.
Command syntax details are available here: https://technet.microsoft.com/en-us/library/cc772785(v=ws.10).aspx
You could also use the Task Scheduler application in Windows if you like GUIs:
Create a new basic task
Set the Trigger (run daily, weekly, etc)
Set the Action. (what program to run) Be sure to
include only the executable in the Program field, and put the
command arguments in the Add Arguments field.
You probably want to set your task to 'Run whether the user is logged on or not'. This is achieved by modifying the task, and adjusting the Security Options on the General Tab of the task.
When troubleshooting your task, use the History tab for info regarding job failures.
Related
I am trying to create a shell script as part of my cron job for my MySQL daily backups. I am using Webmin as the GUI interface and I was told to insert a small shell script as part of the Command to run after backup option in Webmin per noisemarine's response in this post: https://www.virtualmin.com/node/54190.
Currently, I have to manually create a folder named after the current date (e.g. 6/6/2018) and move my four MySQL database backup files into this newly created folder. If I don't, the backup files will be overwritten by the next day's backup.
All my MySQL backups are stored in /etc/mysql/MySQL Backups/. Since I am still new to this, I need help in knowing which commands/script I need to perform for the following:
Automatically create a new folder each day named after the current date
(e.g. 6/6/2018) that's stored in the /etc/mysql/MySQL Backups/ directory.
Automatically move all four .sql backup files into the newly created folder
after an automatic MySQL backup (e.g. cron job automatically backups MySQL
and subsequently, the command used in the script will automatically move the
backup files into the 6/6/2018 folder)
I tried using the following commands separated by a ; as notated by noisemarine but to no avail:
BDIR=/etc/mysql/MySQL Backups/ ; DATE=`date +%F ; mkdir -p $BDIR/$DATE ;mv $BDIR/*sql $BDIR/$DATE/
I don't know what's wrong with this picture.
Again, it looks like all I need to do is insert a list of commands in the box titled Command to run after backup option in Webmin (see attached image for visual representation).
Any help would be generously appreciated!
Thank you!
Webmin Configuration screenshot
I have a SQL script which selects data from DB and stores it to files. I am unable to create a directory to store these files.
I have shell script that loads the SQL file. Shell and the SQL are on separate server than MySQL db. I would prefer to create this directory using SQL as I want to avoid ssh.
Any suggestions? Surprisingly I couldn't find anything on Google.
I will assume that you're using mysql, according to your tags. You could do it with a Microsoft SQL Server or Oracle database but unfortunately, at the moment, there is no solution to create a directory from MySQL.
Some will guide you with a workaround based on the creation of a data directory, I wouldn't recommand this, as it could lead to performances issues in the future, or worst.
The best solution would be to use a script (java, vbscript, SSH, batch, ...). Again, you won't be able to start this script within your SQL query easily. I know that's no good news, but it is important not to lead you on the wrong direction.
I would suggest to reverse your thinking, and start your SQL query from a script (again, any language you're used to).
I couldn't find any other way other than opening ssh session to the target box.
Open ssh session
Create directory
close ssh session
Load sql file using shell
The sql adds the generated files to the directory created in step 2.
ssh -t $USER#$HOST <<-SSH-END;
mkdir -p "dir/path";
exit;
SSH-END
Sharing just in case someone else needs to do the same.
Situation
I am trying to automate the downloading of my websites' files, emails and databases to my iMac. I am also trying to use rsync so that besides the first download, the rest will just be incremental. I discovered crontab and have finally gotten the right expression to rsync my websites' files, as per follows:
30 01 * * * * rsync -azvv -e 'ssh -p MYPORTNUMBER' root#MYIPADDRESS:/var/www/vhosts/ /Users/MYUSERNAME/var/www/vhosts
Question
How do I automate the downloading of all the databases in my server in SQL format to my iMac, plus do it in incremental style? There are so many solutions out there but they all seem to suggest using shell script, and that shell script has to be stored on the server. Is it possible to store the shell script on my imac then have the crontab to run it? And can someone please help me with the expressions for it?
I have tried creating a .sh file in my imac, then running it in crontab or terminal. But I get this error message "cannot execute binary file".
Or, should I be finding the script to transform the databases into sql format, store it somewhere on the server, then download it to my computer, then delete it off the server?
I have never touched shell script or crontab until now, so I am very scared that some wrong command will screw up the server or delete or modify files unknowingly.
You want a shell script that will dump your MySQL database to a .sql file and then you can rsync that file over. The script will need to live on your server and create the file. Then you can rsync it over to your Mac. Something like this to get you started:
#!/bin/sh
/usr/bin/mysqldump MyDatabase > mydatabase_backup.sql
Of course this just overwrites your backup each time so if you want to get extra credit you can learn how to create variables based on the current date in a shell script and use the date in your backup file name.
I use Java and MySQL with Appserv. I have a database file named "modb.sql" (in a specific directory relative to my program's location), and I frequently need to drop the old database, create a new database (with the same name every time, inside phpMyAdmin) and import the modb.sql file to the new DB.
Is there a way to automate this process and include it with the program or inside a setup file? Instead of doing it manually by me or the user.
I can use another MySQL database manager or C# instead of Java if that would allow the process to be automatic.
You can do all of this from the Windows command line or through a Scheduled Task. I don't have Appserv installed, so I can't give you all of the exact file path locations which may vary based on your installation location. You could technically do it from a Java application as well, but that's a lot of overhead and not really automating it like a schedule task will.
You'll basically be calling the mysql.exe command directly from your schedule task -- or from a batch file.
First, let's create a new SQL file, perhaps called DropAndCreateMoDb.sql. Put your commands to drop (DROP DATABASE ...) and recreate the database here. Of course the exact drop command depends on what you call your database and the create command depends heavily on what structure is created by modb.sql; you'll also create any permissions that you need to here. This automates the part about dropping and recreating.
Write a batch file. You don't really need to do this, you could call MySQL twice from the schedule task, but we're trying to do this the right way. This is untested, and obviously you'll want to substitute the proper paths and MySQL username/password -- I suggest creating a maintenance account for this so your full credentials aren't in the batch file; the usual disclaimers apply about properly securing your account), but perhaps something like:
#ECHO OFF
C:\Program Files\Appserv\MySQL.exe -u foo -p bar < C:\data\DropAndCreateMoDb.sql
C:\Program Files\Appserv\MySQL.exe -u foo -p bar < C:\data\modb.sql
Obviously it doesn't catch any errors and your username and password are in the clear here, but as long as this is on your local development machine for development purposes and you know and understand the risks, it will work.
At this point, you can double-click the .bat file and it should drop and recreate your database. To finish it off and fully automate it, you'll add a scheduled task. Go to the Scheduled Tasks control panel and add a new task. Tell it the path to your new DropAndRecreate.bat or whatever you decide to call it, tell Windows when you want the task to run, and now it's fully automated.
There are lots of variables here that make the specifics of the implementation very dependent on your exact configuration and so on. Make sure you understand what each step actually does instead of just copying and pasting.
I am looking for a way to schedule a database table optimisation on all MySQL data.
Currently I can do it by using the mysqlcheck -o --all-databases command, and I thought about scheduling it with cron, but the problem is that the password would remain cleartext inside /etc/crontab.
There is a scheduling feature in MySQL, but I don't know how it could fit the need, it sends MySQL commands, it doesn't launch shell programs.
Any tips?
Can use cron, use the --defaults-extra-file option to specify a file, in my.cnf format. It can contain the username/password needed.
Use standard linux permissions to make it only readable to the user running the cron task.
See third bullet-point here: http://dev.mysql.com/doc/refman/5.1/en/password-security-user.html