If you are a Database Administrator and want to safe keep your business you need regular backups. Here is a small bash script I made to schedule MySQL databases and dump mysql database and compress the output file.
All you need to do is give values to USER, PASSWORD,DB_NAME,OUTPUT reflect your MySQL user, password, database name and folder where you want to put the dumps.
mysql_backup.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | USER= "db_user_name" PASSWORD= "db_password" OUTPUT= "/backup_folder_path" DB_NAME= "db_name" databases=`mysql --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep - v Database` for db in $databases; do if [ "$db" = "$DB_NAME" ]; then FILE=$db "_" ` date +%Y%m%d%H%M`.sql echo "Dumping database: $db" mysqldump --force --opt --user=$USER --password=$PASSWORD --databases $db > $OUTPUT/$FILE gzip $OUTPUT/$FILE fi done |
Now make mysql_backup.sh executable by setting permission
1 | chmod 700 mysql_backup.sh |
Now you are ready to schedule the job and run the backup script. Simply add to crontab so that it will run automatically
1 2 | # m h dom mon dow command 00 4 * * * sh /path_to_your_script/mysql_backup .sh |
The above crontab is scheduled to every morning at 4AM
No comments :
Post a Comment
Post Your Thoughts