Set Up Website Backup From cPanel Automatically [No Worries About Data Loss]

Thiết Lập Backup Website Từ cPanel Tự Động [Không Lo Mất Dữ Liệu]

Losing all website data is a real nightmare. I had a "heart attack" once because of a hosting error. But you don't have to live in such fear. There is an extremely simple way to set up "insurance" for your website, it only takes 15 minutes to install once with Cron Job in cPanel so you can sleep well every night. Let's review this whole experience with Pham Hai to know how to not lose cPanel website data right here.

Instructions for installing automatic cPanel website backup from A-Z with Cron Job

As of cPanel's latest updates in 2026, setting up cPanel automatic backups with Cron Job is still a method of using a PHP script combined with a scheduler on the server to automatically compress source code and export the database periodically. This method helps you immediately have a free cPanel automatic backup software without having to install heavy plugins that slow down page loading speed.

Why use Cron Job and not the built-in Backup feature?

Using Cron Job helps to completely automate backups according to your desired schedule (daily, weekly) without having to manually log in to the cPanel control panel each time a backup is needed.

The system's default Backup feature often requires you to operate it manually. If you are busy and forget, the risk of data loss due to a network attack or hosting error is very high. By setting up Cron Job, the system will automatically run in the background. This is the most effective way to Auto backup website automatically every day that I apply to every project. It is especially useful when you want to continuously protect website data without wasting effort. Furthermore, for websites with large amounts of data, automating backups at night will not affect the visitor experience during the day.

Step 1: Prepare PHP Script to automatically compress source code and database

Bước đầu tiên trong hướng dẫn sao lưu website cPanel từng bước là tạo một file PHP (ví dụ: backup.php) chứa các lệnh thực thi nén thư mục public_html và xuất database MySQL.

Bạn có thể sử dụng đoạn mã PHP cơ bản dưới đây. Đoạn mã này sử dụng hàm exec() trong PHP để chạy lệnh nén file .tar.gz trên server Linux và lệnh mysqldump để tạo file .sql.

<?php
// Thông tin cấu hình Database
$dbhost = 'localhost';
$dbuser = 'ten_user_db';
$dbpass = 'mat_khau_db';
$dbname = 'ten_database';

// Cấu hình đường dẫn và tên file backup
$date = date("Y-m-d_H-i-s");
// Lưu ý: Thay 'your_username' bằng username cPanel của bạn
$db_backup_file = "/home/your_username/backups/db_".$date.".sql.gz";
$file_backup = "/home/your_username/backups/files_".$date.".tar.gz";

// Lệnh thực thi backup
$cmd_db = "mysqldump --opt -h $dbhost -u $dbuser -p$dbpass $dbname | gzip > $db_backup_file";
$cmd_files = "tar -czf $file_backup /home/your_username/public_html";

// Chạy lệnh
exec($cmd_db);
exec($cmd_files);
echo "Quá trình backup đã hoàn tất thành công!";
?>

Bảng giải thích các thông số trong Script:

Parameter Meaning Need a change?
$dbuser, $dbpass Database login name and password Yes (Enter correct web information)
$dbname Website database name Have
your_username Your cPanel hosting account username Have

Đoạn script nhỏ gọn này chính là cốt lõi để Backup restore database MySQL tự động và nén toàn bộ mã nguồn một cách trơn tru. Bạn có thể dùng Notepad hoặc bất kỳ trình soạn thảo văn bản nào để tạo file này và lưu lại với đuôi .php.

Step 2: Upload the script to the hosting root directory via File Manager

Sau khi có file backup.php, bạn đăng nhập vào cPanel, mở File Manager và tải file này lên một thư mục bí mật (không nằm trong public_html) để đảm bảo an toàn.

Working with File Manager is very intuitive and easy to operate. If you are not familiar with this interface, you can read the article cPanel user guide for newbies to clearly understand the management areas. Below are the detailed steps:

  1. Log in to the hosting administration system.
  2. Find the Files section and click File Manager.
  3. Điều hướng đến thư mục gốc (thường là /home/your_username/). Lưu ý tuyệt đối đừng vào trong public_html nhé.
  4. Tạo một thư mục mới tên là scripts ngang hàng với public_html.
  5. Nhấn nút Upload ở thanh công cụ phía trên và chọn file backup.php vừa tạo.
  6. Đừng quên tạo thêm một thư mục tên là backups cũng tại thư mục gốc để script có nơi lưu trữ các bản sao lưu sau khi chạy.
  7. Đặt quyền (CHMOD) cho file backup.php là 0755 để server cấp quyền thực thi nó.

