Page 1 of 2
Auto backup mysql
Posted: Wed Sep 30, 2015 6:36 pm
by northwest
Hello,
How do I run an automatic copy of MySQL performed every 60 minutes? I could ask for a command to cron?
Re: Auto backup mysql
Posted: Thu Oct 01, 2015 6:53 am
by tjebbeke
You can use this bash script with cronjob:
Code: Select all
#!/bin/bash
# Database credentials
user="DB_USERNAME"
password="DB_PASSWORD"
host="localhost"
db_name="DB_NAME"
# Other options
backup_path="/home/username/web/youdomain/public_html/backup"
date=$(date +"%d-%b-%Y")
# Set default file permissions
umask 177
# Dump database into SQL file
mysqldump --user=$user --password=$password --host=$host $db_name | gzip > $backup_path/$db_name-$date.sql.gz
# Delete files older than 30 days
find $backup_path/* -mtime +30 -exec rm {} \;
Re: Auto backup mysql
Posted: Thu Oct 01, 2015 9:17 am
by northwest
Thank you. How can i add this to crontab?
In terminal: crontab -e and paste your code?
Re: Auto backup mysql
Posted: Thu Oct 01, 2015 11:53 am
by tjebbeke
northwest wrote:Thank you. How can i add this to crontab?
In terminal: crontab -e and paste your code?
Create a file: backup.sh
in crontab -e:
0 * * * * bash /home/username/backup.sh
Re: Auto backup mysql
Posted: Tue Oct 06, 2015 6:54 pm
by skurudo
tjebbeke wrote:
Create a file: backup.sh
Don't forget:
chmod +x backup.sh
..why we need bash? simple /path/to/backup.sh ?
Re: Auto backup mysql
Posted: Tue Oct 06, 2015 6:56 pm
by skurudo
tjebbeke wrote:
0 * * * * bash /home/username/backup.sh
If you don't want to receive e-mail notification, add after .sh:
>/dev/null 2>&1
Re: Auto backup mysql
Posted: Wed Apr 20, 2016 10:00 pm
by waynebr76
I am trying to implement this but just keep getting errors. Can anyone help me figure it out? I have little knowledge so am stuck.
: octal number out of ranges-#####/public_html/backup/backup.sh: line 11: umask: 177
: No such file or directorys-#######/public_html/backup/backup.sh: line 13: /home/admin/web/######/public_html/backup
' (0) when trying to connectUnknown MySQL server host 'localhost
Re: Auto backup mysql
Posted: Thu Apr 21, 2016 8:41 am
by tjebbeke
waynebr76 wrote:I am trying to implement this but just keep getting errors. Can anyone help me figure it out? I have little knowledge so am stuck.
: octal number out of ranges-#####/public_html/backup/backup.sh: line 11: umask: 177
: No such file or directorys-#######/public_html/backup/backup.sh: line 13: /home/admin/web/######/public_html/backup
' (0) when trying to connectUnknown MySQL server host 'localhost
What is your OS? Have you created the 'backup' dir in /home/admin/web/######/public_html ?
Re: Auto backup mysql
Posted: Thu Apr 21, 2016 11:07 am
by waynebr76
Hi I am using Ubuntu Linux 14.04.2, the directory has been created and I tested it using mkdir and it says it exists.
Re: Auto backup mysql
Posted: Fri Apr 22, 2016 1:26 pm
by skurudo
- Check path in cron
- Check sh script - exutable or not?
mkdir /path/to/directory
chmod +x /path/to/script.sh
Add in cron.
PS: Don't forget, if you run this under user (not root), simple user will have ssh (nologin don't work there) or have right to sudo your script.