add daily cronjob instructions & optimize script

This commit is contained in:
VirtuBox 2018-11-30 17:56:06 +01:00
parent b1b012ab10
commit d41b486bf3
6 changed files with 51 additions and 33 deletions

View File

@ -65,10 +65,24 @@ git -C $HOME/.img-optimize pull
source .bashrc
```
### Setup daily cronjob
You copy the scripts to /etc/cron.daily :
```bash
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/jpg-png-cron
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/webp-cron
chmod +x /etc/cron.daily/jpg-png-cron
chmod +x /etc/cron.daily/webp-cron
```
Then just edit your websites path set with the variables `sites` at the beginning of the cron scripts.
### Warning
Conversion process can take a while, you can use `tmux` to launch the script and be able to close your ssh connection without interrupting conversion. Then just use `tmux attach` to login back in your tmux session.
### Credits
WebP conversion script is inspired by this [DigitalOcean Community Tutorial](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website)
WebP conversion script was inspired by this [DigitalOcean Community Tutorial](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website)

View File

@ -4,11 +4,13 @@
## images path are listed in sites.csv
## written by VirtuBox (https://virtubox.net)
input="sites.csv"
while IFS='|' read -r f1 f2
do
sites="/var/www/yoursite.tld/images \
/var/www/yourothersite.tld/content/images \
/var/www/yourthirdsite.tld/wp-content/uploads"
for site in $sites; do
# optimize jpg images created in the last 24 hours
find "$f2" -ctime 0 -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 jpegoptim --preserve --quiet --strip-all -m82 >> /var/log/jpg-png-cron.log
find "$site" -ctime 0 -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 jpegoptim --preserve --strip-all -m82 >> /var/log/jpg-png-cron.log 2>&1
# optimize png images created in the last 24 hours
find "$f2" -ctime 0 -type f -iname '*.png' -print0 | xargs -0 optipng -o7 -strip all -quiet >> /var/log/jpg-png-cron.log
done < "$input"
find "$site" -ctime 0 -type f -iname '*.png' -print0 | xargs -0 optipng -o7 -strip all >> /var/log/jpg-png-cron.log 2>&1
done

View File

@ -1,2 +0,0 @@
1|/var/www/yoursite.tld/htdocs/wp-content/uploads
2|/var/www/yourothersite.tld/htdocs/wp-content/uploads
1 1 /var/www/yoursite.tld/htdocs/wp-content/uploads
2 2 /var/www/yourothersite.tld/htdocs/wp-content/uploads

View File

@ -4,22 +4,18 @@
## images path are listed in sites.csv
## written by VirtuBox (https://virtubox.net)
input="sites.csv"
while IFS='|' read -r f1 f2
do
sites="/var/www/yoursite.tld/images \
/var/www/yourothersite.tld/content/images \
/var/www/yourthirdsite.tld/wp-content/uploads"
for site in $sites; do
# convert png created in the last 24 hours to webp
{
find "$f2" -ctime 0 -type f -iname "*.png" -print0 | xargs -0 -I {} \
bash -c 'webp_version="$0".webp
if [ ! -f "$webp_version" ]; then
{ cwebp -quiet -z 9 -mt {} -o {}.webp; }
fi'
find "$site" -ctime 0 -type f -iname "*.png" -print0 | xargs -0 -I {} \
bash -c '[ ! -f "{}.webp" ] && { cwebp -z 9 -mt {} -o {}.webp; }' >> /var/log/webp-cron.log 2>&1
# convert jpg created in the last 24 hours to webp
find "$f2" -ctime 0 -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -I {} \
bash -c 'webp_version="$0".webp
if [ ! -f "$webp_version" ]; then
{ cwebp -quiet -q 82 -mt {} -o {}.webp; }
fi'
} >> /var/log/webp-cron.log
done < "$input"
find "$site" -ctime 0 -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -I {} \
bash -c '[ ! -f "{}.webp" ] && { cwebp -q 82 -mt {} -o {}.webp; }'
} >> /var/log/webp-cron.log 2>&1
done

View File

@ -65,10 +65,24 @@ git -C $HOME/.img-optimize pull
source .bashrc
```
### Setup daily cronjob
You copy the scripts to /etc/cron.daily :
```bash
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/jpg-png-cron
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/webp-cron
chmod +x /etc/cron.daily/jpg-png-cron
chmod +x /etc/cron.daily/webp-cron
```
Then just edit your websites path set with the variables `sites` at the beginning of the cron scripts.
### Warning
Conversion process can take a while, you can use `tmux` to launch the script and be able to close your ssh connection without interrupting conversion. Then just use `tmux attach` to login back in your tmux session.
### Credits
WebP conversion script is inspired by this [DigitalOcean Community Tutorial](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website)
WebP conversion script was inspired by this [DigitalOcean Community Tutorial](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website)

View File

@ -128,10 +128,7 @@ if [ "$WEBP_OPTIMIZATION" = "y" ]; then
echo -ne ' png to webp conversion [..]\r'
cd $IMG_PATH || exit 1
find . -type f -iname "*.png" -print0 | xargs -0 -I {} \
bash -c 'webp_version="$0".webp
if [ ! -f "$webp_version" ]; then
{ cwebp -z 9 -mt {} -o {}.webp; }
fi'
bash -c '[ ! -f "{}.webp" ] && { cwebp -z 9 -mt {} -o {}.webp; }'
echo -ne " png to webp conversion [${CGREEN}OK${CEND}]\\r"
echo -ne '\n'
@ -140,10 +137,7 @@ if [ "$WEBP_OPTIMIZATION" = "y" ]; then
echo -ne ' jpg to webp conversion [..]\r'
cd $IMG_PATH || exit 1
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -I {} \
bash -c 'webp_version="$0".webp
if [ ! -f "$webp_version" ]; then
{ cwebp -q 82 -mt {} -o {}.webp; }
fi'
bash -c '[ ! -f "{}.webp" ] && { cwebp -q 82 -mt {} -o {}.webp; }'
echo -ne " jpg to webp conversion [${CGREEN}OK${CEND}]\\r"
echo -ne '\n'