By Ankit Mehta, Alibaba Cloud Community Blog author.
Application backup was and is the biggest challenge for the system administrator. With evolution of the cloud technology, people can use the snapshot of the system and use it as a backup. However there are many other ways with some tweaks and tools to make the backup process easier.
This blog post focuses on taking a backup of PHP - MySQL application, storing the backup on the OSS (Alibaba cloud Object Storage Service). This post covers some of the important aspects of the backup like types of backup, online vs offline backup and types of backup storages.
Backups are divided into three main types. Full, Differential and Incremental.
|Backup Type||Backup||Backup Time||Storage Space||Restore Time|
|Differential Backup||All data since last full backup||Medium||Medium||Fast|
|Incremental Backup||only new/modified files / folders||Fast||Low||Medium|
Full backup takes all the files and folder structure of the application and is considered slow. Full backup requires more disk space as compared with the other two types of backup types. As a best practice it is preferred to take complete backup of the application once a week.
Differential backup is taking the differences since the last full backup. Differential backup considered as quick and it requires less storage space. It is recommended to use the differential backup once a day.
Incremental backup backups up the files that are changed since the last backup. This is the most space effective backup and quick backup type. Incremental backup can be taken more than once in a day without impacting the server performance.
Some other types of backup are Cold, Warm and Hot backups. These type of backup are mainly considered for the server rather than the application.
Cold backup ensures the consistency of the backup. During the cold backup the application should not be in use and no read or write operations are allowed during the backup period. In most cases during the cold backup, servers are offline and complete system is backed up.
Warm backup can be applied when the server is on but will not allow to apply any changes to the system.
Hot backups can be applied even when the application is in use, however it does not guarantee any data consistency in the case of data change during the backup process.
Apart from the above three types, backups can also be categorized based on the storage location. If the backup is stored locally on the server or system, it is considered as the local backup and if it is stored on a remote location then it is considered as the remote backup. From the disaster recovery perspective it is recommended to use the remote backup. Local backups can help recovering the application faster as compared with the remote backup.
Object Storage Service (OSS) is secure cloud storage service offered by Alibaba Cloud. OSS can be used to store your application backup securely in the same region or the remote region based on the choices.
Alibaba cloud OSS has three storage classes as mentioned below.
1. Standard Storage: Standard storage is the best available option for the OSS without any data lockin period. Daily application backup - Incremental or differential backup can be stored under the standard storage class.
2. Infrequent Storage: Infrequent storage can be used for the weekly backup. This storage class comes with a lockin period where minimum 30 days of the file storage is needed. Note that, once the file is stored, modification of the file within 30 days is penalized and penalty charges are applied.
If the files require frequent access then Standard Storage is the best choice.
3. Archive Storage: As its name implies, Archive Storage is the best fit to archive the backup for longer period. This is one of the cheapest If the backup requires to store for more than 60 days.
Following table gives storage class choice based on the backup type.
|Backup Type||Backup Class||Suggested Frequency|
|Full Backup||Infrequent / Archive||Weekly|
|Incremental Backup||Standard||Daily / Hourly|
Above two sections gave you an idea of the backup types and storage classes to store the backup files. Let's jump in to start taking the application backup and storing them on the appropriate storage classes.
For the demonstration purpose we are using a PHP - MySQL application hosted on Alibaba Cloud Elastic Compute Service (ECS). This section is divided into three subsections,
1. Manual Application Backup:
To backup a mysql database you can run the following command as root user
mysqldump <db_name> > backup_dbname.sql
If the root access is not available then the following command can be supplied with username and password.
mysqldump -u <username> -p <dbname> > backup_dbname.sql
Take a compressed application backup using the tar command
tar -czf <archive_name> -C <path_to_remove> .
Above command will create a tar compressed archive of the website code. The -C parameter helps in removing the absolute path from the compressed folder and update the absolute path with . (root)
Above scenarios are taking a full backup of the application and database. To take the incremental or differential backup of the MySQL database binary method can be used. In the case of bigger databases it is recommended to use the incremental backup.
For the file backups rsync with the following configuration
1. Daily Backup:
rsync -av --delete <source_path> <daily_backup_destination_path> tar -cvjf daily_$(date +%Y%m%d).tar.bz2 <daily_backup_destination_path>
2. Weekly Backup:
rsync -av --delete <daily_backup_destination_path> <weekly_backup_destination_path> tar -cvjf weekly_$(date +%Y%m%d).tar.bz2 <weekly_backup_destination_path>
3. Monthly Backup:
tar -cvjf monthly_$(date +%Y%m%d).tar.bz2 <daily_backup_destination_path>
As the backups are ready, it can be moved to the OSS for secure storage. Following steps will help in configuring the OSS on ECS.
1. Get a latest copy of OSSutil based on OS architecture
2. Give executable permissions to OSSUtil
3. Create a Standard bucket by navigating to OSS section under Alibaba Cloud console
4. Create a user with Programmatic Access under Resource Access Manager section in Alibaba Cloud Console
5. Download AccessKey and AccessSecret for the user
6. Provide AliyunOSSFullAccess permission to the user
7. Copy the OSSUtil to /usr/sbin and give it executable permission. This will allow to execute OSSUtil commands from anywhere on the system
8. Configure the OSSUtil. Following informations are needed to configure the OSSUtil.
<blockquote>a. Bucket URL:</blockquote> <blockquote>b. Access Key:</blockquote> <blockquote>c. Access Key Secret:</blockquote>
The OSSUtil config command generated a .ossutilconfig file on the user root path and can be easily reconfigured as needed.
9. Validate that the configuration works well with following command
ossutil64 ls oss://<bucket-name>
As the server is configured to send the files to OSS bucket, as a last step, configure the scripts and automated jobs (cron jobs) to send the files to the server.
1. Structure Preparation: Following is the structure followed for the final configuration, where daily, weekly and monthly folder structures are prepared with folder named "Compressed". Compressed folders contains the files to be synced with the OSS bucket.
Following is the configuration at the bucket level.
2. Life Cycle Configuration : To make sure that data is retained as per the requirement, Lifecycle management needs to be applied to the bucket. Lifecycle management can be accessed from the basic settings of the bucket. Following lifecycle management rules will be applied to the bucket.
|Backup Type||Backup Class||Prefix||Auto Deletion|
3. Script Preparation: Following is the script to clone the website code and send it to the server.
The code clones the source code from the website directory to the backup/daily/code folder, compresses it and send it to the OSS bucket. Along with sending the file, it sets the Storage Class to Standard with --meta key.
Following is the script to take the database backup and store it to the OSS
Same way weekly and monthly scripts can be prepared and oss-Storage-Class can be configured accordingly.
Weekly backup script can be found here: https://gist.github.com/ankyit/0fda7907e3f884c4552762151914de33#file-aliyun-weekly-file-backup-sh
Monthly backup script can be found here: https://gist.github.com/ankyit/0fda7907e3f884c4552762151914de33#file-aliyun-monthly-file-backup-sh
4. Automating the Backups: To automate the backup, a cronjob can be configured. As a best practice it is preferred to use the off-peak hours for the backup. Following is the sample script.
For daily backup:
0 0 * * * /usr/bin/sh <daily-backup-script-path>.sh
For weekly backup:
0 0 * * 7 /usr/bin/sh <weekly-backup-script-path>.sh
For monthly backup:
0 0 1 * * /usr/bin/sh <monthly-backup-script-path>.sh
Hope, This tutorial will help you configure a remote backup for your application. Feel free to ask if you come across any issues while implementing the solution.
Ankit Mehta is an Alibaba Cloud MVP and an IT professional with more than 14 years of IT experience from application development to deployment and management. Currently working as a technical specialist and WordPress application development team lead at Aware Corporation Limited. Ankit strongly believes in "Sharing is caring" and help the open source communities by arranging meetups and sharing knowledge on various blogging platforms.
Alibaba Cloud MVP - November 22, 2019
Alibaba Clouder - September 14, 2020
Alibaba Cloud MVP - February 14, 2020
Alibaba Clouder - November 29, 2017
Alibaba Clouder - January 25, 2019
ApsaraDB - November 17, 2020
An encrypted and secure cloud storage service which stores, processes and accesses massive amounts of data from anywhere in the worldLearn More
Block-level data storage attached to ECS instances to achieve high performance, low latency, and high reliabilityLearn More
AnalyticDB for MySQL is a real-time data warehousing service that can process petabytes of data with high concurrency and low latency.Learn More
Plan and optimize your storage budget with flexible storage servicesLearn More
More Posts by Alibaba Clouder