[How To] Automated Filesystem Datestamp Backup using CRON via shell
by
19 Jan 2006
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: 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. Code:
#! /bin/bash # Compressed Directory Tree Backup tar -czvf /your/backup/folder/sitedata-`date --iso-8601`.tar.gz /path/to/your/siteroot/httpdocs/ /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: Code:
[cron.weekly]# sh sitedata.sh [cron.weekly]# ls /home/backups/ sitedata-2006-01-19.tar.gz [cron.weekly]# An example would be: /var/www/vhosts/yourdomain.tld/httpdocs/backups To Use This inline with database backup as referenced in 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. Code:
#! /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 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. |