Step 3: Configure Cron Job in cPanel to run the script file periodically

Access the Cron Jobs section in cPanel, set the backup frequency (for example, every night at 2am) and enter the command to execute the PHP file to trigger the backup process.

Scroll down to the Advanced section in cPanel and click Cron Jobs. In the "Add New Cron Job" section, you will see time options. I recommend that you select "Once Per Day" at dawn (for example 2:00 AM) so as not to affect web speed when there are many visitors.

Tại ô Command (Lệnh), bạn nhập đường dẫn thực thi PHP như sau: php -q /home/your_username/scripts/backup.php

Click Add New Cron Job and you're done. This is an extremely gentle and accurate way to automatically backup cPanel websites. You don't need to remember the schedule, the system will do it for you.

Step 4: Check again and receive the backup file automatically sent to email (or saved in the specified folder)

Sau khi thiết lập, bạn có thể chạy thử lệnh Cron Job hoặc chờ đến giờ hẹn, sau đó kiểm tra trong thư mục backups xem file .tar.gz và .sql đã được tạo thành công hay chưa.

In the Cron Jobs section, you can also enter your email address in the "Cron Email" section. The system will send a notification every time it completes or report an error if it fails. Receiving notifications helps you proactively control the situation. At Pham Hai, we always require the technical team to check this log regularly. A small note is that the backup process may fail if your hosting is full (exceeds data quota). Therefore, always leave at least 30-40% of your hosting capacity empty.

"Traditional" backup methods and use cases

In addition to using Cron Job, cPanel provides the cPanel Backup Wizard tool that allows you to perform Full Backup or Partial Backup manually in an intuitive way, suitable for unexpected backup needs.

Full Backup (Full Backup) with cPanel Backup Wizard: Quick but easy to forget

Full Backup will package the entire hosting account including Home Directory, MySQL Database, email forwarders and configuration into a single compressed file, very useful when you want to switch hosting provider.

To perform a full backup of cPanel website, go to Files > Backup Wizard > Backup > Full Backup. Here, you select "Home Directory" as the storage location (Backup Destination) and enter your email to receive notification when completed. Then click "Generate Backup".

However, this Full Backup version has a drawback: you cannot use it to restore directly from the regular cPanel interface but must rely on the root rights of the server administrator (Web Host Manager - WHM). This method is great for monthly archiving, but because it has to be done manually, it's easy to forget.

Partial Backup: When do you only need to back up Home Directory or MySQL Database?

Partial Backup allows you to individually download Home Directory (source code, images) or backup mysql cPanel database, suitable for quick download to personal computer for minor editing.

If you just posted a new article or have a few new orders, you don't need a full backup. At this point, just go to Backup Wizard, select Partial Backup and download the MySQL Database section. Or you can also use the phpMyAdmin export tool to export individual .sql files.

Đối với những ai đang tìm cách backup wordpress trên cPanel, việc sao lưu riêng thư mục wp-content trong Home Directory và Database là cách làm tối ưu nhất để tiết kiệm thời gian và dung lượng. Để đảm bảo an toàn toàn diện cho trang web, bên cạnh việc sao lưu, bạn nên kết hợp với các giải pháp bảo mật website wordpress chuyên sâu như cài đặt tường lửa hoặc đổi đường dẫn đăng nhập.

Personal experience: How do I combine both automatic and manual backups safely?

My experience is to use Cron Job to automatically backup the Database daily, and perform a manual Full Backup once a month before updating the system core.

Integrating multi-layer backup helps minimize risks. I once encountered a case where the file automatically crashed because the hosting suddenly ran out of space. Thanks to the manual Full Backup saved on my computer's hard drive last week, I saved the entire project. For websites using open source code, backup wordpress website regularly using both of these methods is a vital principle that any webmaster must remember. Backup frequency depends on how often your content is updated, but "be careful, don't worry", it's better to have excess than loss.

