Automatic backup from all databases in MySQL and transfer them to another server
You can follow these steps to take a backup from your database and transfer them to safe place outside of this server. then put it inside cronjob to do it periodically. First login to your server, we are use Ubuntu.
enter your mysql root password or some other user. change username if you want and enter your password
sudo mysql -u root -p
CREATE USER 'dumper'@'localhost' IDENTIFIED WITH mysql_native_password BY 'PUT_YOUR_PASSWORD_HERE';
GRANT SELECT, PROCESS, LOCK TABLES ON *.* TO 'dumper'@'localhost';
flush privileges;
exit;
Also as alternative way, you can insert user via phpmyadmin. check only needed permission for create readonly user
open or create mysql public conf file
sudo nano ~/.my.cnf
copy and paste below text and update username and password if needed
[mysqldump]
user=dumper
password=PUT_YOUR_PASSWORD_HERE
Press ctrl+x
then press y
to save file and exit
after above steps you can create a backup file from specefic database with command mysqldump without password. -v flag show process list and we don't need it inside automation process
mysqldump -v --column-statistics=0 --quick --single-transaction YOUR_DATABASE_NAME > /home/backup-$(date +%Y%m%d-%H%M%S).sql
it's depend on you to create a backup from one database or all.
mysqldump -v --column-statistics=0 --quick --single-transaction --all-databases > backup-$(date +%Y%m%d-%H%M%S).sql
for our example database 1.7G compressed into 70Mb! so it's better to compress backup
mysqldump -v --column-statistics=0 --quick --single-transaction --all-databases | gzip > backup-$(date +%Y%m%d-%H%M%S).sql.gz
To sync your files from server A (main) to server B (backup), follow these steps.
Install rsync on both server A and server B.
apt-get install rsync
Run the following command to generate an SSH key.
ssh-keygen
Press enter to skip all inputs.
ssh-copy-id root@1.2.3.4
press yes and enter password of target server. you can change user to anything
Connect on port 22 of ssh. delete file on target server if exist then copy to path in another server
rsync -avrt --delete --rsh='ssh -p 22' /home/backup-file.sql /target_server/path/
We are recommend to create backup from all. so create a sh file
sudo nano /home/mysql-auto-backup/backup.sh
Copy and paste below line inside editor to create mysql backup
FILENAME=backup-$(date +%Y%m%d-%H%M%S).sql.gz
mysqldump --column-statistics=0 --quick --single-transaction --all-databases | gzip > /home/mysql-auto-backup/$FILENAME
rsync -avrt --delete /home/mysql-auto-backup/$FILENAME root@1.2.3.4:/home/mysql-auto-backup/
press ctrl+x
then press y
to save file and exit
Setup a cronjob to sync your files automatically. This example syncs them every hour.
nano /etc/crontab
paste below line to run sh
0 * * * * root sh /home/mysql-auto-backup/backup.sh >/dev/null 2>&1
press ctrl+x
then press y
to save file and exit