Focus on cloud service provider activities
Notes on website operation and maintenance

Method of automated backup of ECS database using custom script tools

By the end of the year, many of our companies had begun to take vacations, but as operation and maintenance personnel, we must always pay attention to the operation of the company's website and projects in charge of customers, At the same time, a few days ago, Lao Zuo also checked whether the client server and domain name he was responsible for were renewed in advance when they expired during the Spring Festival. Generally, the update data of the website is not large during the Spring Festival, so the image backup and local data backup are done uniformly.

The files of general websites are rarely changed. Unless the template data is modified, the static files are also separated from the third-party storage, so the security is relatively high. The most important thing is the database. If some of our customers use cloud databases, automatic backup is enabled, so I don't care about this, For other databases, if the WEB panel used by the customer has its own backup function, I will push them to a third party to store the backup database. For other databases, I can use scripts to customize the backup and upload them to the backup server synchronously.

First Database backup script

#!/ bin/bash
tool=/usr/bin/mysqldump
dd=`date +%Y%m%d`
Username=root # Database user name
Password=laozuo. org # Database password
Database=laozuo # Database name
backup_count=2
Backup_dir=~/mysqlbackup # Backup database directory
#Create backup directory
mkdir -p $backup_dir
#Create a backup mysqldump - u root - p123456 users>/root/mysqlbackup/users - $filename.sql
$tool -u $username -p$password $database > $backup_dir/$database-$dd.sql
#Write backup log
echo "create $backup_dir/$database-$dd.sql" >> $backup_dir/action.log
#Find out which backups need to be deleted
delfile=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | head -1`
#Determine whether the current number of backups is greater than $backup_count
count=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | wc -l`
if [ $count -gt $backup_count ] then
Rm $delfile # Delete the oldest backup, and only retain the backup_count
#Write Delete File Log
echo "delete $delfile" >> $backup_dir/action.log
fi

For this file, we can set the name backupdata.sh, and need to modify it to our own database parameters.

Second, execute the backup effect

We can directly drop it to the corresponding directory and execute the script first to see if it can take effect. If it can be backed up to, then we indicate that the script is effective.

Third, set scheduled backup rules

zero zero * * * / backup / backupdata. sh > / dev / null two > &1 &
We can add a timing rule to set the backup in the early morning. Of course, if the data is not updated much, we should not back it up. For example, if most of our company's websites are enterprise websites, no one will update them during the New Year, so I have backed them up in these days. Let's talk about it after the New Year. Even if there is a problem, my local backup can be restored.
Domain name host preferential information push QQ group: six hundred and twenty-seven million seven hundred and seventy-five thousand four hundred and seventy-seven Get preferential promotion from merchants.
Like( zero )
Do not reprint without permission: Lao Zuo's Notes » Method of automated backup of ECS database using custom script tools


Scan the code to follow the official account

Get more news about webmaster circle!
Entrepreneurship, operation and new knowledge