Home Database Mysql Tutorial A solution for database remote full backup

A solution for database remote full backup

Dec 15, 2016 pm 04:01 PM
database backup

A solution for remote full database backup

--exec BackUPDatabase_MaJiatao 'pubs','\XZ154ABC$','16:50:00.000',1,'XZ154MaJiatao','MaJiatao'/***************************************************describe :Database full backup and incremental backup Written by: Ma Jiatao Modified by Ma Jiatao: 2014-02-12: 1. Added the backup path to choose local and remote paths 2. Modified the way to save historical backup records, no longer requiring the local hard disk Text file on the file to be used as the storage medium****************************************** *********** /if object_id('BackUPDatabase_MaJiatao') is not nulldrop PRoc BackUPDatabase_MaJiatao

GO

alter proc BackUPDatabase_MaJiatao@database_name sysname,--The name of the database to be backed up@physical_backup_device_name sysname,--The backup file storage directory@all_backup_datetime char(17)=' 20:00:00.000',--Full backup time@IntDistance int=1,--Full backup time range (hours)@UserName varchar(100),--Remote server login name@PassWord varchar(100)=' '--Remote server login password with ENCRYPTION as

/*********************************declare @database_name sysname,--the name of the database to be backed up @physical_backup_device_name sysname,--backup File storage directory @all_backup_datetime char(17)select @database_name='test',@physical_backup_device_name='E: Backup file query server',@all_backup_datetime='16:00:00.000'

************ ********************************/

--Create backup history if not exists (select * from dbo.sysobjects where id = object_id(N'backup_recorder') and OBJECTPROPERTY (id, N'IsUserTable') = 1) exec('CREATE TABLE backup_recorder (backup_datetime datetime not null,backup_name varchar (500) PRIMARY KEY,backup_path varchar (500) NOT NULL ,is_all_backup char(1) not null default 0,file_is_exists char(1) not null default 0)')elsebeginif not exists(select * from syscolumns where name='file_is_exists' and ID=object_id(N'backup_recorder'))begindrop table backup_recorderexec('CREATE TABLE backup_recorder (backup_datetime datetime not null, backup_name varchar (500) PRIMARY KEY,backup_path varchar (500) NOT NULL ,is_all_backup char(1) not null default 0,file_is_exists char(1) not null default 0)')endend

declare @backup_set_full sysname,@backup_set sysname, --Backup file name @backup_name sysname

declare @Return_Int intdeclare @CommandText nvarchar(4000)declare @DelFilePathName nvarchar(4000)

declare @physical_backup_device_name_now nvarchar(4000)

debackclare @physical_device_name backup nvarchar(4000)

if isnull( @database_name,'')='' or rtrim(@database_name)=''--The database name is empty set @database_name=db_name()--Back up the current database

if isnull(@physical_backup_device_name,'')='' or rtrim(@physical_backup_device_name)=''--The backup directory is empty, use the system default directory beginSELECT @physical_backup_device_name=ltrim(rtrim(reverse(filename))) FROM master.dbo.sysdatabases where name=@database_nameset @physical_backup_device_name=reverse( substring(@physical_backup_device_name,charindex('',@physical_backup_device_name)+5,260))+'backup'end

--Determine whether the path is a network path or a local path if left(@physical_backup_device_name,2)='\' and ltrim( rtrim(@UserName))<>'' and ltrim(rtrim(@Password))<>''beginselect @CommandText='net use '+@physical_backup_device_name+' "'+@Password+'" /user:' + @UserName exec master.. 0 -- The directory does not exist, create beginselect @CommandText='Mkdir '+@physical_backup_device_name+'Full backup'exec @Return_Int=master..xp_cmdshell @CommandText, no_outputend

select @CommandText='dir '+@physical_backup_device_name+'Differential backup' exec @Return_Int=master..xp_cmdshell @CommandText, no_outputif @Return_Int<>0 --The directory does not exist, create beginselect @CommandText='Mkdir '+@physical_backup_device_name+'differential backup'exec @Return_Int=master..xp_cmdshell @CommandText, no_outputend

select @physical_backup_device_name_now=@database_name+'_'+ltrim(rtrim(REPLACE(REPLACE(REPLACE(REPLACE(convert(char(23),getdate(),21),'-',''),':' ,''),'.',''),' ','')))+'.bak'

if object_id('tempdb..#backup_recorder') is not nulldrop table #backup_recorderCREATE TABLE #backup_recorder (backup_datetime datetime not null,backup_name varchar (500) PRIMARY KEY,backup_path varchar (500) NOT NULL ,is_all_backup char(1) not null default 0,file_is_exists char(1) not null default 0)

