Create a Cron Task in Ubuntu 16.04

Cron jobs are an incredibly useful Linux tool aimed at saving you time by scheduling tasks within your server. A programmed cron task will execute commands within a script by the minute, day, week or month. They can be scheduled to do many tasks including backing up your server’s files nightly, updating inventory orders in a database or even compressing files for migrating. Repetitive tasks become a cinch when incorporating a cron job. While there are numerous ways to run a cron task, we will be using the crontab option that is inherent within Ubuntu to set up a nightly backup of our website.

Pre-flight

Log in to your Ubuntu 16.04 LTS server, preferably not as root if you aren’t altering anything on the server level.

Setting Up a Website Backup through Cron

Step 1: Update your server. As a best practice, we will update and upgrade our server with the following command.

apt-get update && apt-get upgrade

 

Step 2: Verify if the cron package is installed.
dpkg -l cronOur example output let’s us know that the cron package is installed, along with its version:
||/ Name Version Architecture Description
+++-=========================-=================-=================-========================================================
ii cron 3.0pl1-128ubuntu2 amd64 process scheduling daemon

Install cron package if necessary.
sudo apt-get install cron

Ensure that the cron service is running with the following command:
systemctl status cronExample Output:
* cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2018-10-27 02:53:20 EDT; 5 days ago

 

Step 3: Configure the cron job. When you are logged in as your user, you are creating a cron job under that user. Creating a cron jobs owner is helpful when to know who is in charge of the cron as well as how to alter the cron job in the future.

crontab -e

The system asks which editor you’d like to use; this tutorial is using option 2 (vim.basic).

tom@host2:~$ crontab -e
no crontab for tom - using an empty one
Select an editor. To change later, run 'select-editor'.
1. /bin/ed
2. /usr/bin/vim.basic
3. /usr/bin/vim.tiny
Choose 1-3 []: 2

In this file, you’ll see # signs followed by the direction on how to use the file. Allow us a minute to explain the syntax needed to create a cron task.

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

Each set of digits designates the time for that section and follows the time set in your server The first set represents minutes, the next set hours, the next set day of the month and so on. You may be asking yourself what the asterisk mean. They represent every or all value for that set. So, if you put an asterisk on the month column, you’ll be telling the system to perform the tasks every month.

After the digits section is the /bin/sh and the type of file that should be executed, lastly, the final column assigns the command of which to run.

Backup Cron:
Let’s say you want to backup your website files daily. Our example uses 4 am since server traffic is low during this time. Take note of the .sh extension, indicating a bash script. Use the appropriate extension for your script. Be sure to save the script when exiting with :wq

crontab -e

