

Run the following command to open the root crontab file: sudo crontab -e We will do this by adding a call to the mysql-backup.sh script to the root crontab. Now we will schedule the backup script to be run daily.

mysql-backup.sh Step 2: Create the Crontab Scheduled Task You can now test the backup script by running: sudo. Once you have modified the options, make the script executable with the following command: sudo chmod +x mysql-backup.sh This should be changed to a MySQL user and password that has permissions to list and backup databases. The main ones you will need to edit are the USER and PASSWORD. You will notice 5 configurable options at the beginning of this script. Mysqldump -u $USER -p$PASSWORD -databases $db | gzip -c > $BACKUP_PATH/$date-$db.gzĮcho "Deleting backups older than $DAYS_TO_KEEP days"įind $BACKUP_PATH/* -mtime +$DAYS_TO_KEEP -exec rm \ Mysqldump -u $USER -p$PASSWORD -databases $db > $BACKUP_PATH/$date-$db.sqlĮcho "Backing up database: $db with compression" If || || || thenĮcho "Backing up database: $db without compression" I suggest /scripts for this example:Ĭreate a file called mysql-backup.sh inside the scripts folder: sudo vim /scripts/mysql-backup.shĪdd the following code to the file and save it: #!/bin/bashĭatabases=`mysql -u $USER -p$PASSWORD -e "SHOW DATABASES " | tr -d "|" | grep -v Database` Create a folder to store your backup script in.
