Scheduled backup of remote VPS (CentOS) data batches on a Windows host _mysql

Source: Internet
Author: User
Tags mkdir ssh vps pscp
But after the toss of the VPS, it is not just to back up MySQL, some independent site data also need to back up.
If you have a third-party Linux host or Amazon-like cloud storage, it is much simpler, linux-linux backup is convenient.
But I want to run a backup every day/week under my Windows7, and I have this gadget.

The approximate idea is as follows:
Use Putty to connect to VPS with Plink.exe, run backup and exit login;
The backup is then copied to the local computer using the Putty Pscp.exe;
Add a scheduled task, run it automatically every day/week, and implement a scheduled backup.

Copy Code code as follows:

Index.bat responsible for triggering backups under Windows
@echo Off & setlocal enableextensions

:: This is just SSH connection and backup path configuration information
:: Please also modify the same directory in the Linux file to back up the path and MySQL database

:: SSH Related information
Set server=192.168.1.100
Set port=22
Set User=root
Set Password=password

:: Corresponds to the path configured in the Linux file in the same directory
Set Backup_path=/root/backup

:: Backup to the native location to download (automatically created)
Set Save_path=d:\backup\

::----------configuration ends, do not modify the following----------

:: Run-time variables
Set Exe_ssh=%~dp0\exe\plink.exe
Set Exe_scp=%~dp0\exe\pscp.exe
Set Linux=%~dp0\linux

Set year=%date:~0,4%
Set month=%date:~5,2%
Set day=%date:~8,2%
Set true_path=%save_path%%year%-%month%-%day%\

:: Creating a backup path
echo Creating Backup Dir ...
If not exist%true_path% (
mkdir%true_path% 2>nul
)
If not exist%true_path% (
echo Backup path:%true_path% NOT exists, create Dir failed.
Goto exit
)
echo Creating Backup Dir ... Done.

:: Generating backup directories and files
echo Connecting to remote server and creating backups ...
%exe_ssh%-C%user%@%server%-P%PORT%-pw%password%-M%linux% 2>nul
echo Connecting to remote server and creating backups ... Done.

:: Download Backup
echo Downloading backups ...
%exe_scp%-PW%password%-P%PORT%%user%@%server%:%backup_path%/*.*%true_path%
echo Downloading backups ... Done.

: Exit
Echo Exit

Linux-side commands called by Index.bat
Copy Code code as follows:

#!/bin/bash

Path=/usr/local/sbin:/usr/bin:/bin

#----------Configuration items, do not last/----------

# The path to the backup file store, keep in line with the Backup_path in Index.bat
Backup_path=/root/backup

# files or directories to back up, multiple spaces separated
Files= "/WWW/CMSTOP/WWW/DBPMA"

# mysqldump The full path of the executable file (if you can directly execute the mysqldump without the full path)
Mysqldump=mysqldump

# MySQL database to back up, multiple spaces separated
databases= "MySQL test"

# MySQL User Name
User=root

# MySQL Password
Passwd=

#----------Configuration entry ends, the following do not need to be modified----------

# Run-time variables
datetime=$ (date-d now +%y-%m-%d)
curpath=$ (CD "$ (dirname" $) "; pwd)

# Create backup directory
if [!-D $BACKUP _path]; Then
Mkdir-p $BACKUP _path
Fi

RM-FR $BACKUP _path/*

# Backup MySQL Database
CD $BACKUP _path
For database in $DATABASES
Todo
If ["$PASSWD" = ""]; Then
$MYSQLDUMP-u$user $database > $database. $DATETIME. Dump.sql
Else
$MYSQLDUMP-u$user-p$passwd $database > $database. $DATETIME. Dump.sql
Fi
Tar czf $database. $DATETIME. dump.sql.tar.gz $database. $DATETIME. Dump.sql
Rm-f $database. $DATETIME. Dump.sql
Done

# Backup files or directories
For file in $FILES
Todo
Tar czf $file. $DATETIME. tar.gz $file
MV $file. $DATETIME. tar.gz $BACKUP _path
Done

# complete
CD $CURPATH
Exit

The backup files generated on the server are not deleted after the download is complete because they are automatically cleared before the next backup.
Download and use the download address
Download the file to your native, unzip it to a certain place, and then modify the configuration items according to the instructions above;
Because SSH first login needs to add Key to this machine, this step can not be simply ignored, the feasible way is to load into the Putty session, but very troublesome, or run a simple, we want to perform an SSH login:
Copy Code code as follows:

:: Switch to the directory you extracted to, such as D:\VPS\:
CD/D D:\VPS\

:: Modify the following parameters as appropriate
Exe\plink.exe-c root@192.168.1.100-p 22-pw Password

:: Wait for a moment, will be prompted to join the Key to the local Trust host list, enter Yes on it.
Yes

:: This goes in, input point commonly used command play, enter exit exit.
Exit

According to the introduction of this article, add a scheduled task to perform regularly;
Manually perform the scheduled tasks added above or observe an execution cycle to see if the backup is valid.
Pending improvement
As with previous scripts, there seems to be a lack of the ability to automatically delete the number of days before the backup.
Related Article

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.