--Check if there is a full backup select @CommandText='dir '+@physical_backup_device_name+'Full backup*.bak'exec @Return_Int=master..xp_cmdshell @CommandText, no_output

if @Return_Int<>0 --No The full backup file exists, perform a full backup beginselect @backup_set_full='full backup'+@database_nameselect @physical_backup_device_namebackup=@physical_backup_device_name+'full backup'+@physical_backup_device_name_now

--Full backup, rewrite the media header BACKUP DATABASE @database_name to DISK=@ physical_backup_device_namebackup WITH FORMAT ,NAME = @backup_set_fullif @@error=0--Backup successful, delete all historical backup files before the full backup of the day begin--Write backup log insert into backup_recorder(backup_datetime,backup_name,backup_path,is_all_backup,file_is_exists)values( getdate(),@physical_backup_device_name_now,@physical_backup_device_namebackup,'1','1')insert into #backup_recorder(backup_datetime,backup_name,backup_path,is_all_backup,file_is_exists)select backup_datetime,backup_name,backup_path,is_all_backup,file_is_existsfrom backup_recorderwhere backup_name<&g t;@physical_backup_device_name_now and is_all_backup='1' and file_is_exists='1'endendelsebegin--If there is a full backup, verify whether the full backup is the specified time of the previous day--select @all_backup_datetime=REPLACE(REPLACE(@all_backup_datetime,':','') ,'.','')if right(left(right(@physical_backup_device_name_now,21),17),9) between REPLACE(REPLACE(@all_backup_datetime,':',''),'.','') and REPLACE(REPLACE(substring(convert(char(23),dateadd(hh,@IntDistance,@all_backup_datetime),21),12,12),':',''),'.','')--Proceed Full backup beginselect @backup_set_full='Full backup'+@database_nameselect @physical_backup_device_namebackup=@physical_backup_device_name+'Full backup'+@physical_backup_device_name_now--Full backup, rewrite the media header BACKUP DATABASE @database_name to DISK=@physical_backup_device_namebackup WITH FORMAT ,NAME = @backup_set_fullif @@error=0--Backup successful begin--Write backup log insert into backup_recorder(backup_datetime,backup_name,backup_path,is_all_backup,file_is_exists)values(getdate(),@physical_backup_device_name_now,@physical_backup_device_namebackup,'1','1')- -Find historical backup files insert into #backup_recorder(backup_datetime,backup_name,backup_path,is_all_backup,file_is_exists)select backup_datetime,backup_name,backup_path,is_all_backup,file_is_existsfrom backup_recorderwhere backup_name<>@physical_backup_device_name_now and is_all_backup='1' and file_is_exists=' 1'endendelse --The current backup time is less than the specified full backup time, perform differential backup begin

select @backup_set_full='incremental backup'+@database_nameselect @physical_backup_device_namebackup=@physical_backup_device_name+'differential backup'+@physical_backup_device_name_now--differential backup, append media BACKUP DATABASE @database_name to DISK=@physical_backup_device_namebackup WITH NOINIT, DIFFERENTIAL,NAME = @backup_setif @@error=0--Backup successful begin--Write backup log insert into backup_recorder(backup_datetime,backup_name,backup_path,is_all_backup,file_is_exists)values(getdate( ),@physical_backup_device_name_now,@physical_backup_device_namebackup,'0','1')--Find historical backup files insert into #backup_recorder(backup_datetime,backup_name,backup_path,is_all_backup,file_is_exists)select backup_datetime,backup_name,backup_path,is_all_backup,file_is_existsfrom backup_recorderwhere backup_name< ; >@physical_backup_device_name_now and is_all_backup='0' and file_is_exists='1'endendend

DECLARE DelFilePathName CURSOR FORWARD_ONLY FOR select backup_path From #backup_recorder OPEN DelFilePathNameFETCH NEXT FROM DelFilePathName into @DelFilePathNameWHILE @@FETCH_STATUS = 0beginif exists(select *from backup_recorder where backup_path =@DelFilePathName and backup_name<>@physical_backup_device_name_now)beginselect @CommandText='del '+@DelFilePathNameexecute @Return_Int=master..xp_cmdshell @CommandText--,no_outputif @Return_Int=0 beginupdate backup_recorder set file_is_exists=0 where backup_path=@DelFileP athNameendendFETCH NEXT FROM DelFilePathName into @DelFilePathNameendCLOSE DelFilePathNameDEALLOCATE DelFilePathName

if object_id('tempdb..#backup_recorder') is not nulldrop table #backup_recorder

