Use batch files to back up databases offsite (data in recent days) _vbs

Source: Internet
Author: User
Tags ftp commands ftp connection goto rar remote ftp server system log oracle database

In order to ensure the recoverability of the data in the server, a remote backup database scheme is used. Offsite backup data are divided into off-site backup file data and off-site backup database data two kinds. The off-site backup file data is designed to back up files uploaded by users in the site. and offsite backup database data is the database of data backup files uploaded to the remote server.

First, the implementation process and principles are:

1. Need to install WinRAR software, used to compress multiple files to be backed up into a compressed file.
2. Write back steps:
Directory is located under the WinRAR Software installation path
cd C:\Program Files\winrar
Execute Rar.exe Program
RAR a-r-y c:\c.rar c:\c\a c:\c\b
Connect to a remote FTP server where you want to write ftp://ftp.txt/files
Ftp-s:c:\ftp.txt 192.168.0.1
Can delete local backup files
del/f/q C:\c.rar
Catalog Relocation
cd\
The complete code is as follows (Back.bat):

Copy Code code as follows:

cd C:\Program Files\winrar
RAR a-r-y c:\c.rar c:\c\a c:\c\b
Ftp-s:c:\ftp.txt 192.168.0.1
del/f/q C:\c.rar
cd\

3. Write the remote FTP connection configuration file

User name
Password
CD path
Put local file path and filename remote file path and filename
Bye for exit
The complete code is as follows (Ftp.txt):
Admin
Heroic
Put C:\c.rar C.rar
Bye
4. Run the bat file regularly with the "Scheduled Tasks" feature of Windows. (by "Start"-> "-> System Tools"-> "Task Scheduler")
5. Backup Oracle database commands are:
Exp userid/pwd@servicename file = "Backupfilepath" owner = user
Note UserID for the system administrator, backupfilepath the file save path (file suffix is dmp), user is a backup for that users, if not write this parameter, the default for UserID users to backup
For example:
Exp user/pwd@servername file=d: "Backup Oracle" ORACLE%DATE:~0,10%.DMP owner=system log=d: "Backup Oracle" Oracle%date : ~0,10%.log
(where%date:~0,10% is to take the current system time.) 0 is start position, 10 is end position)

Copy Code code as follows:

Set Files=c:\backup\%date:~0,10%.rar
Set Dbfilesdmp=c:\backup\%date:~0,10%db.dmp
Set userdb= "Jbpmdemo"
Set pwddb= "Jbpmdemo"
Set domain= "Master.jhserver"
Set Filespath=c:\c\a c:\c\b
Set ftpfile= "C:\backup\ftp.txt"
Set Ftpname=admin
Set Ftppwd=heroic
Set ftpfiles= "%date:~0,10%.rar"
Set ftpdbfiles= "%date:~0,10%db.rar"
Set ftpdomain= "192.168.0.1"
Set rarpath= "C:\Program Files\winrar"

@echo start backing up the database
Exp%userdb%/%pwddb%@%domain% file= "%dbfilesdmp%"

@echo Start compressing files
C:
cd C:\Program Files\winrar
RAR a-r-y%files%%filespath%%dbfilesdmp%

@echo Write File ftp
@echo%ftpname%>%ftpfile%
@echo%ftppwd%>>%ftpfile%
@echo put%files%%ftpfiles%>>%ftpfile%
@echo bye>>%ftpfile%
@echo Upload%files%.rar file
Ftp-s:%ftpfile% 192.168.0.1
del/f/q%files%

Database offsite backup for a site is very necessary, here to talk about the use of batch files to the database off-site process, the first need to prepare two files, one is ftp.txt (storage FTP command), one is movefile.bat (Storage dos command).

==========ftp.txt
The IP address of the open server
FTP User Name
FTP password
Bin
Get database name. mdb
Bye
Quit
The above is a simple example, specific to see your database storage path, a one-time access to multiple files. You can add FTP commands such as Cd,mget
==========movefile.bat
@echo off
Ftp-i-s:ftp.txt
REM date format is "YYYY-MM-DD"
Set dt=%date:~0,10%
Set td=%date:~0,10%

rem set/p dt= "Input Date:"
rem set/p td= "Input Date:"

Set I=1

: Forok
If%i%==1 goto i01
If%i%==2 goto i02
If%i%==3 goto i03
If%i%==4 goto i04
If%i%==5 goto i05
: i01
Set i=2
Goto Nextok
: i02
Set i=3
Goto Nextok
: i03
Set i=4
Goto Nextok
: i04
Set i=5
Goto Nextok
: i05
Set I=6
Goto Nextok
: Nextok

Set dy=%dt:~0,4%
Set dm=%dt:~5,2%
Set dd=%dt:~8,2%

If%dm%%dd%==0101 goto L01
If%dm%%dd%==0201 goto L02
If%dm%%dd%==0301 goto L07
If%dm%%dd%==0401 goto L02
If%dm%%dd%==0501 goto L04
If%dm%%dd%==0601 goto L02
If%dm%%dd%==0701 goto L04
If%dm%%dd%==0801 goto L02
If%dm%%dd%==0901 goto L02
If%dm%%dd%==1001 goto L05
If%dm%%dd%==1101 goto L03
If%dm%%dd%==1201 goto L06

If%dd%==02 goto L10
If%dd%==03 goto L10
If%dd%==04 goto L10
If%dd%==05 goto L10
If%dd%==06 goto L10
If%dd%==07 goto L10
If%dd%==08 goto L10
If%dd%==09 goto L10
If%dd%==10 goto L11
set/a dd=dd-1
Set dt=%dy%-%dm%-%dd%
Goto END
: L10
set/a dd=%dd:~1,1%-1
Set dt=%dy%-%dm%-0%dd%
Goto END
: L11
Set dt=%dy%-%dm%-09
Goto END

: L02
set/a dm=%dm:~1,1%-1
Set dt=%dy%-0%dm%-31
Goto END
: L04
set/a dm=dm-1
Set dt=%dy%-0%dm%-30
Goto END

: L05
Set dt=%dy%-09-30
Goto END
: L03
Set dt=%dy%-10-31
Goto END
: L06
Set dt=%dy%-11-30
Goto END
: L01
set/a dy=dy-1
Set dt=%dy%-12-31
Goto END

: L07
set/a "Dd=dy%%4"
If not%dd%==0 goto L08
set/a "Dd=dy%%100"
If not%dd%==0 goto L09
set/a "dd=dy%%400"
If%dd%==0 goto L09
: L08
Set dt=%dy%-02-28
Goto END
: L09
Set dt=%dy%-02-29
Goto END

: End
If%i%==2 goto Forok
If%i%==3 goto Forok
If%i%==4 goto Forok
If%i%==5 goto Forok
If%i%==6 Goto MDB

: MDB
echo Copy%td%
Echo del%dt%
Copy database name. mdb "%td%". mdb
Del "%dt%". mdb
Above is a backup of the last 5 days of data, one per day. You can modify the above code to the last 6, 7 .... N Days

such as: Modified into 6 days
If%i%==5 goto i05 under Add
If%i%==6 goto i06
: i05
Set I=6
Goto Nextok Increased under
: i06
Set i=7
Goto Nextok
If%i%==6 Goto MDB instead
If%i%==6 goto Forok
If%i%==7 Goto MDB

By analogy .... To N Day:-)

The above two files are ready and we can use Windows ' own Task Scheduler to schedule programs to automatically back up data every day.

Concrete steps: Control Panel--mission plan--Add task plan .... Choose Daily ... Select Movefile.bat
It's OK.

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.