Saving space automatically – Beginners guide to Digital Ocean

Ernest Marcinko Hosting, Tutorials, Wordpress 2 Comments

After a while, you will notice an exponential growth of used space, without even copying anything to it. How so?

Your server size grows mostly because:

  • You are adding/updating new packages – cached package data is stored
  • Kernels are updating regularly – old ones are kept
  • Log files are being created and archived for a long long time
Keeping the packages clean

I'm using this command to clear the apt-get cache clean:

sudo apt-get autoremove & sudo apt-get autoclean & sudo apt-get clean

You can automate this task by adding this entry to the crontab. (sudo crontab -e):

0 11 * * * sudo apt-get autoremove & sudo apt-get autoclean & sudo apt-get clean > /dev/null 2>&1

This will run the command every day at 11AM. I choose this time, as I have other cron-jobs running at different hours.

Remove unused kernel files

WARNING: Please be very careful when deleting kernels. Always make a back-up of your droplet before running these commands, just in case.

After some research I have found this really neat command by David Kemp on askubuntu.com:

echo $(dpkg --list | grep linux-image | awk '{ print $2 }' | sort -V | sed -n '/'`uname -r`'/q;p') $(dpkg --list | grep linux-headers | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p') | xargs sudo apt-get -y purge

Cron version:

0 10 * * * echo $(dpkg --list | grep linux-image | awk '{ print $2 }' | sort -V | sed -n '/'`uname -r`'/q;p') $(dpkg --list | grep linux-headers | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p') | xargs sudo apt-get -y purge > /dev/null 2>&1
Logs and log rotation

In ubuntu there is a built in service called logrotate, which is configured to rotate the log files for other services. It's configured for each service individually. To get to the logrotate configuration directory run:

cd /etc/logrotate.d/

If you now run an ls command, there will be a few files for each important service. We are going to check the apache2 config file, as it's the most important, and it can grow really quickly to a tremendous size – however in most cases it's not needed.

Let's open the apache2 config file shall we:

sudo nano /etc/logrotate.d/apache2

After opening, you should see something like this:

/var/log/apache2/*.log {
        weekly
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                if /etc/init.d/apache2 status > /dev/null ; then \
                    /etc/init.d/apache2 reload > /dev/null; \
                fi;
        endscript
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi; \
        endscript
}

Okay. So the important parts for us are the following lines:

  • weekly – meaning the log files are rotated every week
  • rotate 52 – meaning that 52 rotations are kept, thus 52x1week = almost a year

For me this is too much information to keep for no reason. My final configuration is:

/var/log/apache2/*.log {
        daily
        missingok
        rotate 14
        size 1M
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                if /etc/init.d/apache2 status > /dev/null ; then \
                    /etc/init.d/apache2 reload > /dev/null; \
                fi;
        endscript
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi; \
        endscript
}

There is one more parameter I appended after the rotate line, which is size 1M. This will make sure the rotation occurs once the current log file exceeds 1MB of data. Keep in mind, that the original configuration would have rotated only if the log exceeds 100M by default. This could have potentially created over 5+GB of overhead from log files. Instead, now it caps at 14x1MB.

Chapters

Ernest Marcinko

WordPress developer, javascript enthusiast, photoshop clicker and the founder of wp-dreams.com

Stay connected

Related Posts
Filter by
Post Page
Blog Tutorials Hosting Wordpress Testing
Sort by

Swap file – Beginners guide to Digital Ocean

If you reached this step, your server is up and running flawlessly. However after gaining some traffic it will run out
2015-01-21 15:48:02
admin

18

Mailing – Beginners guide to Digital Ocean

Sending mails properly is probably the hardest task to achieve with Digital Ocean. I will present two ways of doing
2015-01-20 21:38:53
admin

18

FTP & SFTP – Beginners guide to Digital Ocean

So far we have a running web server with a working database, but we don't have FTP access. SFTP However
2015-01-16 17:08:19
admin

18

Virtual Hosts - Beginners guide to Digital Ocean

By now we have a working private server with very basic features and a demo user. By default your website is data is
2015-01-15 19:23:28
admin

18

From XAMPP to Vagrant WordPress

2016-01-05 19:58:17
admin

8

Comments 2

  1. klik365

    Everything is very open with a really clear description of
    the challenges. It was really informative. Your site is useful.
    Thanks for sharing!

Leave a Reply

Your email address will not be published. Required fields are marked *