Hi Everyone! Backups are actually done to avoid losing important data from a website. Therefore, don't let the website crash, your hosting provider doesn't provide backups. Or do you want to do the backup yourself? You can! You can directly download the backup files via hosting one by one. So for those who are lazy to download backup files one by one, you can set them automatically. You only need to create a backup script then make scheduling via cronjob so that it is automatic. You can set the backup time, for example, once a day, once a week or once a year. For this reason, this guide will explain how to do daily backups with cronjob.

How to Backup Daily with Cronjob

To do a daily backup with a cronjob, you can follow a few steps to do a backup, set the required cron script, then continue setting the cronjob.

Doing a Backup

First, you have to back up your website data first.

1. Login cPanel Hosting

Please login cPanel Hosting via https: // domainname / cpanel, according to the email sent from KincaiHosting regarding Hosting Account Information. For those of you who don't know how to log in to cPanel, please read on How to Login cPanel Hosting .

After a successful login, please type "backup" in the search field. To do a backup, please select Menu Backup.

3. Perform Data Backup

There are several backup options such as Full Backups, Account Backups, Partial Backups or MySQL Database Backups. Well, you can back up data manually by downloading the backup file. For example, you want to download a backup file home. Please click Home Directory.

how to backup daily with cronjob

Creating Script For Cron

Now it's time to write a script so you can do daily data backups.

4. SSH to Hosting

Please SSH to Hosting by following the SSH Access to Hosting guide .

5. Make Daily Cron Backup Script

After successfully entering hosting via SSH, please enter root or public_html. Then create a folder named " backups" with the command:

After that create a file backup.shwith the command,

Open the backup.sh file then paste the following script,

 

#!/bin/bash

tar -czvf ~/backups/backup_username-%d-%m-%Y.tar.gz ~/public_html

You can use an editor like Nano or Vim. Don't forget to save it! For those of you who use the nano editor, to save the edited file, ctrl + x then press Y on the keyboard then Enter.

Now, it's time to change the permissions of the backup.sh file to 700. To change the permissions, please do the command:

Cronjob Settings

After creating a backup file and creating a cron script, now is the time to set up cronjob on the hosting.

 

Please select the cronjob menu in cPanel so that the script can be scheduled automatically.

7. Make a Daily Cron

Then write the email address in the section Cron Email. This email serves as a destination email when you want to do a full backup. If so, please click Update Email.

how to backup daily with cronjob
Now, it's time to add a new cronjob. In the Common Settings section, please select " Once Per Day (0 0 * * *)" if you want to do a backup every day.
Then in the Command section, you can write:

If so, don't forget to click Add New Cron Job.

how to backup daily with cronjob

If the backup script has been scheduled for cronjob, a command will appear in the column Current Cron Jobs. You can change the command and schedule time with a click Edit.

how to backup daily with cronjob

So that your hosting is not full, then delete the previous backup file. To do this, please write to create a backup file script again with the name deletebackup.sh. Then write the following script:

 

#!/bin/bash

rm -rf ~/backups/*

tar -czvf ~/backups/backup_username-%d-%m-%Y.tar.gz ~/public_html

Don't forget to be given 700 access rights! The command rm -rf ~ / backups / * means, the backup file the day before will be deleted.

What if you want to do weekly or monthly backups?

Backups can be done weekly or monthly. We recommend doing weekly backups if your hosting capacity is sufficient. Because if there is an error from time to time, there will be a backup file.

Weekly Backups

To perform weekly backups, please create a "weekly" folder in the backups folder earlier. Then create a script backupweekly.sh. After that, fill it with:

 

#!/bin/bash

rm -rf ~/backups/weekly/*

tar -czvf ~/backups/weekly/backup_username-%d-%m-%Y.tar.gz ~/public_html

Then set it in the Cron Menu. In the Common Settings section, please select Once Per Week. Then in the Command section, please write ~/backups/backupweekly.sh. Then click Add New Cron Job.

how to backup daily with cronjob

Monthly Backups

To do a monthly backup, please create a "monthly" folder in the backups folder earlier. then create a script backupmonthly.sh. After that, fill it with:

 

#!/bin/bash

rm -rf ~/backups/monthly/*

tar -czvf  ~/backups/weekly/backup_username-%d-%m-%Y.tar.gz ~/public_html

Then set it in the Cron Menu. In the Common Settings section, please select Once Per Month. Then in the Command section, please write ~/backups/backupmonthly.sh. Then click Add New Cron Job.

how to backup daily with cronjob

Database Backup

Before doing a database backup, make sure you have a super package hosting and SSH it first. To back up the database, you can follow the following command.

 

mysqldump -u nama_user -p'password' nama_database > file.sql

Conclusion

How to backup daily with cronjob is very easy. All you need to do is create a backup script then adjust the scheduling at cronjob. You can adjust as you wish. For example, one per day (1 time a day), once per week (1 time a week) or once per month (1 time a month). Customize backups as desired. Oh yes, because this backup file will be stored in the hosting, make sure your hosting capacity is full! To upgrade your hosting, you can follow the Hosting Upgrade Guide

Also Read: When Should I Upgrade Hosting?

What is Safelink Asia?

Safelink.asia is a completely free tool where you can create short links. URL security with cryptographic algorithm. Protect your web visitors with a secure link, to avoid viruses, malware, thief, etc. Apart from being free, you get paid! Register now!


Shorten URLs & earn money

So, now you can make money from home, when managing and protecting your links with Safelink Asia. Signup for an account in just 1 minutes. Once you've completed your registration just start creating short URLs and sharing the links with your family and friends. See our proof of payment and publisher rate.


Our best partner

The following are some of our best partners who have worked together on advertising: 401XD Group, KM Panel, MC Project, My Coding, Ilmunesia, Blackexpo Indo, Senja Teduh, Business Media.