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

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/mysite.com/public
DESTINATION: /home/myuser/public/_DB_BACKUP/mysite.com/mysite.com_yyyy-mm-dd.gz

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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/mysite.com/mysite.com_`date "+%Y-%m-%d%n"`.tar.gz /home/myuser/public/mysite.com/public

Open in new window

Here's a quick way to find and delete the older file
find /home/myuser/public/_DB_BACKUP/mysite.com/ -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/mysite.com/mysite.com_`/bin/date "+%Y-%m-%d%n"`.tar.gz /home/myuser/public/mysite.com/public; /usr/bin/find /home/myuser/public/_DB_BACKUP/mysite.com/ -mtime 7 -exec rm {} \;

Open in new window

crontab quick reference  http://www.adminschoice.com/crontab-quick-reference/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.