In linux, the tar command is used most frequently to back up files. Let's take a look at how to back up files with ssh + tar and download the backup files to the local device.
Currently, most virtual hosts have automatic backup functions, but sometimes they cannot rely solely on the host. If the host is hacked and the backup file cannot be recovered, it will be miserable, therefore, it is better to depend on people. BKjia. c0m today teaches you how to back up your website. Here we use Hostgator as a foreign space.
Backup is generally divided into two parts: file backup and database backup. Here we will first introduce how to back up files. For details about database backup, see: how to back up a database using the SSH Mysqldump command
Let's talk about File backup first. Generally, a virtual host has a File management system. For Hostgator, there is a File Manager in it. Although we can use it to Compress folders, then we use the FTP tool to achieve our local backup goal, but if you have too many website files, it will lead to false positives. Therefore, we do not recommend using the file management system in CPANEL to back up bKjia. c0m. Here we will talk about packaging files with SSH commands.
First, make sure that your space has enabled the SSH function. If not, contact the Space Provider to enable the function and then use Putty to connect to our space. Enter the username and password, and Putty will open a window similar to the CMD command line. Then we use the TAR or ZIP command to package our website folder.
To use SSH to compress a specified folder:
TAR compression command
| The Code is as follows: |
Copy code |
Tar-czvf bKjia.c0m.tar./bKjia.c0m.net BKjia.c0m.tar is the compressed file name. ./BKjia.c0m.net is the target folder to be compressed
|
ZIP compression command
| The Code is as follows: |
Copy code |
Zip-r bKjia.c0m.zip bKjia.c0m.net BKjia.c0m.tar is the compressed file name. ./BKjia.c0m.net is the target folder to be compressed
|
How to decompress the package using SSH:
| The Code is as follows: |
Copy code |
Tar decompress the Tar compressed package command Tar xvfz bKjia.c0m.tar Unzip decompress the zip package command Unzip bKjia.c0m.zip
|
OK. At this time, your database has been backed up. Drag it to the local machine using FTP to back up the database.
For example, check the SSH Mysqldump command to back up the database.
First, make sure that your space has enabled the SSH function. If not, contact the Space Provider to enable the function and then use Putty to connect to our space. Enter the username and password, and Putty will open a window similar to the CMD command line. Then we use the TAR or ZIP command to package our website folder.
Data backup process:
| The Code is as follows: |
Copy code |
| Mysqldump-h mysql.forece.net-p mysql_dbname-u mysql_dbuser> bak. SQL |
Mysql.forece.net here is the address of your database server. If your database does not have a domain name, you can enter localhost directly. mysql_dbname is your database name, And mysql_dbuser is your database username, bak. SQL is the name of the script file of the database you have backed up (data can be recovered in the future ). Then press enter, prompting you to enter the database login password. after entering the password, press Enter. The password entered in Liunx is not displayed, so you just need to enter it. Be sure not to make a mistake!
OK. At this time, your database has been backed up. Drag it to the local machine using FTP to back up the database.
The process of importing (restoring) data:
In SSH command line mode, enter the following command to enter the database:
| The Code is as follows: |
Copy code |
Mysql-h mysql.forece.net-p mysql_dbname-u mysql_dbuser
|
Then press enter to prompt you to enter the database login password. after entering the password, press enter to display the MySQL operation prompt symbol, and then enter the following command:
| The Code is as follows: |
Copy code |
Source bak. SQL
|
Make sure that the bak. SQL file is in the current directory. If there is no problem, press enter to restore the data. Wait for a while...