Running Auto Backups
This article will guide you for the following:
- Coding A Executable Backup System through Linux Shell
- Optional: Mailing The Backup Automatically To An Email ID
- Setting Up Cron To Run It Automatically as per schedule
Stage 1: Coding the shell file
You can use any usual text editor like Notepad for Windows to write this file.
Start the file with bash command:
Create the MySQL dump:
/bin/mysqldump --user=cpUsername --password=cpPassword cpUsername_database > /home/cpUsername/cpUsername_database.sql
Remove the old backups:
rm -f /home/cpUsername/backup.tar.gz
Archive and Compress you SQL Dump and public_html folder:
/bin/tar zcvf /home/cpUsername/backup.tar.gz *.sql public_html/*
Note: I you want to add a folder outside public_html, you can enter folder_name/*.
Note: Replace cpUsername and cpPassword with you respective cPanel username and password. Replace database with your database name. Repeat this line for every database you have. Also replace Email ID with the required email which will receive the backup.
Optional Step: Attaching and Mailing:
date | /bin/mutt -a "/home/cpUsername/backup.tar.gz" -s "cPanel Automated Backup" -- Email ID
BEWARE: The backup is not encrypted. Mailing it to an unknown Email will give the Email ID Owner access to all MySQL databases and public_html folder. any credentials contained within will be exposed.
Save the file as a .sh file with the name backup. So the file will be backup.sh. It is a linux shell file and cannot be executed on a windows/mac.
Upload it to the root directory of your account i.e. /home/cpUsername/
Give it 755 permissions. It cannot run without these required permissions. The permissions can be modified using the cPanel's inbuilt File Manager.
Stage 1 Done!!!
Stage 2: Setting the cron
In your cPanel, go to Cron Jobs under Advanced Section in any theme.
Set it to run monthly and then change the minute, hour and day of the operation to something random.
Request: please do not run it more frequently than monthly and set the day, minute and hour of run to something completely random to prevent a collective load on the server. If everyone runs their backup at the same time, it will cause a very heavy load on the server and all of us love HelioHost, so please. This is a sincere request from a HelioHost user to other users.
In the command box type:
If you want to prevent error messages from being emailed you can add "2>/dev/null" to the end of the command. An error message will always be created as you will be passing a password on the linux shell command line.
Stage 2 Done!!!
This is the end. Your automatic backup system is ready.
An Assembled Example
#!/bin/bash /bin/mysqldump --user=user123 --password=test123 user123_testdb > /home/user123/user123_testdb.sql rm -f /home/user123/backup.tar.gz cd /home/user123/ /bin/tar zcvf /home/user123/backup.tar.gz *.sql public_html/* date | /bin/mutt -a "/home/user123/backup.tar.gz" -s "cPanel Automated Backup" -- email@example.com