Array
(
)
Parser stuck: '
|
Now for the fun part.
We will write a backup script using PHP, tar and gzip. This backup script will keep compressed copies of some directories we define, one per day for one week. Once per week we will also save a weekly version and keep this for 4 weeks. Finally, once per month we keep a monthly version for 6 months. This way you wil almost certainly have a good version at thand in case something gets lost. So, let's look at the complete script and then go through it line by line: SimpleBackup.php
# 0: <?
No, this is really not as scary as it may look so let's go
Line 4-7 simply declare an array with the directories we want to backup. The key will make up the name of the compressed backup file (so make sure you have no whitespace or funny characters in there), the values are the directories.
Line 10 will be the path to your backup directory. Make sure you have write permissions there! In this directory you have to create the subdirectories daily/, weekly/ and monthly/.
#0: <?
just extend our include paths to the usual directories where you will normally find the tar binary that we're going to use later.
#0: <?
Will get the time for the backup timestamps. The -14400 simply means that we are looking for the name of the day that we had 14400 seconds = 4 hours before. The reason for this is that I like to run this script with cron every night at 3:30 when there is really nothing going on on the server otherwise.
#0: <?
This defines the directory for the weekly backup files, and if it doesn't exist tries to create it.
#0: <?
This defines the directory for the monthly backup files, and if it doesn't exist tries to create it.
#0: <?
This sets the directory for the daily backup files, and if it doesn't exist tries to create it.
Now the actual backup start. We will loop through the array of directories to save, and for each one we call the program tar.Together with the options c (create), z (compress it with gzip) and f (force mode - "just do it, don't complain") this will create a compressed archive of each directory.
# 0: <?
In case we have reached a week or month start we copy this archive to the respective directories as well.
That's it!
If you want to use this script you can download it here
Note: Instead of compressing the archives with gzip you can also use the more effective (but somewhat slower) bzip2. Just change "tar -czf" to "tar -cjf". and the file suffixes ".tgz" to "tar.bz2"
|
||
| Previous:Using *NIX tools in your script | Start | Next: Executing the backup automatically |
| .GR - Domains for you |
No residence restrictions - anybody can register
Registration starting at € 16,33 / year
Web Hosting incl. .gr-Domain from € 8,90 / month
|