When using WordPress, you must be careful about security.In terms of security measures, it is important to prevent the intrusion of attackers, but just as important is whether a backup system is in place to quickly restore the attacked site to its original state .If you back up your database regularly, even if your website is tampered with by a malicious third party, you can quickly restore the contents before the tampering .This time, I will introduce how to back up the database used by WordPress using the “script periodic execution tool” function of the shared rental server “ACE01” .
Prepare a script to make a backup of your database.MySQL and PostgreSQL are installed as standard on the shared rental server “ACE01”, but this time it is the procedure when using the MySQL database recommended by WordPress .
1.1 Acquisition of database name and login information
First , find out in advance the name of the database used by WordPress and the user name and password to log in to the database .If you don’t know , open wp-config.php directly under the WordPress installation directory with a text editor and check the following location.wp-config.php/ ** Database name for WordPress * /
define (‘DB_NAME’,’ai ●●●● kw9c_wordpress’);
/ ** MySQL database user name * /
define (‘DB_USER’,’ai ●●●● kw9c ‘);
/ ** MySQL database password * /
define (‘DB_PASSWORD’,’ ●●●●●●●●●’);( ‘DB_NAME’ , ‘ai ●●●● kw9c_wordpress’ );( ‘DB_USER’ , ‘ai ●●●● kw9c’ );( ‘DB_PASSWORD’ , ‘●●●●●●●●●’ );
“DB_NAME” is the database name, “DB_USER” is the user name, and “DB_PASSWORD” is the user password, respectively.
1.2 Creating a script
First, create a directory (db_backup) in your home directory to store backup files in advance.mkdir ~ / db_backup~ / db_backup
The “~ /” in the above command means your home directory, so you can execute the command anywhere. After executing the above command, check if “db_backup” is created in your home directory.Next, create a new PHP script for regular backup directly under the WordPress installation directory.wp-teiki-backup.php (newly created)<? phpphp// Backup file name$ file_name = ‘teiki-backup_’ . date ( ‘YmdHis’ ) . ‘ . sql ‘ ;exec ( ‘/ usr / local / bin / mysqldump -h 127.0.0.1 –u [username] –p [password] [database name]> / usr / home / <server ID> / db_backup /’ . $ File_name );
Let’s run a script to see if the backup is successful.cd ~ / html / <WordPress installation directory> ~ / html / < WordPress installation directory>
php-7.1 wp-teiki-backup.php– 7.1 Wp – Teiki – Backup . PhpIf the backup is successful, the backup file will be added to the storage directory created earlier.cd ~ / db_backup ~ / db_backup
~~ Output result ~~
-rw-r–r– 1 ai ●●●● kw9c cst 185855 Oct 3 14:50 teiki-backup_20191003054951.sql– LRw – R – R – 1 Ai ●●●● Kw9c Cst 185855 Oct 3 14 : 50 Teiki – Backup_20191003054951 . Sql
2. Setting of “Script periodic execution tool”
Set the script created in 1. to be executed periodically using the “Script periodic execution tool” function of the shared rental server “ACE01” .Log in to the web control panel and select “Public site settings” → “Script periodic execution tool”.Please refer to the photo and set as follows-For “New registration” , select the file (wp-teiki-backup.php) created earlier . -Select 7.1 or higher for
“PHP version” . -Select the date and time for backup for “Execution schedule” . (Example: Every Sunday at 3:50 am)When all settings are complete, click the “Add” button.
3. Restore the database
So far, we have introduced how to create a backup file, but now we will show you how to restore the database using the backed up file .Execute the following command.cd ~ / db_backup ~ / db_backup
mysql -h 127.0.0.1 –user = [username] –password = [password] [database name] <teiki-backup_2019 ●●●●●●●●●● .sql– H 127.0 . 0.1 – User = [user name] – Password = [password] [database name] < Teiki – Backup_2019 . ●●●●●●●●●● SqlThe user name, password and database name are the same as those obtained in 1.1.
Specify the backup file name at the end of the command.When you restore, the current WordPress contents will be restored to the contents when the backup was taken.
Once you restore, the contents of WordPress before (current) restoration will be completely lost, so please execute the command carefully.
This time, I introduced how to make a backup of the database and how to create a script and execute it regularly.It is possible to take backups every day and every hour depending on the setting of periodic execution, but the more frequently you create backup files, the more storage (disk) usage of the server will increase and the free space will decrease . We recommend once or twice a week.You can easily set the automatic execution by using the periodic execution script even for the backup work that is troublesome to do manually like this.