Storing and restoring backups: An important step that cannot be overlooked

A backup is only truly valuable when it is stored in a safe place outside the hosting and you clearly understand the process of restoring website data from cPanel (restore data) when something goes wrong.

Never save backups on the same hosting! Suggested safe storage locations (Google Drive, Dropbox...)

Storing a secure website backup means pushing the backup file off the current server to cloud storage services like Google Drive, Dropbox or Amazon S3.

Nếu server bị hỏng ổ cứng vật lý hoặc dính mã độc tống tiền (ransomware), toàn bộ file trên hosting (kể cả thư mục backups bạn vừa tạo) đều sẽ "bốc hơi" hoặc bị mã hóa. Tại Phạm Hải, chúng mình khuyên bạn nên tải file về máy tính định kỳ hoặc dùng các script nâng cao để tự động đẩy file nén lên Google Drive. Đây là lớp phòng thủ cuối cùng chống lại mọi thảm họa.

Gợi ý một số nền tảng lưu trữ đám mây phổ biến:

Foundation Advantage Free capacity
Google Drive Easy to use, good sync 15GB
Dropbox Fast upload/download speed 2 GB
OneDrive Built-in on Windows 5GB

Detailed instructions on how to restore website from .tar.gz file and .sql file when encountering problems

To restore data, you need to extract the .tar.gz file overwriting the public_html folder via File Manager, and use the phpMyAdmin tool to re-import the .sql file into the database.

When your website has a blank page or is hacked, don't panic. Please calmly follow these quick data recovery steps:

  1. Khôi phục mã nguồn: Truy cập File Manager, đổi tên thư mục public_html cũ thành public_html_old (để dự phòng). Tạo một thư mục public_html mới tinh. Upload file .tar.gz của bạn lên đó và nhấn chuột phải chọn Extract (Giải nén).
  2. Khôi phục Database: Truy cập phpMyAdmin từ giao diện cPanel. Chọn đúng Database của website ở cột bên trái. Nhấn vào tab Import (Nhập), chọn file .sql (hoặc .sql.gz) đã backup từ máy tính và nhấn Go (Thực hiện).
  3. Kết nối lại: Kiểm tra file cấu hình (ví dụ wp-config.php đối với WordPress) để đảm bảo thông tin user và mật khẩu kết nối database vẫn chính xác.

With just the 3 steps above, your website will return to normal operation as before the problem occurred.

Some "deadly" mistakes to avoid when managing backup files

Not checking the backup file to see if it works, saving the database password in unencrypted text in a visible place, and leaving the backup file public on the internet are extremely dangerous mistakes.

Many people leave it alone after configuring backup automation. When I need to take it out and use it, the compressed file is corrupted and cannot be decompressed. Make it a habit to download the file and unzip it once a month on localhost.

Ngoài ra, tuyệt đối không lưu file backup trong thư mục public_html vì bất kỳ ai có link trực tiếp hoặc dùng tool quét thư mục cũng có thể tải trộm toàn bộ mã nguồn và dữ liệu khách hàng của bạn. Hãy luôn lưu ở thư mục gốc (Home Directory) hoặc đẩy lên lưu trữ đám mây nhé.

Việc backup website từ cPanel tự động không phải là kỹ thuật gì cao siêu, nó là "bảo hiểm" rẻ và hiệu quả nhất cho tài sản số của bạn. Don't wait until you "lose the cow to build a barn". Hãy dành ra một ly cafe và 15 phút thiết lập ngay hôm nay để không bao giờ phải đối mặt với rủi ro mất dữ liệu oan uổng. Website safety is also peace of mind for you.

If you encounter any difficulties during the installation process, don't hesitate to leave a comment below, I will assist as soon as possible!

Lưu ý: Thông tin trong bài viết này chỉ mang tính chất tham khảo. Để có lời khuyên tốt nhất, vui lòng liên hệ trực tiếp với chúng tôi để được tư vấn cụ thể dựa trên nhu cầu thực tế của bạn.

Categories: Affiliate Marketing Bảo Mật cPanel & DirectAdmin Hosting & VPS Kiếm Tiền Online Wordpress

mrhai

Để lại bình luận