Helpful Information
 
 
Category: Programming Articles
[How To] Automated Filesystem Datestamp Backup using CRON via shell

To automatically make a backup of your raw files using *nix cron:

Requires: Shell access, ability to run bash and add (hah) scripts to cron.xxx.
Recommended: Use in conjunction with this script here: Auto Database CRON Backup (http://www.vbulletin.org/forum/showthread.php?p=875820)

Create a bash script (a text file, if you will) named sitedata.sh - you can replace sitedata with whatever you like.


#! /bin/bash
# Compressed Directory Tree Backup
tar -czvf /your/backup/folder/sitedata-`date --iso-8601`.tar.gz /path/to/your/siteroot/httpdocs/

Open it up, replace:

/your/backup/folder
I recommend you use a place outside of the WWW tree, for example, /home/backups/mysite/

sitedata
This is just the tag for the beginning of the archive name. For example if you were running vB.org, you might use vborg-`date --iso-8601`.tar

/path/to/your/siteroot/httpdocs/
This is the absolute path where your site is located on your webserver. I'm using httpdocs because I have no httpsdocs and no need to backup the logfiles on a regular basis (Mostly because I don't care about them).

Verbose Switch
The -v switch just enables verbose (listing files out as it adds them) mode. Ideally you should probably remove it and use simply -czf for the switch.

Example: /var/www/vhosts/yoursitename.com/httpdocs. This will depend on where your site is installed and the way apache is setup. If you are unsure, go to your site's root folder and type pwd, it will tell you the location that you're working out of.

Output of it is a tar archive, sitedata-(the date).tar.gz

Putting it in cron.weekly will run it every Sunday night at Midnight, and give you a file that looks like this:


[cron.weekly]# sh sitedata.sh
[cron.weekly]# ls /home/backups/
sitedata-2006-01-19.tar.gz
[cron.weekly]#

Note: If you don't have access to /etc on your webserver, but have a crontab for your account at your host, you should use the absolute directory path and save it to a location within your host.

An example would be: /var/www/vhosts/yourdomain.tld/httpdocs/backups

To Use This inline with database backup as referenced in This Post (http://www.vbulletin.org/forum/editpost.php?do=editpost&p=875820):

This will backup your database and all raw files weekly, replace with your pathnames/username/passwords, etc. The Sleep operators in there are just for my own peace of mind to give the CPU a quick chance to rest and are probably not necessary, just my preference.


#! /bin/bash
# Weekly Site Backup
sleep 5
mysqldump --opt -Q -u dbuser -pdbpass dbname > /path/to/backups/db-`date --iso-8601`.sql
sleep 5
tar -czf /path/to/backups/data-`date --iso-8601`.tar.gz /path/to/yourdomain.tld/httpdocs/
sleep 5


This will give you the following two files weekly:

data-2006-01-19.tar.gz - The files
db-2006-01-19.sql - The database

Backup your normal way before trying this out, and if you are at all unsure, post here and wait for an answer before running an untested script on a live site.

mate, the link (http://www.vbulletin.org/forum/editpost.php?do=editpost&p=875820) isn't working for me, why? I'm a registered and full licensed user. :(

Whoops. My link was linked to editing it. Fixed, thanks. :)

Whoops. My link was linked to editing it. Fixed, thanks. :)

another question, i want to make an automatic backup from my board daily, can i use this hack aswell?

Yep, just put the script in cron.daily instead.










privacy (GDPR)