Originally, I used Hexo for my blog, but migrating between static blog platforms was cumbersome and data backup was not convenient. Therefore, I switched to using Mysql + Typecho dynamic blog. This article records the process of using a script to back up the relevant data of the blog and restore the Mysql data, which can be considered as a form of documentation and sharing.
1. Script Backup#
This script is a Typecho website data backup script that I found online. I made a few modifications to the script, such as changing the naming rules and backup frequency. For convenience, I also added this script to Crontab for regular automatic backups. If the blog is not updated frequently, you can skip this step and manually backup the data. The script code and instructions are as follows:
Usage:#
Upload the script to your VPS and execute ./backup_typecho.sh /path/to/yoursite.com/
to start the backup process. Replace /path/to/yoursite.com
with the installation location of your blog program. The backup results will be stored in the /var/backups/typecho/yoursite.com/
directory (this directory can be customized).
Please note that you need to add write permissions to the /var/backups/
directory. If you want to add a Crontab automatic backup, switch to the /etc/
directory and edit crontab
to add a scheduled task. For example: 0 23 * * 6 root sudo /path/backup.sh /path/to/yoursite.com/
, which means the script will be executed every Saturday at 11 PM. Replace /path/backup.sh
with the location of the script and /path/to/yoursite.com/
with the root directory of your blog site (the installation location of the blog program).
# Set backup directory
backup_dir="/var/backups/typecho/"
function print_help(){
echo 'Usage: $shell dir_to_typecho'
}
function die(){
test -z "$1" || echo "$1"
exit 1
}
function parse_db(){
config_file=$2
db_key=$1
cat "$config_file" | grep -A 6 '$db' | grep '=>' | grep "$db_key" | awk -F "'" '{print $4}'
}
if [ "$#" -lt "1" ]
then
print_help
exit 1
fi
te_dir="$1"
backup_dir="$backup_dir`basename "$te_dir"`"
# Check if backup time is too frequent
min_time="43200" # 12H
flag="/tmp/last_backup_typecho_`echo $te_dir | md5sum | awk '{print $1}'`"
last_backup="0"
test -f $flag && last_backup="`ls -l --time-style=+%s "$flag" | awk '{print $6}'`"
delta_time=$(expr "`date +%s`" - "$last_backup")
test "$delta_time" -lt "$min_time" && die "Time from last backup is less than $min_time, skip this time"
# Initialize variables
te_config="$te_dir/config.inc.php"
te_usr_dir="$te_dir/usr"
# Initialize environment
test -f "$te_config" || die "Can not find config file: $te_config"
test -d "$backup_dir" || mkdir -p "$backup_dir" || die "Can not create backup dir"
db_host=$(parse_db 'host' "$te_config")
db_port=$(parse_db 'port' "$te_config")
db_user=$(parse_db 'user' "$te_config")
db_pass=$(parse_db 'password' "$te_config")
db_name=$(parse_db 'database' "$te_config")
# Backup the database
echo "Found database config: host=$db_host, port=$db_port, user=$db_user, pass=**** and database=$db_name"
echo 'Try to dump database....'
dump_target='/tmp/database.sql';
test -f "$dump_target" && rm "$dump_target"
mysqldump -h"$db_host" -P"$db_port" -u"$db_user" -p"$db_pass" "$db_name" > "$dump_target"
echo 'Dump done.'
# Backup the usr directory
echo "Try to tar usr dir..."
tar_target="/tmp/user.tar.gz"
test -f "$tar_target" && rm "$tar_target"
tar czvf "$tar_target" "$te_usr_dir"
echo "Tar done."
echo "Try to pack..."
md5sum "$dump_target" > "$dump_target.md5sum"
md5sum "$tar_target" > "$tar_target.md5sum"
backup_file="$backup_dir/`basename "$te_dir"`.`date +%Y%m%d`.tar.gz"
tar czvf "$backup_file" "$dump_target" "$dump_target.md5sum" "$tar_target" "$tar_target.md5sum"
# Clean up temporary files
rm $tar_target
rm "$tar_target.md5sum"
rm $dump_target
rm "$dump_target.md5sum"
touch "$flag"
echo "Backup to $backup_file done."
2. Data Restoration#
Since my blog is hosted on an Alibaba Cloud ECS server, I prefer using the lnmp one-click installation package to install the website environment. Therefore, the following method may have certain limitations, please refer to it accordingly. The specific steps are as follows:
Step 1: Use the lnmp vhost add
command to add the domain name. Usually, I add both the www
and non-www
versions, with the www
domain as the primary one. It is recommended to use the same database name as before to avoid unnecessary trouble.
Step 2: In the folder of the domain name, use the following commands to download, install, and enable the Typecho blog program. You can refer to this article: Cheap VPS+LAMP Setup+Blog One-Click Installation Tutorial.
wget https://typecho.org/downloads/1.1-17.10.30-release.tar.gz // Download the Typecho program archive
tar -zvxf 1.1-17.10.30-release.tar.gz // Extract the Typecho program archive
mv build/* . // Move the contents of the extracted build folder to the root directory of the domain
rm -rf build 1.1-17.10.30-release.tar.gz // Delete unnecessary files
Step 3: Enter the domain name in the browser and follow the on-screen instructions to install the Typecho blog. It is recommended to deploy HTTPS, HSTS, CDN, and other related configurations.
Step 4: Extract the previously backed up file and upload the database.sql
file to the VPS. Then execute the following command to import the original Typecho backend settings and article data.
mysql -u username -p password database_name < database.sql // For example: mysql -u root -p 123456 DATA < database.sql
At this point, the restoration process is basically complete. The remaining steps involve resolving any miscellaneous issues that may arise. If there are no issues, there is no need to make any further adjustments.
Additional Information: If you want to automatically synchronize the backup files from the server to a cloud drive on a regular basis, you can refer to: Rclone Periodic Sync of Data