After altering the /path/to/script/backup-script.sh path to match the location of your script, set the command in a new line, below the hashtag (#), like so:

# m h dom mon dow command
0 4 * * * /bin/sh /path/to/script/backup-script.sh

 

Step 4: Place Script in Path. Most importantly is the creation and placement of your script. With our example script we are creating a backup that will compress files and directories, indicated by the /SOURCE/DIRECTORY/. The /SOURCE/DIRECTORY/ needs to be alter to the directory and files you wish to save (sometimes a website’s default directory path is /var/www/html), while the /DESTINATION/DIRECTORY/file.zip indicates where the compressed backup are stored. We will name the script backup-script.sh, and afterwards, we will create the script in the location of /path/to/script/backup-script.sh.

vim backup-script.sh

#!/bin/sh
zip -9pr /DESTINATION/DIRECTORY/file.zip /SOURCE/DIRECTORY/

 

Note:
If you are running this script multiple times it will overwrite the file.zip each time, thus it runs it will only store one backup.

Set execute permissions this allows the system permission to run the script.

chmod +x backup-script.sh

When the time of execution has passed, check your /DESTINATION/DIRECTORY/ to see file.zip. YYou now have backups of your files running daily! It’s best to come up with a plan to offload your backups to another location other than your server. Pushing backups to an offsite location not only adds a layer of security but aids in saving server disk space. Check out our Cloud Object Storage for cost-effective storage solution that is accessible via API call. If you are having trouble with your cron check out our other helpful article, Troubleshooting: Cron Jobs.

 

An Overview of Managed WordPress

WordPress is open source software for building unique and powerful websites! It is quickly becoming the easiest and most popular way to create blogs, business sites, portfolios, forums, memberships, and e-commerce websites.

Liquid Web’s Managed WordPress Hosting is a complete solution for your web publishing needs. With pre-installed plugins, streamlined plugin updates, website staging area, nightly backups, iThemes sync, and customizable website stencils, it’s a must-have for any WordPress developer.

 

MWP Features

Let’s get right into it with a couple of Managed WordPress Hosting best features and ease of use!

 

 

Pre-installed Plugins:

Managed WordPress (MWP) comes with quite a few money saving, pre-installed plugins, curated for maximum performance:

Akismet
Akismet: The number one plugin for spam filtering on blogs and forum pages.  This indispensable plugin helps comment-heavy sites by preventing spam comments from being posted to your site. Akismet protects your website from being marked negatively thus helping your Google SEO standing. A free plan is included with every site as well as having the option to subscribe to their Plus and Enterprise plans.
Async JavaScript
Async JavaScriptWith a 4.5-star rating by its users, Async JavaScript increases site speed and search engine ranking by only loading javascript viewable by the user.
Autoptimize
Autoptimize: If speed is essential to your site then you can’t go wrong with the Auto Optimizer plugin. Autoptimize takes out the legwork of site optimization by aggregating, minifying and caching scripts. For the CSS and JavaScript programmer, it can inject CSS into your page header, async non-aggregated JavaScript, and minimize HTML.
BJ Lazy Load
BJ Lazy Load: This plugin comes in handy for sites with lots of images. The idea is only to load those videos/pictures that are viewable on the browser and not those below the screen view (or “fold” as it’s called) until the client scrolls down the page. Thus increasing site speed and performance by reducing the resources loaded at a given time.
iThemes Sync
Themes Sync: Ever wish you had a portal where you could update all your plugins and themes for multiple websites in one spot? Look no further then iThemes Sync.  iThemes Sync provides you with one central dashboard for all your WordPress admin tasks saving you time to focus on development.
TinyPNG
TinyPNG: Minimizes load times for your site by compressing your images, while still maintaining photo resolution. Compression increases site performance especially if you have a large number of images.

WP Forms Lite
WP Forms Lite: Easily create contact forms with intuitive tools that allow for drag and drop construction.

Liquid Web’s Managed WordPress staging site allows you to experiment with themes, plugins, or any variety of other changes you might want to make, all without affecting your live site! It works by creating a temporary clone of your site to test your changes, giving you a chance to get things exactly the way you want them before applying them to your live site. So feel free to test away!

Nightly backups of your sites are included in each plan. The backups allow you to roll back to an older version by clicking the restore option, or you can download a copy. No need to install an extra plugin or stress when a development error occurs.

Core updates help secure your site from hackers and malware by keeping your website up to date. Once available, the core WordPress plugin updates MWP are tested before being implemented on your site. If the plugin or update is compatible with your site, it will auto push the update to the live site. If the new update is not compatible, it will let you know via email allowing you time to inspect at your leisure.

The stencil feature is useful for developers who use the same themes and plugins across multiple sites. You can create as many stencils as you like with the click of a button! Click here for information about how you can set up your stencil.

Managed WordPress allows you to access your content via SSH and FTP. Once logged into SSH, Liquid Web’s Manage WordPress comes with WP-CLI pre-installed, so you can make simple commands from the command line to fine-tune users, plugins, and current themes settings.

Let your users know their information is safe! Managed WordPress includes free SSL for all sites on your server to help keep your sites secure.  With automatic SSLs you no longer have to purchase certificates for your websites!

Our managed WordPress product has a 24/7 operations team that manages routine server maintenance and monitors for DDoS attacks, so you don’t have to, leaving you free to develop your website’s content.

Experience a streamlined way managing your sites through Liquid Web’s Managed WordPress platform.

 

Using Object Storage for cPanel Backups

Object Storage is simply the easiest, most cost effective and secure backup solution for your data!

What are backups?

A backup is simply the procedure of making extra copies of your data in case the original data is lost or damaged. Liquid Web understands that your data is invaluable and so, we have provided a stable, secure and cost effective product called Object Storage which makes managing your cPanel backups much easier.

Continue reading “Using Object Storage for cPanel Backups”

Backup Your WordPress Database with WP-CLI

In this article you will learn how to backup your WordPress database using the wp-cli tool. Knowing how to backup your database is a critical skill to have when running a WordPress site. All your posts, pages, and more live in your database; keeping backups is critical.

You should always take a backup before any major changes to your site, just in case. It’s much quicker to take a backup now and do a restore if you need to, than to find a useful backup when you need it. Continue reading “Backup Your WordPress Database with WP-CLI”

How To Back up Your Website in cPanel

  1. This tutorial assumes you’ve already logged in to cPanel, and are starting on the home screen.cpanel-paperlantern-15-backups--01
  2. Now let’s learn how to backup our website.cpanel-paperlantern-15-backups--02
  3. Click the “Backup Wizard” icon.cpanel-paperlantern-15-backups--03
  4. This is the main backup screen where you can backup, or restore the files in your hosting account.cpanel-paperlantern-15-backups--04
  5. To backup your website, click the “Backup” button.cpanel-paperlantern-15-backups--05
  6. You have the option of only backing up part of your account …cpanel-paperlantern-15-backups--06
  7. … or generating a full backup, which we’ll do now. Click the “Full Backup” button.cpanel-paperlantern-15-backups--07
  8. Select the destination, then click “Generate Backup”.cpanel-paperlantern-15-backups--08
  9. That’s it! We’ve just created a full backup to the home directory.cpanel-paperlantern-15-backups--09
  10. To download a copy of the backup to your computer, simply click its link. That’s all there is to it!cpanel-paperlantern-15-backups--10
  11. Restoring files is just as easy. Click the “Backup Wizard” icon again.cpanel-paperlantern-15-backups--11
  12. Then click the “Restore” button.cpanel-paperlantern-15-backups--12
  13. Choose what type of restore you want to do… in this case, let’s restore the “Home Directory”.cpanel-paperlantern-15-backups--13
  14. Locate the Home Directory backup…cpanel-paperlantern-15-backups--14
  15. Then click “Upload”.cpanel-paperlantern-15-backups--15
  16. The files have been restored!

 

How To Enable Server Backups in WHM/cPanel

The most important thing you can do to protect your server against data loss is to take regular backups. Properly configured backups are a critical aspect to the maintenance of any website, and can mean the difference between a quick recovery and rebuilding a site from scratch.

If a key file were to accidentally be deleted, a site database were to become irreparably corrupted, or your site were to become infected with malware, would you be able to restore your data and get your site back up within a few minutes? If you can’t answer “yes”, then it’s time to review your backup strategy.

Liquid Web customers have access to a variety of backup methods including Guardian and Storm® backups, but this article focuses exclusively on cPanel backups. Because cPanel backups are not managed by Liquid Web, you will want to ensure that you have both enabled backups as described in this article and formulated an appropriate backup retention policy. It’s critical that you not attempt to store so many backups that your server runs out of disk space. In fact, you ideally want to reserve at least 10 percent to 20 percent of your total disk space: cPanel needs to (temporarily) use disk space to create backups and update itself and other software packages, and your operating system and database both rely on having disk space available to perform core functions.

Pre-Flight Check

  • This article covers backup configuration in WebHost Manager using cPanel’s “new” backup system. Legacy backups still are supported in cPanel for backward compatibility, but the current system is preferred as it is designed to be more efficient and reliable.
  • While these instructions specifically apply to WHM version 54.20; the same steps should apply to any recent cPanel version.

Step #1: Configure Global Settings

Log in to WHM using your server’s root credentials, and select Backup Configuration under the Backup section in WHM’s main menu. You can begin typing “backup” into the search menu to narrow the list of menu options.

WHM backup

In the Global Settings section. select the radio button next to Enable to turn on the backup system.

Note: The Enable button only turns on the backup system; you still need to instruct cPanel to back up accounts, and which accounts to back up, which we cover later in this article.

Now choose the type of backups you want cPanel to create:

  • Compressed backups use the least amount of disk space, potentially giving you more restore points than could get with the other methods. Compressed backups take longer to complete and the process of restoring an individual file requires the extra step of first extracting the file, but in many cases it represents an ideal option. Note that the size of a compressed cPanel backup will range from about 25% to 75% of the actual account size, depending on a number of factors, such as the types of files owned by the account. Until backups have run and you can see the actual size of each archive, it’s generally safe to estimate that a compressed backup will take up about half the space of the account itself. Once backups have run, you can use the actual figures to determine how many compressed backups to retain.
  • Uncompressed backups can be completed faster, and the process of restoring individual files is quicker because they don’t first need to be extracted from the archive. However, they take up the same amount of space as the account itself and, as such, allow for fewer restore points. If you choose to use uncompressed backups, you must ensure that your retention policy does not allow you to run out of disk space.
  • Incremental backups allow you to retain only one daily, weekly, and monthly backup, giving you fewer restore points than either of the other options. The backups are uncompressed, so each will take up the same amount of disk space as the account itself. Individual files can be quickly restored, but changes to your files are synced each time backups run. That means that a corrupted, compromised, or changed file you wish to restore could already have been synced to your backup if the change isn’t immediately noticed.

Step #2: Choose Scheduling and Retention Options

The Scheduling and Retention section allows you to enable or disable daily, weekly, and monthly backups, and specify the number of each type of backup you wish to store. For daily and weekly backups, you can select the days of the week on which backups will be taken and, for monthly backups, you can select whether you want them to be taken at the beginning of the month, in the middle of the month, or both.

Scheduling and retention

Note: If you select both the first of the month and the 15th of the month for monthly backups to run, each will count as one monthly backup. When choosing both backup date options, you will need to retain two monthly backups for each month you wish to retain.

Step #3: Select Account Backup Options

The Files section allows you to select which accounts to back up and choose more detailed options for each:

FilesBackupAccounts

  • We enabled the backup system itself in Global Settings above, and now we’ll instruct cPanel to back up user accounts by checking the box next to Backup Accounts.
  • Click the Select Users to open a new browser window (or tab, depending on your browser settings) where you can select the specific cPanel accounts to back up. Simply click the ON button in the Backups column for each domain you want to back up, and ensure that the buttons in the Legacy are turned off if you’re not using the legacy backup system. Note that you can access this page directly at any time by clicking Backup User Selection in WHM’s main menu.

    BackupUserSelection

    Choose whether you want to back up accounts that are suspended, as well as access logs and bandwidth data for the accounts, and whether you wish to back up system files as well as individual accounts (which is necessary only for server restoration; individual account can be restored regardless of your choice here).

Step #4: Select Database Backup Options

In the Databases section, select whether you want to back up databases per account, the entire MySQL directory, or both. In most cases, you’ll want to choose Per Account and Entire MySQL Directory.

BackupDatabases

Step #5: Select Backup Location

The Configure Backup Directory section allows you to choose whether you want to back up locally or externally.

BackupDirectory

  • The Default Backup Directory is pre-configured, and in most cases should not be changed.
  • If you elect to Retain backups in the default backup directory, local backups will be retained even if you configure the server to automatically transfer them to an external destination.
  • The Mount Backup Drive as Needed option should, in most cases, remain disabled, as an external drive set up by Liquid Web will be configured to mount at boot.

Step #5: Select Remote Transport Options

The Additional Destinations section allows you to set up a method for transferring your backups off-server via SFTP or another method. To do so, select a Destination Type, click the Create new destination button and provide the requested credentials on the form.

AdditionalDest

Step #6: Review and Save Backup Settings

Once you’re satisfied with your backup configuration, click the Save Configuration button to apply your settings.
 

How To Restore WordPress Optimized Template Backups

WordPress Optimized Template sites installed via the Sites tab in Manage automatically are backed up each day, with the server retaining the 10 most recent backups. You can easily restore any site from an available backup in the Sites dashboard in Manage. Continue reading “How To Restore WordPress Optimized Template Backups”

Proactive Server Maintenance Checklist, Prior to Reboot

Take a Backup, or Be Sure Backups are Running

We have tutorials on How to Create a Storm Server Backup, and How to Restore a Storm Server Backup.

Check Running Processes

Liquid Web’s Heroic Support team closely monitors the load on your servers with Sonar® proactive monitoring and service restoration. That said, it’s always wise to glance at what processes are running on your server to assure your services are running as expected. Check-out our tutorial on Monitoring Server Processes with Top for Linux.

Verify Services Will Start at Boot

Be sure to verify that all of your necessary services are configured to start when the server boots up. For CentOS users, you can follow this tutorial: chkconfig Command Examples for Red Hat and CentOS.

Save, Save, and Save Some More

Always check whether or not there are any unsaved changes to configuration files, etc. prior to a reboot. Save early and safe often!
Continue reading “Proactive Server Maintenance Checklist, Prior to Reboot”