Last active
February 1, 2025 17:55
-
-
Save bdeleasa/dccfff9c1a79a379315615ff80084abf to your computer and use it in GitHub Desktop.
A command for DDEV which takes a backup of either the database, files OR a full backup. Drop into your .ddev/commands/web folder and modify accordingly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# A simple backup script to back up the database, files, or both. | |
# | |
# Usage: ddev backup {db|files|full} | |
# | |
# db: Export the database and save it to the backups/db directory. | |
# files: Zip the public_html folder and save it to the backups/files directory. | |
# full: Export the database, save it to the site folder, zip the | |
# site folder, and save it to the backups/full directory. | |
# Database backup is removed from site folder after zipping. | |
# | |
# Useful for creating backups of your site before making changes. | |
# This is an alternative to the ddev snapshot or ddev export commands. | |
# It is useful if you need to send a copy of the site to someone else, | |
# or create routine full snapshots of the site with both the files and database, | |
# which you can't do with the ddev snapshot command since that only | |
# includes the database. | |
# | |
# Define the project vars | |
current_date=$(date +"%Y-%m-%d_%H-%M-%S") | |
project_root="/var/www/html" | |
docroot=${DDEV_DOCROOT:-public_html} | |
site_root="$project_root/$docroot" | |
db_folder="$project_root/backups/db" | |
files_folder="$project_root/backups/files" | |
full_folder="$project_root/backups/full" | |
# Define the database and zip backup paths. | |
db_backup_file="${DDEV_SITENAME}_db-backup_$current_date.mysql.gz" | |
db_backup_file_path="$db_folder/$db_backup_file" | |
files_backup_file="${DDEV_SITENAME}_files-backup_$current_date.zip" | |
files_backup_file_path="$files_folder/$files_backup_file" | |
full_backup_file="${DDEV_SITENAME}_full-backup_$current_date.zip" | |
full_backup_file_path="$full_folder/$full_backup_file" | |
# Create the backups directories if they don't exist | |
echo "Step 1: Creating directories..." | |
mkdir -p "$db_folder" "$files_folder" "$full_folder" | |
echo "Success! Created necessary directories." | |
# Function to back up the database | |
backup_db() { | |
echo "Step 2: Exporting database..." | |
mysqldump --no-tablespaces -u db -pdb --single-transaction db | gzip > "$db_backup_file_path" | |
echo "Success! Database backup saved to $db_backup_file_path" | |
} | |
# Function to back up the files | |
backup_files() { | |
echo "Step 3: Zipping files..." | |
cd "$project_root" || exit 1 | |
zip -r "$files_backup_file_path" "$docroot" >/dev/null | |
echo "Success! Files backup saved to $files_backup_file_path" | |
} | |
# Function to back up both the database and files | |
backup_full() { | |
backup_db | |
echo "Step 4: Bundling everything up..." | |
cd "$site_root" || exit 1 | |
mv "$db_backup_file_path" "$site_root" | |
zip -r "$full_backup_file_path" "." >/dev/null | |
echo "Success! Full backup saved to $full_backup_file_path" | |
rm -f "$site_root/$db_backup_file" | |
rm -f "$db_backup_file_path" | |
echo "Success! Removed the temporary database backup file." | |
} | |
# Check the argument and perform the corresponding backup | |
case "$1" in | |
db) | |
backup_db | |
;; | |
files) | |
backup_files | |
;; | |
full) | |
backup_full | |
;; | |
*) | |
echo "Usage: $0 {db|files|full}" | |
exit 1 | |
;; | |
esac | |
# Completion message | |
echo "Backup process completed!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment