I tried many scripts for database backup but I couldn't make it. I want to backup my database every hour.
I added files to "/etc/cron.hourly/" folder, changed its chmod to 755, but it didn't run.
At least I write my pseudo code.
I would be happy if you can write a script for this operation and tell me what should I do more ?
After adding this script file to /etc/cron.hourly/
folder.
- Get current date and create a variable,
date=date(d_m_y_H_M_S)
- Create a variable for the file name,
filename="$date".gz
- Get the dump of my database like this
mysqldump --user=my_user --password=my_pass --default-character-set=utf8 my_database | gzip > "/var/www/vhosts/system/example.com/httpdocs/backups/$("filename")
- Delete all files in the folder
/var/www/vhosts/system/example.com/httpdocs/backups/
that are older than 8 days - To the file
"/var/www/vhosts/system/example.com/httpdocs/backup_log.txt"
, this text will be written:Backup is created at $("date")
- Change the file owners (chown) from root to "my_user". Because I want to open the backup and log files from the "my_user" FTP account.
- I don't want an email after each cron.
>/dev/null 2>&1
will be added.
Create a script similar to this:
Then you can edit the
crontab
of the user that the script is going to run as:And append the entry
This will make it run on the first minute of every hour every day.
Then you just have to add in your rolls and other functionality and you are good to go.
You might consider this Open Source tool, matiri, https://github.com/AAFC-MBB/matiri which is a concurrent mysql backup script with metadata in Sqlite3. Features:
Full disclosure: original matiri author.
After hours and hours work, I created a solution like the below. I copy paste for other people that can benefit.
First create a script file and give this file executable permission.
Then copy following lines into file with Shift+Ins
Edit:
If you use InnoDB and backup takes too much time, you can add "single-transaction" argument to prevent locking. So mysqldump line will be like this:
As a DBA, You must schedule the backup of MySQL Database in case of any issues so that you can recover your databases from the current backup.
Here, we are using mysqldump to take the backup of mysql databases and the same you can put into the script.
[orahow@oradbdb DB_Backup]$ cat .backup_script.sh
Continue Reading .... MySQL Backup
I got the same issue. But I manage to write a script. Hope this would help.