Also expire files uploaded to gnusocial/postactiv

This commit is contained in:
Bob Mottram 2017-04-23 17:25:10 +01:00
parent a59923cc6e
commit bbfa6d353f
1 changed files with 44 additions and 36 deletions

View File

@ -503,53 +503,61 @@ function expire_gnusocial_posts {
expire_months=3
fi
expire_days=$((expire_months * 30))
# To prevent the database size from growing endlessly this script expires posts
# after a number of months
if [ ! -d /var/www/${domain_name}/htdocs ]; then
return
fi
gnusocial_expire_script=/usr/bin/${gnusocial_type}-expire
gnusocial_expire_posts_script=/usr/bin/${gnusocial_type}-expire-posts
echo '<?php' > $gnusocial_expire_script
echo '' >> $gnusocial_expire_script
echo "// ${gnusocial_type} post expiry script, based on StatExpire by Tony Baldwin" >> $gnusocial_expire_script
echo '// https://github.com/tonybaldwin/statexpire' >> $gnusocial_expire_script
echo '' >> $gnusocial_expire_script
echo "\$oldate=date((\"Y-m-d\"), strtotime(\"-${expire_months} months\"));" >> $gnusocial_expire_script
echo '$username="root";' >> $gnusocial_expire_script
echo "\$password=shell_exec('${PROJECT_NAME}-pass -u root -a mariadb');" >> $gnusocial_expire_script
echo "\$database=\"${gnusocial_type}\";" >> $gnusocial_expire_script
echo '' >> $gnusocial_expire_script
echo 'if (!$link = mysql_connect("localhost", $username, $password)) {' >> $gnusocial_expire_script
echo ' echo "Could not connect to mariadb";' >> $gnusocial_expire_script
echo ' exit;' >> $gnusocial_expire_script
echo '}' >> $gnusocial_expire_script
echo '' >> $gnusocial_expire_script
echo 'if (!mysql_select_db($database, $link)) {' >> $gnusocial_expire_script
echo " echo \"Could not select ${gnusocial_type} database\";" >> $gnusocial_expire_script
echo ' exit;' >> $gnusocial_expire_script
echo '}' >> $gnusocial_expire_script
echo '' >> $gnusocial_expire_script
echo "\$notice_query=\"DELETE FROM notice WHERE created <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_script
echo "\$conversation_query=\"DELETE FROM conversation WHERE created <= '$oldate 01:01:01'\";" >> $gnusocial_expire_script
echo "\$reply_query=\"DELETE FROM reply WHERE modified <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_script
echo '' >> $gnusocial_expire_script
echo 'mysql_query($notice_query);' >> $gnusocial_expire_script
echo '$rowaff1=mysql_affected_rows();' >> $gnusocial_expire_script
echo 'mysql_query($conversation_query);' >> $gnusocial_expire_script
echo '$rowaff2=mysql_affected_rows();' >> $gnusocial_expire_script
echo 'mysql_query($reply_query);' >> $gnusocial_expire_script
echo '$rowaff3=mysql_affected_rows();' >> $gnusocial_expire_script
echo 'mysql_close();' >> $gnusocial_expire_script
echo '' >> $gnusocial_expire_script
echo -n "echo \"Expire ${gnusocial_type} posts: " >> $gnusocial_expire_script
echo '$rowaff1 notices, $rowaff2 conversations, and $rowaff3 replies deleted from database.\n";' >> $gnusocial_expire_script
echo '<?php' > $gnusocial_expire_posts_script
echo '' >> $gnusocial_expire_posts_script
echo "// ${gnusocial_type} post expiry script, based on StatExpire by Tony Baldwin" >> $gnusocial_expire_posts_script
echo '// https://github.com/tonybaldwin/statexpire' >> $gnusocial_expire_posts_script
echo '' >> $gnusocial_expire_posts_script
echo "\$oldate=date((\"Y-m-d\"), strtotime(\"-${expire_months} months\"));" >> $gnusocial_expire_posts_script
echo '$username="root";' >> $gnusocial_expire_posts_script
echo "\$password=shell_exec('${PROJECT_NAME}-pass -u root -a mariadb');" >> $gnusocial_expire_posts_script
echo "\$database=\"${gnusocial_type}\";" >> $gnusocial_expire_posts_script
echo '' >> $gnusocial_expire_posts_script
echo 'if (!$link = mysql_connect("localhost", $username, $password)) {' >> $gnusocial_expire_posts_script
echo ' echo "Could not connect to mariadb";' >> $gnusocial_expire_posts_script
echo ' exit;' >> $gnusocial_expire_posts_script
echo '}' >> $gnusocial_expire_posts_script
echo '' >> $gnusocial_expire_posts_script
echo 'if (!mysql_select_db($database, $link)) {' >> $gnusocial_expire_posts_script
echo " echo \"Could not select ${gnusocial_type} database\";" >> $gnusocial_expire_posts_script
echo ' exit;' >> $gnusocial_expire_posts_script
echo '}' >> $gnusocial_expire_posts_script
echo '' >> $gnusocial_expire_posts_script
echo "\$notice_query=\"DELETE FROM notice WHERE created <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_posts_script
echo "\$conversation_query=\"DELETE FROM conversation WHERE created <= '$oldate 01:01:01'\";" >> $gnusocial_expire_posts_script
echo "\$reply_query=\"DELETE FROM reply WHERE modified <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_posts_script
echo '' >> $gnusocial_expire_posts_script
echo 'mysql_query($notice_query);' >> $gnusocial_expire_posts_script
echo '$rowaff1=mysql_affected_rows();' >> $gnusocial_expire_posts_script
echo 'mysql_query($conversation_query);' >> $gnusocial_expire_posts_script
echo '$rowaff2=mysql_affected_rows();' >> $gnusocial_expire_posts_script
echo 'mysql_query($reply_query);' >> $gnusocial_expire_posts_script
echo '$rowaff3=mysql_affected_rows();' >> $gnusocial_expire_posts_script
echo 'mysql_close();' >> $gnusocial_expire_posts_script
echo '' >> $gnusocial_expire_posts_script
echo -n "echo \"Expire ${gnusocial_type} posts: " >> $gnusocial_expire_posts_script
echo '$rowaff1 notices, $rowaff2 conversations, and $rowaff3 replies deleted from database.\n";' >> $gnusocial_expire_posts_script
chmod +x $gnusocial_expire_posts_script
gnusocial_expire_script=/usr/bin/${gnusocial_type}-expire
echo '#!/bin/bash' > $gnusocial_expire_script
echo "/usr/bin/php $gnusocial_expire_posts_script" > $gnusocial_expire_script
echo "find /var/www/${domain_name}/htdocs/file/* -mtime +${expire_days} -exec rm {} +" > $gnusocial_expire_script
chmod +x $gnusocial_expire_script
# Add a cron job
if ! grep -q "${gnusocial_type}_expire_script" /etc/crontab; then
echo "10 3 5 * * root /usr/bin/timeout 500 /usr/bin/php ${gnusocial_expire_script}" >> /etc/crontab
echo "10 3 5 * * root /usr/bin/timeout 500 ${gnusocial_expire_script}" >> /etc/crontab
fi
# remove old expire script