if left(@physical_backup_device_name,2)='\' and ltrim(rtrim(@UserName))< ;>'' and ltrim(rtrim(@Password))<>''beginselect @CommandText='net share '+@physical_backup_device_name+' /delete'exec master..xp_cmdshell @CommandText,no_outputend

The above is the database remote A solution for full backup. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and PDO: How to perform database backup and restore operations PHP and PDO: How to perform database backup and restore operations Jul 29, 2023 pm 06:54 PM

PHP and PDO: How to perform database backup and restore operations When developing web applications, database backup and restore are very important tasks. As a popular server-side scripting language, PHP provides a wealth of libraries and extensions, among which PDO (PHP Data Objects) is a powerful database access abstraction layer. This article will introduce how to use PHP and PDO to perform database backup and restore operations. Step 1: Connect to the database Before actual operation, we need to establish a connection to the database. Use PDO pair

Analysis of project experience on MySQL database backup and recovery performance optimization Analysis of project experience on MySQL database backup and recovery performance optimization Nov 02, 2023 am 08:53 AM

In the current Internet era, the importance of data is self-evident. As one of the core components of Internet applications, database backup and recovery work is particularly important. However, as the amount of data continues to increase and business requirements become increasingly complex, traditional database backup and recovery solutions can no longer meet the high availability and high performance requirements of modern applications. Therefore, optimizing the backup and recovery performance of MySQL database has become an urgent problem that needs to be solved. In practice, we have adopted a series of project experiences to effectively improve MySQL data

How to use ThinkPHP6 to implement database backup and recovery How to use ThinkPHP6 to implement database backup and recovery Jun 20, 2023 pm 07:25 PM

In the process of developing business systems, the database is a very important part. Therefore, backing up and restoring the database is a very necessary operation. This article will combine examples of the ThinkPHP6 framework to introduce how to use ThinkPHP6 to implement database backup and recovery. 1. Database backup 1.1 Environment preparation Before performing database backup, you need to confirm the following points: 1. You need to set the bin directory address of the mysql database and add its path to the system Path variable; 2. Mysqld needs to be installed

How to backup database in Golang? How to backup database in Golang? Jun 01, 2024 am 11:56 AM

Backing up your database in Golang is crucial to protecting your data. You can use the database/sql package in the standard library, or a third-party package such as github.com/go-sql-driver/mysql. Specific steps include: Connect to the database. Create a file to store the backup data. Use the Dump function or Exporter to back up the database to a file.

PHP and Memcached database backup and recovery PHP and Memcached database backup and recovery May 15, 2023 pm 09:12 PM

With the rapid development of the Internet, large-scale MySQL database backup and recovery has become one of the essential skills for major enterprises and websites. With the widespread application of Memcached, how to back up and restore Memcached has also become an important issue. As one of the main languages ​​for web development, PHP has unique advantages and skills in handling backup and recovery of MySQL and Memcached. This article will introduce in detail the implementation method of PHP processing MySQL and Memcached backup and recovery.

How to use thinkorm to implement database backup and restore How to use thinkorm to implement database backup and restore Jul 28, 2023 pm 02:05 PM

Title: Using ThinkORM to realize database backup and restoration Introduction: In the development process, database backup and restoration is a very important task. This article will introduce how to use the ThinkORM framework to implement database backup and restoration, and provide corresponding code examples. 1. Background introduction During the development process, we usually use databases to store and manage data. The principle of database backup and restore is to perform regular backups of the database so that the data can be quickly restored in the event of database problems or data loss. With the help of

Database backup, optimization and recovery of Pagoda Panel Database backup, optimization and recovery of Pagoda Panel Jun 21, 2023 am 09:45 AM

In today's online world, websites have become an important carrier for every enterprise, organization or individual to display their brands, services, products, etc. In order to ensure the normal operation and security of the website, we need to continuously back up and optimize the database. and recovery. As a server management software with simple operation, rich functions and beautiful interface, Pagoda Panel is also quite excellent in database management and has important functions such as backup, optimization and recovery. This article will focus on the database backup, optimization and recovery functions of Pagoda Panel and related concerns.

How to implement differential backup of database backup in PHP How to implement differential backup of database backup in PHP May 16, 2023 am 08:15 AM

As the amount of data continues to increase, database backup becomes more important. Regular backup of the database can ensure data security, but traditional full backup takes up a lot of storage space and takes a long time to backup. How to implement differential backup has become a problem that needs to be solved during the backup process. In PHP applications, the way to implement differential backup of database backup is to use incremental backup and differential backup. The principle of incremental backup is to back up only changed data. First perform a full backup, and then only back up the full backup every time if there are changes in the future

See all articles