This guide covers backup and restore strategies for databases, files, and volumes in your development environment.
Export a specific database:
dev mysqldump database_name > backup.sqlWith compression:
dev mysqldump database_name | gzip > backup.sql.gzBackup all databases:
dev mysqldump --all-databases > all_databases.sqlBackup specific tables:
dev mysqldump database_name table1 table2 > tables_backup.sqlBackup structure without data:
dev mysqldump --no-data database_name > schema.sqlBackup data without structure:
dev mysqldump --no-create-info database_name > data.sqldev mysql database_name < backup.sqlFrom compressed backup:
gunzip < backup.sql.gz | dev mysql database_nameIf the database doesn't exist:
dev mysql -e "CREATE DATABASE database_name;"
dev mysql database_name < backup.sqldev myroot < all_databases.sqlNote: Use myroot for restoring all databases as it requires root privileges.
For MySQL 8 databases, use direct connection:
mysqldump -h 127.0.0.1 -P 3308 -u root -p database_name > backup.sqlmysql -h 127.0.0.1 -P 3308 -u root -p database_name < backup.sqlIf using local workspace directory:
cd ~/development
tar -czf workspace-backup-$(date +%Y%m%d).tar.gz workspace/Backup a single project:
cd ~/development/workspace
tar -czf project-backup-$(date +%Y%m%d).tar.gz customer/project/Exclude vendor and node_modules:
tar -czf project-backup.tar.gz \
--exclude='vendor' \
--exclude='node_modules' \
--exclude='.git' \
customer/project/If using Docker volumes instead of local directories:
docker run --rm \
-v dockerdev-workspace-volume:/data \
-v $(pwd):/backup \
ubuntu tar czf /backup/workspace-backup-$(date +%Y%m%d).tar.gz /dataBackup MySQL data directory:
docker run --rm \
-v dockerdev-mysql-volume:/data \
-v $(pwd):/backup \
ubuntu tar czf /backup/mysql-backup-$(date +%Y%m%d).tar.gz /datacd ~/development
tar -xzf workspace-backup-20241126.tar.gzdocker run --rm \
-v dockerdev-workspace-volume:/data \
-v $(pwd):/backup \
ubuntu bash -c "cd /data && tar xzf /backup/workspace-backup.tar.gz --strip 1"Create ~/bin/dev-backup.sh:
#!/bin/bash
BACKUP_DIR=~/backups
DATE=$(date +%Y%m%d)
mkdir -p $BACKUP_DIR
cd ~/development
# Backup all databases
./bin/dev mysqldump --all-databases | gzip > $BACKUP_DIR/databases-$DATE.sql.gz
# Backup workspace
tar -czf $BACKUP_DIR/workspace-$DATE.tar.gz \
--exclude='workspace/*/vendor' \
--exclude='workspace/*/node_modules' \
workspace/
# Keep only last 7 days
find $BACKUP_DIR -name "*.sql.gz" -mtime +7 -delete
find $BACKUP_DIR -name "*.tar.gz" -mtime +7 -deleteMake it executable:
chmod +x ~/bin/dev-backup.shAdd to crontab (crontab -e):
# Daily backup at 2 AM
0 2 * * * ~/bin/dev-backup.shcd workspace/customer/project/htdocs
# Database
dev mysqldump magento2 | gzip > ../backups/db-$(date +%Y%m%d).sql.gz
# Media files
tar -czf ../backups/media-$(date +%Y%m%d).tar.gz pub/media/
# Configuration
cp app/etc/env.php ../backups/env.php.backupcd workspace/customer/project/htdocs
# Database
gunzip < ../backups/db-20241126.sql.gz | dev mysql magento2
# Media
tar -xzf ../backups/media-20241126.tar.gz
# Configuration
cp ../backups/env.php.backup app/etc/env.php
# Clear cache
dev console bin/magento cache:flushAlways backup before:
- Upgrading PHP versions
- Updating major dependencies
- Significant code changes
- Docker environment updates
Essential:
- Databases
- Custom code
- Configuration files
- User-uploaded content
Optional:
- Vendor dependencies (can be reinstalled)
- Generated files (can be regenerated)
- Cache (not needed)
Store backups:
- On a different disk/partition
- In cloud storage (S3, Dropbox, etc.)
- On external drives
- Multiple locations for critical data
Periodically test your backups:
# Test database restore
dev mysql test_db < backup.sql
dev mysql -e "SHOW TABLES FROM test_db;"
dev mysql -e "DROP DATABASE test_db;"# Quick database backup
dev mysqldump mydb > quick-backup-$(date +%H%M).sql
# Quick file snapshot
tar -czf quick-backup-$(date +%H%M).tar.gz workspace/customer/project/Delete quick backups:
rm quick-backup-*.sql
rm quick-backup-*.tar.gzUse the full dev command:
./bin/dev mysqldump database > backup.sqlEnsure you have write permissions:
ls -la
chmod +w .Check available space:
df -hClean up old backups or compress existing ones.
Verify backup integrity:
gunzip -t backup.sql.gz
tar -tzf backup.tar.gz- mysql-mailhog-redis-cronjobs.md - Database basics
- mysql8.md - MySQL 8 specifics
- docker-volumes.md - Volume management