[Tutorial - Backups] Commands for backing up your files and databases

Connect with other users about what to run on your webhosting (and how to run it) here.
Post Reply
sebinmichael
New to forums
New to forums
Posts: 1
Joined: Thu May 27, 2021 6:26 am

[Tutorial - Backups] Commands for backing up your files and databases

Post by sebinmichael » Thu May 27, 2021 6:46 am

Hi there!

A lot of you guys who are using shared hosting services here may be looking for a reliable way to have personal backups of your files and databases, so I decided to publish the commands here for your convenience. You can use these to manually backup your files and databases after which you can download and preserve these backups. If you wish to automate it, you can prepare a shell script for it. Sample Tutorial on Shell Scripts: https://www.guru99.com/introduction-to- ... pting.html

File Backup Commands:

Code: Select all

tar -cvpzf DESTINATION SOURCE
What this does is, it creates a .tar archive of all the files in your source directory and places it in your destination directory.

In case you want to have a full backup from the root folder of your user, you can exclude the backup folder itself by running this instead:

Code: Select all

tar --exclude='/path/to/your/backup/folder' -cvpzf DESTINATION SOURCE
Database Backup Commands:

Code: Select all

mysqldump -u YOURusername --password=YOURpassword --databases YOURusername_YOURdatabase --no-tablespaces | gzip > /path/to/your/backup/folder/FILENAME.sql.gz
What this does is, it creates a zipped MySQL dump (in .sql.gz format) of your database and ignores tablespaces to avoid an error you may run into due to an update pushed recently that requires certain additional parameters and consequently require you to inter alia have root access to run this command without ignoring the tablespaces. (Don't worry, it has no impact on your databases).

If you would rather not have your password saved within the command and prefer entering it manually every time, use this instead:

Code: Select all

mysqldump -u YOURusername -p --databases YOURusername_YOURdatabase --no-tablespaces | gzip > /path/to/your/backup/folder/FILENAME.sql.gz
In case you want to backup multiple databases, you can add their names separated by a space, such as:

Code: Select all

mysqldump -u YOURusername --password=YOURpassword --databases YOURusername_YOURdatabase1 YOURusername_YOURdatabase2 YOURusername_YOURdatabase3 --no-tablespaces | gzip > /path/to/your/backup/folder/FILENAME.sql.gz
Special thanks to John, CEO of NFOServers for helping me with troubleshooting and polishing these commands

Post Reply