-
-
Save jasondavis/3c5768eeca6504f40859addd8b4909a4 to your computer and use it in GitHub Desktop.
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 | |
| # 1: Define backup filenames with timestamps. | |
| db_backup_name="wp-db-backup-$(date +%d-%m-%Y-%H.%M).sql.gz" | |
| wpfiles_backup_name="wp-content-backup-$(date +%d-%m-%Y-%H.%M).tar.gz" | |
| # 2: Database connection info. | |
| db_name="wordpress" | |
| db_username="wordpress" | |
| db_password="wordpress123" | |
| # 3: Path to WordPress root directory and wp-content. | |
| wp_root_folder="/var/www/html/wordpress" | |
| backup_folder_path="/home/mahesh/backups/wordpress" | |
| # 4: Backup MySQL database. | |
| mysqldump --opt -u"$db_username" -p"$db_password" "$db_name" | gzip > "$backup_folder_path/$db_backup_name" | |
| # 5: Navigate to WordPress root and create a tarball of wp-content only. | |
| cd "$wp_root_folder" || exit | |
| tar -czf "$backup_folder_path/$wpfiles_backup_name" wp-content | |
| # 6: Delete all but 3 most recent database backups. | |
| find "$backup_folder_path" -maxdepth 1 -name "*.sql.gz" -type f -printf "%T@ %p\n" | sort -rn | awk 'NR>3 {print $2}' | xargs -r rm -- | |
| # 7: Delete all but 3 most recent wp-content backups. | |
| find "$backup_folder_path" -maxdepth 1 -name "*.tar.gz" -type f -printf "%T@ %p\n" | sort -rn | awk 'NR>3 {print $2}' | xargs -r rm -- | |
| === II nd Script == | |
| #!/bin/bash | |
| # 1: Define backup filenames with timestamps. | |
| db_backup_name="wp-db-backup-$(date +%d-%m-%Y-%H.%M).sql.gz" | |
| wpfiles_backup_name="wp-content-backup-$(date +%d-%m-%Y-%H.%M).tar.gz" | |
| # 2: Database connection info. | |
| db_name="wordpress" | |
| db_username="wordpress" | |
| db_password="wordpress123" | |
| # 3: Path to WordPress root directory and backup directory. | |
| wp_root_folder="/var/www/html/wordpress" | |
| backup_folder_path="/home/sasc/test" | |
| temp_wp_content="$backup_folder_path/temp_wp_content" | |
| # 4: Enter maintenance mode | |
| echo "Enabling maintenance mode..." | |
| touch "$wp_root_folder/.maintenance" | |
| # 5: Backup MySQL database | |
| echo "Backing up the database..." | |
| mysqldump --opt -u"$db_username" -p"$db_password" "$db_name" | gzip > "$backup_folder_path/$db_backup_name" | |
| # 6: Create temporary folder and sync wp-content for consistency | |
| echo "Creating a temporary copy of wp-content..." | |
| mkdir -p "$temp_wp_content" | |
| rsync -a --delete "$wp_root_folder/wp-content/" "$temp_wp_content/" | |
| # 7: Backup the wp-content folder from the temporary copy | |
| echo "Backing up wp-content..." | |
| tar -czf "$backup_folder_path/$wpfiles_backup_name" -C "$temp_wp_content" . | |
| # 8: Cleanup temporary folder | |
| rm -rf "$temp_wp_content" | |
| # 9: Exit maintenance mode | |
| echo "Disabling maintenance mode..." | |
| rm "$wp_root_folder/.maintenance" | |
| # 10: Delete all but 3 most recent database backups | |
| echo "Cleaning up old database backups..." | |
| find "$backup_folder_path" -maxdepth 1 -name "*.sql.gz" -type f -printf "%T@ %p\n" | sort -rn | awk 'NR>3 {print $2}' | xargs -r rm -- | |
| # 11: Delete all but 3 most recent wp-content backups | |
| echo "Cleaning up old wp-content backups..." | |
| find "$backup_folder_path" -maxdepth 1 -name "*.tar.gz" -type f -printf "%T@ %p\n" | sort -rn | awk 'NR>3 {print $2}' | xargs -r rm -- | |
| echo "Backup complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment