From d41b486bf3fc3fe77c00c2914e6cfc6a095b2c2d Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Fri, 30 Nov 2018 17:56:06 +0100 Subject: [PATCH] add daily cronjob instructions & optimize script --- README.md | 16 +++++++++++++++- crons/jpg-png-cron.sh | 14 ++++++++------ crons/sites.csv | 2 -- crons/webp-cron.sh | 26 +++++++++++--------------- docs/README.md | 16 +++++++++++++++- optimize.sh | 10 ++-------- 6 files changed, 51 insertions(+), 33 deletions(-) delete mode 100644 crons/sites.csv diff --git a/README.md b/README.md index 9504b9f..9b7e4aa 100644 --- a/README.md +++ b/README.md @@ -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) \ No newline at end of file +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) \ No newline at end of file diff --git a/crons/jpg-png-cron.sh b/crons/jpg-png-cron.sh index ecb1a41..55632e2 100644 --- a/crons/jpg-png-cron.sh +++ b/crons/jpg-png-cron.sh @@ -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" \ No newline at end of file + find "$site" -ctime 0 -type f -iname '*.png' -print0 | xargs -0 optipng -o7 -strip all >> /var/log/jpg-png-cron.log 2>&1 +done \ No newline at end of file diff --git a/crons/sites.csv b/crons/sites.csv deleted file mode 100644 index c0421ae..0000000 --- a/crons/sites.csv +++ /dev/null @@ -1,2 +0,0 @@ -1|/var/www/yoursite.tld/htdocs/wp-content/uploads -2|/var/www/yourothersite.tld/htdocs/wp-content/uploads \ No newline at end of file diff --git a/crons/webp-cron.sh b/crons/webp-cron.sh index c8e12b3..aa983d8 100644 --- a/crons/webp-cron.sh +++ b/crons/webp-cron.sh @@ -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 diff --git a/docs/README.md b/docs/README.md index 9504b9f..9b7e4aa 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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) \ No newline at end of file +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) \ No newline at end of file diff --git a/optimize.sh b/optimize.sh index d966e8d..64d5f9d 100755 --- a/optimize.sh +++ b/optimize.sh @@ -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'