Linux backup
LINUX MYSQL DATABASE BACKUP USING AUTOMATIC SCHEDULED JOBS
Ensure mysqldump function is available by typing
mysqldump - -version
If mysqldump is installed, proceed to step 3. If not please install mysqldump using
sudo apt install mariadb-client
Install Zip
sudo apt install zip
Next, we are to write a script to backup database using mysqldump
Create a new directory for storing the backups:
sudo mkdir /var/backups/mysql
Create a new shell script that performs the backup and compression:
sudo nano /usr/local/bin/mysql-backup.sh
In the text editor, paste the following code:Adjust where necessary
Save and close the file (Ctrl+X, Y, Enter).
Make the script executable:
sudo chmod +x /usr/local/bin/mysql-backup.sh
Set up a weekly cron job to run the backup script:
sudo crontab -e
Add at the end of the file the following line:
0 16 \* \* \* /usr/local/bin/mysql-backup.sh
Above example (0 16 * * *) means this job runs every day at 16:00
Change the timing of the cron job to suite you and save.
Done .
SENDING BACKUPS TO REMOTE SERVER
In case you want to send backups to a remote server REQUIREMENTS :
REMOTE SERVER
REMOTE USER
Generate SSH keys on your local server(facility machine) if you haven't already done so:
ssh-keygen -t rsa
Press Enter to accept the default file location and passphrase or specify your preferred options.
Copy the public key to the remote server:
ssh-copy-id user@remote_host
This will prompt for remote host password
Test the connection using
ssh user@remote_host
to login with out password. If successful, then our local machine is now able to accessible the remote without passwordEdit the mysql-backup.sh file to add the following code at the end of the file
Save the file and test it by running sudo sh /path/to/mysql-backup.sh
File transferred successfully.
Last updated