Thoughts on the automatic backup program for website files

Source: Internet
Author: User

Abstract:
This article provides an idea of using the asp php script to back up website files. You can back up specified files by day.
Personal websites are often deployed on virtual hosts and have little control permissions on the hosts. Therefore, you cannot use scheduled tasks for Scheduled backup. We need to use a different approach to achieve similar automatic backup.
We can use users' access to the website to back up specified files by day.
The basic idea is: the user accesses the website → reads the last backup date, reads the current date, and then compares it. If the two dates are inconsistent, the backup program is called to back up the specified file, after the backup is complete, a new date mark is written. At this time, the user accesses the website again. If the date is the latest, the backup program will be skipped and other programs will be executed.
Analysis of this idea: the backup program will be started when the first user visits the website every day, and the result of daily backup can be achieved. However, if there is no user access on this day, it will not be backed up, this does not matter much, because if no user visits the website, the website content will not change significantly. Therefore, you can ignore this situation.
Note that two people may access the website at the same time and start the backup program at the same time, which may overwrite the backup content. In this case, we can add judgment statements in the program, if the file already exists, it will not be overwritten.
If the file names of the two files to be backed up are the same, the file will be overwritten. We assume that the files are all different.
Another important issue is that the time format used in the backup program must be 4-digit year and 2-digit month and day, and the number of insufficient digits must be zero. For example: Only in this way can we compare the date size. In asp, we can use a small program to achieve this. in php, there is already such a date format.
The following describes the program construction in detail. asp is used as an example.
=== Bak_set.asp ===

Copy codeThe Code is 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 setting file. root_dir is the path of the home directory of the website, a global setting of the website, and the rest is the Backup Settings. We need to know the file name and path to be backed up, backup location, backup retained for several days, backup file name prefix, two frequently changed settings are the list of the last backup date and backup date. All path settings must end "/".
Iso standard date functions:Copy codeThe Code is 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

Backup functions:
Read the Backup Settings to the variables in the function and obtain the server path of the website.
Check whether the backup master folder (for example, backup/) exists and whether the current backup folder (for example, backup/2006-05-30/) exists, save the file to be backed up to the current backup folder;
Calculate the retention period of the backup file, read the previous backup date list, and then compare the two. If the date is earlier than the retention period, the backup file will be deleted, if you do not need to delete it, the date is converted to the new variable.
Generate new Backup Settings and write them to the original settings file.
The specific procedure is as follows:Copy codeThe Code is as follows: '========================================
'Function: regular backup program for files 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, the most important thing is security. If the backup file can be opened by the browser, the consequences may be very serious! Therefore, we should carefully select the backup folder. If the server permits access to the external directory of the website, we should also specify the backup path to the external directory of the website, for example: the root directory of the website is resolved to XXX/htdoc/, and you have the permission to read and write xxx/. Then you can add the backup file to xxx/backup/to ensure security. If you do not have such permissions, you must ensure that the files to be backed up are safe.

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.