As many of the developers host their e-commerce sites locally or on cloud, they are required to take the backup of their applications on some remote location so that they may recover their data if any type of failure occur and can make their websites available to everyone soon after the downtime. Although many of the e-commerce sites frameworks such as magento, joomla etc provide backup functionality at their own but it requires you to login to the console,navigate to the backup page, click the backup button, wait for the backup to finish and then download the backup. Moreover, you might not be familiar enough with this backup procedure as it may differ from framework to framework.
What if we provide you a better way to automate the complete backup of your e-commerce website on daily basis and store them on some remote location. This requirement includes a simple bash script, FTP connection to destination location where you want to store your backup and setting up cron jobs in order to automate them. Here we go !
STEP1-Login Without Password : Since we want to completely automate the process using cron job, it requires that the backup script won’t ask for password at run time to store the backup at destination location. For this, we must be able to access the destination location from our local system without password. We have multiple ways to do so, the first way is to login using pem files and the other way is to place your public key into destination’s authorized keys.Here, we will describe the latter one.
Generate public key :
1 |
ssh-keygen -b 1024 -t rsa |
The generated public key is stored in /home/users/username/.ssh/id_rsa.pub.Place your public key into destination’s authorized_keys :
1 |
cat .ssh/id_rsa.pub | ssh username@destinationIP 'cat >> .ssh/authorized_keys' |
This command will prompt you for the password if you are trying to connect for the very first time, and then it will place your public key into destination’s authorized keys. Now, everytime you want to ssh/scp the destination location from your local system, it won’t prompt you for password since the destination location now recongnizes your public key.
STEP2-Write The Backup Script :
Create a simple bash script named mysite_backup.sh inside your home directory i.e. /home/username/mysite_backup.sh and paste the following content :-
1 |
nano mysite_backup.sh |
1 2 3 4 5 6 7 8 9 10 11 |
sfolder="NULL" #mention the Source folder's complete path e.g /var/www/magento username="NULL" #Username of Your Destination System dip="NULL" # Destination IP where you want to store your backup mysqluser="NULL" #Mysql Username used in your application dbname="NULL" #Mention the Database Name used in your application extension=".sql" #Do not change it scp -r $sfolder $username@$dip:/home/$username/www/ mysqldump -u$mysqluser -p$dbpassword $dbname > $dbname$extension scp -r $dbname$extension $username@$dip:/home/$username/www/ |
STEP3-Setup Cron : The final step is to run this script on regular basis which requires to setup a cron job. Run the following command :-
1 2 |
crontab -e |
Suppose you want to run this script everyday at 9 pm (this may change according to your requirement), add the following in the last line :-
0 21 * * * $pathtoscript/cpanel_backup.sh
1 |
0 21 * * * /home/users/username/cpanel_backup.sh |
IN CASE OF ANY QUERY, PLEASE CONTACT US