A little thought about the automatic Backup program of website files _ application Skills

Source: Internet
Author: User
Tags php script

Summary:
This article provides a way to use ASP PHP script to realize the backup of the website files, and can make the day backup of the specified files.
Personal sites are often on the virtual host, the control of the host is very small, so can not use such as task scheduling to achieve scheduled backups, we need to change a way of thinking to achieve similar automatic backup.
We can use the user's access to the site to implement a day-by backup of the specified file.
The basic idea is: users visit the website → Read the last backup date, read the current date and then compare, if the two dates are inconsistent, call the backup program to implement a backup of the specified file, the backup is completed and then write a new date tag, when users visit the site, the date is already the latest, The backup program is skipped and other programs are executed.
Analyze This idea: The Backup program will be launched every day when the first user visits the site, it can achieve a daily backup effect, however, if there is no user access this day will not be backed up, this does not matter much, because if no user access to the site, the content of the site will not change significantly. So you can ignore this situation.
It is important to note that there may be two people accessing the Web site at the same time, while starting the backup program, it may overwrite the contents of the backup, for which we can add a judgment statement in the program, if the file already exists then do not overwrite it.
If the file name of the two files to be backed up is also overwritten, we assume that the files are all different.
A more important issue is that the period format that must be used in the backup program is 4-digit years and 2-digit months, days, and the number of low digits must be 0. For example: 2006-05-31 only in this way to achieve the date size of the comparison, in the ASP we can use a small program to implement, in PHP already have such a date format.
The following is a detailed description of the structure of the program, taking the ASP as an example.
===bak_set.asp===

Copy Code code as follows:
Dim Root_dir
Root_dir = "/"
Dim Bak_set
Set Bak_set = Server.CreateObject ("Scripting.Dictionary")
Bak_set. Add "Last_bak", "2006-05-30"
Bak_set. Add "File_list", "Data.mdb|system.mdb"
Bak_set. Add "File_path", "database/|database/"
Bak_set. Add "Bak_dir", "backup/"
Bak_set. Add "Bak_date", "2006-05-27|2006-05-28|2006-05-29|2006-05-30"
Bak_set. Add "Perfix", "@"
Bak_set. Add "Date_out", "2"
The above is the backup settings file, Root_dir is the site's home directory path, the site is a global setting, the rest is the backup settings, we need to know: To back up the file name, path, where the backup, backup for a few days, backup file name prefix, The two frequently changing settings are the last backup date and the list of dates that were backed up. End of "/" is required in all path settings
ISO Standard DATE function:
Copy Code code as follows:
Function isodate (str_date) Dim Temp
If IsDate (str_date) Then
temp = year (str_date) & "-" & Right ("0" &month (str_date), 2) & "-" & Right ("0" & Day (Str_date), 2)
Else
temp = Str_date
End If
Isodate=temp
End Function

The idea of a backup function:
First read the backup settings to the variables in the function, and get the server path of the Web site
Check that the Backup home folder (for example: backup/) exists, whether the current backup folder (for example, backup/2006-05-30/) exists, and then save the files that need to be backed up to the current backup folder;
Calculates the shelf life of the backup file, reads the list of previous backup dates, and then compares the backup files to delete the backed-up file if the date is less than the retention period, and then dump the date into the new variable if no deletion is required.
Generate a new backup setting and write it to the original settings file.
The specific procedures are as follows:
Copy Code code as follows:
'==================================
' Function: File timed backup programs backup files
' Need Var:root_dir, Bak_set, Isodate ()
' Need file bak_set.asp
' Return:true/false
'==================================
Function Bak_start ()
' 1
Dim Perfix
Perfix = Bak_set ("Perfix")
Dim files
Files = Split (Bak_set ("File_list"), "|")
Dim paths
paths = Split (Bak_set ("File_path"), "|")
Dim now_date
Now_date = isodate (date)
' Response. Write (Now_date)
Dim Sev_root
Sev_root = Server.MapPath (Root_dir)
Dim Bak_root
Bak_root = sev_root & "\" & Bak_set ("Bak_dir")
Dim Bak_dir
Bak_dir = bak_root & "\" & now_date & "\"
' 2 Create Backup
Dim FSO
Set FSO = Server.CreateObject ("Scripting.FileSystemObject")
If not FSO. FolderExists (bak_root) Then fso. CreateFolder (Bak_root)
If not FSO. FolderExists (Bak_dir) Then fso. CreateFolder (Bak_dir)
' Response. Write (Bak_dir)
If Ubound (Files) > Ubound (Paths) Then
Bak_start = False
Exit Function
End If
Dim I
' On Error Resume Next
For i = 0 to Ubound (files)
' Response. Write (Sev_root & "\" & Paths (i) & Files (i))
If FSO. FileExists (Sev_root & "\" & Paths (i) & Files (i)) and not FSO. FileExists (Bak_dir & perfix & Files (i)) Then
Fso. CopyFile Sev_root & "\" & Paths (i) & Files (i), Bak_dir & perfix & Files (i)
End If
Next
' 3 Delete out of date backup
Dim Date_out
Date_out = Isodate (Date-abs (Bak_set ("Date_out"))
' Response. Write (Date_out)
Dim Dates
Dim bak_date
Bak_date = ""
Dates = Split (Bak_set ("Bak_date"), "|")
For i = 0 to Ubound (dates)
If Dates (i) < date_out Then
' On Error Resume Next
If FSO. FolderExists (Bak_root & "\" & Dates (i)) Then fso. DeleteFolder bak_root & "\" & Dates (i)
Else
Bak_date = bak_date & Dates (i) & "|"
End If
Next
Bak_date = bak_date & now_date
Bak_set ("bak_date") = Bak_date
' 4 Update settings
Dim F
Set f = fso. OpenTextFile (Server.MapPath ("bak_set.asp"), 2,true) ' 2 write
Dim temp, keys
temp = "<%" &vbcrlf & _
"Dim Root_dir" &vbcrlf & _
"Root_dir =" "" & Root_dir & "" "&vbcrlf & _
"Dim Bak_set" &vbcrlf & _
"Set Bak_set = Server.CreateObject (" "Scripting.Dictionary" ")" &vbcrlf
Keys = Bak_set. Keys
For i = 0 to Ubound (keys)
Temp = temp & "Bak_set. Add "" "&keys (i) &" "" "" & Bak_set (keys (i)) & "" "& VbCrlf
Next
Temp = temp & "%" & ">"
F.write Temp
F.close
Set FSO = Nothing
Set F = Nothing
Bak_start = True
End Function
Finally, and most importantly, that is the security issue, if the backup file can be opened by the browser, the consequences can be very serious! Therefore, the backup folder should be carefully selected, if the server allows access to the external directory of the site, then the backup path should be assigned to the outside of the site, for example: the root directory of the site is resolved to xxx/htdoc/and you have read and write access to it xxx/then you can assign the backup file to Xxx/backup /This is more secure. Without such permissions, it is important to ensure that the files that need to be backed up are inherently secure.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.