A WP-CLI script to archive MP3 files from your WordPress media library, create a ZIP file of the archive, and generate detailed logs.
This script helps WordPress administrators manage large collections of MP3 files by:
- Finding all MP3 files in the WordPress media library
- Creating a dated archive in a dedicated uploads directory
- Generating a ZIP file of all archived MP3s
- Creating a detailed CSV log of the operation
- Adding both the ZIP and log files to the media library for easy access
- Optionally deleting the original files after archiving
- WordPress installation with WP-CLI
- Bash shell environment
zipcommand installed on the server- Sufficient disk space for the archive and ZIP file
bccommand for more precise file size formatting (will use integer math if not available)
-
Download the script to your WordPress root directory:
wget -O archive_mp3_files.sh https://gist.githubusercontent.com/martylouis/fa1691991bdcf5035da2792a111c352b/raw/archive_mp3_files.sh
-
Make the script executable:
chmod +x archive_mp3_files.sh
Run the script from your WordPress root directory (where wp-config.php is located):
# Archive MP3 files without deleting originals
./archive_mp3_files.sh
# Archive MP3 files and delete originals
./archive_mp3_files.sh --delete--delete: Delete original MP3 files after successful archiving--help: Display usage information
- Identifies MP3 files in the WordPress media library (MIME type: audio/mpeg)
- Creates an archive directory at
wp-content/uploads/mp3-archive/YYYY-MM-DD/ - Copies all MP3 files to the archive location
- Creates a ZIP file at
wp-content/uploads/mp3-archive/mp3-archive-YYYY-MM-DD.zip - Generates a CSV log file at
wp-content/uploads/mp3-archive/mp3-archive-log-YYYY-MM-DD.csv - Adds both files to the WordPress media library for easy access
- Displays direct URLs to both the ZIP and log files
- Optionally deletes original files if the
--deleteflag is used
The script provides detailed output including:
- Number of MP3 files found and archived
- Total size of archived files
- Direct URLs to the ZIP and log files
- Media library IDs for the ZIP and log files
- Summary of the operation
The script creates a dedicated archive directory in your uploads folder:
wp-content/uploads/mp3-archive/
├── YYYY-MM-DD/ # Directory containing archived MP3 files
│ ├── file1.mp3
│ ├── file2.mp3
│ └── ...
├── mp3-archive-YYYY-MM-DD.zip # ZIP file of the archive
└── mp3-archive-log-YYYY-MM-DD.csv # Log file of the operation
Important Note: Unlike regular WordPress media uploads that go into year/month-based folders (like wp-content/uploads/2025/03/), this script places all archives in a dedicated mp3-archive folder for better organization and easier access.
The CSV log file contains the following columns:
- ID: WordPress attachment ID
- Title: MP3 file title
- File Path: Original file path
- File Size: Size in bytes
- Archive Path: Path in the archive
- Status: Operation status
- Date Archived: Timestamp
- The script uses
--skip-themes --skip-pluginsflags with WP-CLI commands for better performance - For large media libraries, the script may take some time to complete
- Ensure you have sufficient disk space for both the archive and the ZIP file
./archive_mp3_files.shOutput:
Finding MP3 files to archive...
Finding MP3 files in media library...
Found 25 MP3 files to archive.
Processing: 1234 - Sample MP3 File
...
ZIP file created successfully: wp-content/uploads/mp3-archive/mp3-archive-2025-03-10.zip (45.2 MB)
ZIP file URL: https://example.com/wp-content/uploads/mp3-archive/mp3-archive-2025-03-10.zip
Log file URL: https://example.com/wp-content/uploads/mp3-archive/mp3-archive-log-2025-03-10.csv
...
Operation completed:
- 25 MP3 files archived to wp-content/uploads/mp3-archive/2025-03-10
- Total size of archived files: 45.2 MB
- 0 operations failed
- Original files were preserved (use --delete to remove after archiving)
./archive_mp3_files.sh --delete-
"No MP3 files found"
- Check if you have MP3 files in your media library
- Verify they have the correct MIME type (audio/mpeg)
-
"ZIP command not found"
- Install the zip utility:
apt-get install zip(Debian/Ubuntu) oryum install zip(CentOS/RHEL)
- Install the zip utility:
-
"Failed to add ZIP file to media library"
- Check WordPress permissions
- Ensure the uploads directory is writable
-
Integer file sizes instead of decimal
- This happens when the
bccommand is not installed - The script will still work, but file sizes will be rounded (e.g., "45 MB" instead of "45.2 MB")
- Install bc:
apt-get install bc(Debian/Ubuntu) oryum install bc(CentOS/RHEL)
- This happens when the
-
WP-CLI errors
- Make sure you're running the script from the WordPress root directory
- Verify WP-CLI is installed and working:
wp --info
The script must be run from the root directory of your WordPress installation (where the wp-config.php file is located). Running it from any other directory will result in errors.
- The script should be run by users with appropriate permissions
- The
--deleteoption permanently removes files, so use with caution - Consider backing up your WordPress installation before using the
--deleteoption
This script is provided under the MIT License.
Marty Thierry (hey@martylouis.com)
- 1.0.0 (2025-03-10): Initial release
Contributions are welcome! Please feel free to submit a Pull Request.