Backup the database

We will connect to the MySQL container for this.

Create a file that will store our backup command: touch export_sql_db.sh
Put this in the file:

docker exec wordpressmysql_mysql_wordpress_1 /usr/bin/mysqldump -u <user> --password=<your_pass> <the_db_name> > /tmp/my_wp_database_backup.sql

(It is recommended to create a user specifically for backing up as that user won’t have unnecessary privileges)

Make this file executable with: sudo chmod +x export_sql_db.sh

Lets schedule it: sudo crontab -e
Add a line like this:
38 8 * * * /path/somewhere/export_sql_db.sh

Backup the HTML files and such

We will connect to the actual WordPress container for this.

Create a file that will store our backup command: touch export_website_files.sh
Put this in the file:

rm -rf /tmp/wordpress_site && rm -rf /tmp/wordpress_site.tar && docker cp wordpressmysql_wordpress_1:/usr/www/users/leigho /tmp/wordpress_site && \
tar -cf /tmp/wordpress_site.tar /tmp/wordpress_site

Make this file executable with: sudo chmod +x export_website_files.sh
Lets schedule it: sudo crontab -e
Add a line like this:
41 8 * * * /path/somewhere/export_website_files.sh