Skip to content

Instantly share code, notes, and snippets.

@rvalitov
Created November 29, 2024 11:49
Show Gist options
  • Save rvalitov/371aaf438c90203e41582fc42e9c2945 to your computer and use it in GitHub Desktop.
Save rvalitov/371aaf438c90203e41582fc42e9c2945 to your computer and use it in GitHub Desktop.
Creates ZIP archive of uploads directory of WordPress, selecting only the original images.
#!/bin/bash
#
# This script will compress all of the files in the WordPress uploads directory into ZIP archives,
# excluding any files that contain a resolution in the filename.
# A ZIP archive is created for each month that has a directory in the uploads directory.
# The ZIP archives are placed in directory "uploads-archives".
# Setup:
# 1) copy this file into "wp-content" directory of WordPress.
# 2) make it executable: chmod +x wp-backup-uploads.sh
# 3) create a directory that will store the ZIP archives: mkdir uploads-archives
#
# Usage: ./wp-backup-uploads.sh -s 2000 -e 2023
#
# The -s and -e flags are optional. If they are not provided, the script will default to compressing
# all files for the last 10 years. If they are provided, the script will compress all files from the start
# year to the end year, inclusive.
#
# Original code credits:
# Source: https://philipjohn.blog/2023/05/08/archiving-a-large-wordpress-uploads-folder/
# Author: Philip John <[email protected]>
# Author URI: https://philipjohn.me.uk
# License: MIT
# License URI: https://opensource.org/licenses/MIT
# Version: 1.0.0
#
# What's new and the differences with the original code:
# 1) using ZIP instead of tar.gz, because ZIP is better to send files to Windows users
# 2) tests that the directory for specified year and month does exist, as a result elimination
# of error messages about non-existent files
# 3) skipping creation of empty archive files for non-existent years and months
# 4) all files in ZIP are stored in its root, without the directory structure like "uploads/2024/01" that
# makes it easier to manage the ZIP files later
# Get the start and end year from the command line arguments.
while getopts s:e: flag
do
case "${flag}" in
s) START=${OPTARG};;
e) END=${OPTARG};;
esac
done
# Set the default start and end years to use.
DEFAULT_START_YEAR=`date +%Y -d "10 years ago"` # 10 years ago
DEFAULT_END_YEAR=`date +'%Y'` # This year
# Set the start and end year to the default values if they were not provided.
START_YEAR="${START:=$DEFAULT_START_YEAR}"
END_YEAR="${END:=$DEFAULT_END_YEAR}"
# Let's gooooooo!
for year in `seq $START_YEAR $END_YEAR`
do
for month in `seq -s " " -w 01 12`
do
if [ -d uploads/$year/$month ]; then
# Tell the user what we're doing.
echo "Compressing all $year/$month upload files"
# Find looks for all files in the uploads/year/month/ directory that start with the current character
# and do not contain a resolution in the filename.
# We use pushd and popd to store the files in the root of the zip, without the directory structure
pushd uploads/$year/$month
find . -type f ! -name '*[0-9]x[0-9]*.*' -print0 | zip ../../../uploads-archives/uploads-$year-$month.zip -@
popd
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment