Here is how to perform backups on the mysql database by using a bash script:
First step just create a file backup.sh
1 |
vi backup.sh |
and copy the following scripts into backup.sh file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#!/bin/bash # Database credentials user="databse user" password="database password" host="localhost" db_name="database name" # Backup location backup_path="/home/backup-db" 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 > $backup_path/$db_name-$date.sql # Delete files older than 30 days find $backup_path/* -mtime +30 -exec rm {} \; |
Now you just need to make it executable:
1 |
chmod 700 backup.sh |
And then add it to the crontab so it’ll run automagically:
1 |
crontab -e |
1 |
0 1 * * * /path/to/backup.sh |
In this case it’ll run every day at 1 AM.