On Ubuntu how do I use CRON to backup my sites and delete old backups at the same time?

Posted on 2014-08-31
Last Modified: 2014-09-05
Hi all,

I have a ubuntu 14.04 x64 distribution on a Linode VPS with Apache 2.4.7 and mySQL server 5.5.

I have several working sites on my server already and I need to have some automatic system that could do the following:

At 4 am GMT (my system clock is set to GMT) every day I need certain folders to be gzipped and sent to a specific backup folder. I also need the script to keep the last 7 copies only (including current file) and delete the oldest file within the backup folder.

Working example:-
ORIGIN: /home/myuser/public/
DESTINATION: /home/myuser/public/_DB_BACKUP/

Could anyone please assist? I have no experience with CRON or .sh scripts and I have only started using linux last month.

thanks in advance
Question by:badwolfff
    1 Comment
    LVL 27

    Accepted Solution

    You also can't just gzip a folder.  You'll need tar.  There's also a call to the date command to insert the date into the file name.  It uses the backquote (`) which has a different meaning than the single quote (').

    tar -czf /home/myuser/public/_DB_BACKUP/`date "+%Y-%m-%d%n"`.tar.gz /home/myuser/public/

    Open in new window

    Here's a quick way to find and delete the older file
    find /home/myuser/public/_DB_BACKUP/ -mtime 7 -exec rm {} \;

    Open in new window

    You can then add them to cron.  This sets the backup to start every midnight.
    0 0 * * * /bin/tar -czf /home/myuser/public/_DB_BACKUP/`/bin/date "+%Y-%m-%d%n"`.tar.gz /home/myuser/public/; /usr/bin/find /home/myuser/public/_DB_BACKUP/ -mtime 7 -exec rm {} \;

    Open in new window

    crontab quick reference

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
    It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now