Exporting a database over SSH typically involves using the `mysqldump` or `pg_dump` command, depending on whether you are dealing with a MySQL or PostgreSQL database. SSH is used to securely access the server where the database resides and execute the export command. Here are the general steps to export a database over SSH:
1. Connect to the server via SSH.
- Open your terminal or SSH client.
- Use the `ssh` command to connect to the remote server. Replace `username` with your server’s username and `example.com` with the server’s hostname or IP address.
ssh username@example.com
2. Navigate to the database tools.
- Once connected, navigate to the directory where your database tools are located. This might involve using the `cd` command to change directories.
3. Export the database.
For MySQL:
- Use the `mysqldump` command to export the MySQL database. Replace `database_name` with the name of the database you want to export and `output_file.sql` with the desired name for the export file.
mysqldump -u username -p database_name > output_file.sql
- You will be prompted to enter the MySQL user’s password.
For PostgreSQL:
- Use the `pg_dump` command to export the PostgreSQL database. Replace `database_name` with the name of the database you want to export and `output_file.sql` with the desired name for the export file.
pg_dump -U username -d database_name -f output_file.sql
- You will be prompted to enter the PostgreSQL user’s password.
4. Exit the SSH session.
- After the export is complete, you can exit the SSH session by typing: exit
5. Retrieve the exported file.
- You can now retrieve the exported SQL file from the remote server to your local computer using SCP (Secure Copy) or another file transfer method.
scp username@example.com:/path/to/output_file.sql /local/path/
- Replace `/path/to/output_file.sql` with the path to the exported file on the server and `/local/path/` with the desired destination on your local computer.
That is it! You have successfully exported a database over SSH. The exported SQL file can now be used for backup, migration, or other database-related tasks.