Step-by-Step Guide: Ensuring Data Safety with Incremental/Daily Website Backups (For Cpanel server.)
In this guide, we will walk you through the steps to set up Incremental/Daily backups on your website, with a data retention period of 3 days. Additionally, we will automate the process of removing older backups, ensuring you always have the latest and most relevant data available.
Step 1: Choosing the Backup Frequency Decide on the frequency of your website backups. For this guide, we recommend taking backups daily.
Step 2: Preparing the Backup Storage Location Designate a secure backup storage location for your website data. This could be an external drive, a network-attached storage (NAS) device, or a remote server. Ensure that the storage has ample capacity to accommodate the backups for at least three days but here I am archiving it on the same machine for testing purposes only and the best practice is to attach the mounted drive to synchronize/transfer to the destination server.
In Source machine:-
Step 3: Implementing Incremental/Daily Backups Using a backup tool or script of your choice, schedule Incremental/Daily backups to run automatically. Incremental backups only save changes made since the last backup, reducing the backup time and storage space required. This approach optimizes efficiency while providing comprehensive data protection.
Step 4: Data Retention Policy Establish a data retention policy to manage the number of backups retained. For this guide, we will retain backups for 3 days. After three days, the oldest backup will be automatically removed to maintain the desired retention period.
Step 5: Automating Backup Cleanup To automate the backup cleanup process, incorporate a script or tool that identifies backups older than three days and removes them from the backup storage. This step ensures a streamlined backup repository with the most relevant data.
https://github.com/akdevops7/website_backup_script.git
OR
#!/bin/bash
Specify the backup directory
backup_dir="/backup/"
Function to perform the database backup for a user and database
perform_backup() { local user="$1" local db="$2" # Get the current date and time current_date=$(date +"%Y-%m-%d") current_time=$(date +"%H-%M-%S")
# Backup filename with timestamp backup_filename="${backup_dir}backup_${current_date}${current_time}${user}_${db}.tar.gz"
# Backup the database using cPanel's backup tool /usr/local/cpanel/bin/backup --user="$user" --output-dir="$backup_dir" --type=mysql --database="$db" --compression=gzip --file-prefix="$backup_filename" }
Store the count of existing backup files
existing_backups_count=$(find "$backup_dir" -type f -name "backup_*" | wc -l)
Loop through each cPanel user
for user in $(ls -A /var/cpanel/users/); do # Loop through each MySQL database for the user for db in $(mysql -e "SHOW DATABASES;" | grep -Ev "^(Database|information_schema|performance_schema|sys)$"); do perform_backup "$user" "$db" done done
Cleanup old backups (older than 3 days)
deleted_backups_count=$(find "$backup_dir" -type f -name "backup_*" -mtime +3 -exec rm {} ; -print | wc -l)
Send email notification if any backups were deleted during cleanup
if [ "$deleted_backups_count" -gt 0 ]; then echo "Important: $deleted_backups_count old backups were deleted during cleanup." | mailx -s "Local_Dev_Backup_Deletion_Warning (0.0.0.0)" xyz@example
abc@example.com
fi
Send email notification
echo "Daily_Backup_0.0.0.0 (Last 3days data backup availability)" | mailx -s "Local_Dev_Backup_Is_Successfully_Done (0.0.0.0)"
xyz@example.com
abc@example.com
Confirm that the script has executable permissions. You can grant executable permission using the following command:
#chmod +x /root/database_backup.sh
Step 6: Here is required to add script in cronjobs to schedule to execute task.
Run the script at 1:00 AM and with its 3days retenion policy in which keep always 3 backups.
0 1 1-3 /root/website_backup.sh > /dev/null 2>&1
Step 7: Next it's required to enable backup on WHM by providing basic steps and once the script will be executed by the schedule the backup will be stored in /backup
path.
Step 8: Monitor Backup Process | Verify backups, review logs, and ensure smooth operation.
Conclusion: Implemented reliable & efficient Incremental/Daily backup with 3-day retention secures your website data. Restoring to a known good state is now possible. Regularly review & update backup strategy to safeguard valuable data. Use remote/mount drive for best practice.
Done.
Thanks,
Anil Kushwaha
Subscribe to my newsletter
Read articles from Anil Kushwaha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Anil Kushwaha
Anil Kushwaha
As a technology enthusiast with a passion for efficient operations, I specialize in Linux, AWS Cloud, Prometheus-Grafana, Security, Virtualization, and Git. With a deep understanding of these technologies, I am able to streamline development processes and ensure smooth operations for businesses.