Stefan H.
By:
Stefan H.

mysqlsumb

July 8, 2013 4.6k views
Hallo! How can i via crontab add a mysqldumb that runs every day at 21:00 CET (danish time) and put it in /home/xxx
16 Answers
Set your droplet's timezone to CET:

sudo dpkg-reconfigure tzdata

Run "crontab -e" to edit your cronjobs (or "EDITOR=nano crontab -e" if you want to use nano), and append the following line:

0 21 * * * /bin/bash /home/xxx/script.sh
can i do 0 21 * * * /usr/bin/mysqldump /home/xxx/
You can type out the command there, yes.
I have made a mysqldump.sh (and chmod +x the file) script with the following content :

#!/bin/bash

suffix=$(date +%w%a)

rm /home/backup/xxx/$suffix.sql.gz
mysqldump -h localhost -u root -pxxxxxx xxxx | gzip > /home/backup/xxx/$suffix.sql.gz

and entered this in crontab : 0 21 * * * /bin/bash /home/xxx/mysqldump.sh

will that work?
looks like itsworking, did sh mysqldump.sh :)
Another thing can i get cron to email me when the backup is completet?

like backup is done the cron sents a mail with "Backup Completed" ?
Yes, append this to mysqldump.sh:

echo "MySQL backup" | mail -s "MySQL backup" your@email.com
I tried this : * * * * * /bin/echo "foobar" >> /home/backup/testfile.txt echo "Mysql Backup Cpmpleted | mail -s "Backup Completed" firesecret@gmail.com

But get this error in my mail : /bin/sh: 1: Syntax error: Unterminated quoted string
Add it to the script itself not the cronjob.
missed a " :D but there is no body text (content) only subject does contain what i want
how :

i have this in the script

#!/bin/bash

suffix=$(date +%Y-%m-%d)

rm /home/backup/xxx/$suffix.sql.gz
mysqldump -h localhost -u root -pxxx db | gzip > /home/backup/xxx/$suffix.sql.gz
Add this as a cronjob (and remove the other ones):

0 21 * * * /bin/bash /home/xxx/script.sh

Script:

#!/bin/bash

suffix=$(date +%Y-%m-%d)

rm /home/backup/xxx/$suffix.sql.gz
mysqldump -h localhost -u root -pxxx db | gzip > /home/backup/xxx/$suffix.sql.gz
echo "MySQL backup" | mail -s "MySQL backup" your@email.com
Got it working :)

#!/bin/bash

suffix=$(date +%Y-%m-%d)

rm /home/backup/db/$suffix.sql.gz
mysqldump -h localhost -u root -pxxx db | gzip > /home/backup/db/$suffix.sql.gz
echo "Mysql Backup Completed" | mail -s "Backup Completed" firesecret@gmail.com
echo Backup completed
can i add a from mail adress : right now it reads "root"
Read http://unixhelp.ed.ac.uk/CGI/man-cgi?mail
Got it Working \o/

Steps :

1. chmod 640 /etc/ssmtp/ssmtp.conf
2. chown root:mail /etc/ssmtp/ssmtp.conf
3. nano /etc/passwd
4. changed : root:x:0:0:root:/root:/bin/bash
5. To root:x:0:0:root@TheCloud,,,:/root:/bin/bash
Have another answer? Share your knowledge.