首页 > 数据库 >sqlite3-windows 备份脚本 backup.bat

sqlite3-windows 备份脚本 backup.bat

时间:2022-10-28 14:39:26浏览次数:54  
标签:bat set date windows db echo 2% sqlite3 backup


sqlite3-windows 备份脚本 backup.bat 

注:其中 sqlite3 、gzip 需要手动下载,放到db目录,
下载地址:
    sqlite3.exe https://www.sqlite.org/cli.html
    gzip.exe http://gnuwin32.sourceforge.net/downlinks/gzip-bin-zip.php

命令格式:
    backup.bat test.db 200403
    #dump test.db 到 ./bac/test.db_200403.sql.gz 文件,bac 目录需要手动创建。

backup.bat 文件

::------------------------------------------------------------
@echo off
::命令行参数 1=数据库名 2=6位的日期
::例 backup.bat mingzi.db 200403

set db=%1%
set dt=%2%
set file=./bac/%db%_%dt%.sql.gz
set flag=0

echo db backup ...

if not defined db (
set flag=1
echo db 参数为空
)

if not defined dt (
set flag=2
echo dt 参数为空
)

if %flag% EQU 0 (
if exist %file% (
set flag=3
echo %file%,已存在!
)
)

if %flag% EQU 0 (
echo 命令: sqlite3 %db% .dump ^| gzip -c ^>%file%
echo [%db%] 库备份到 [%file%]
sqlite3 %db% .dump | gzip -c >%file%
)

echo complete!
::pause 查看则打开
@echo on
::------------------------------------------------------------

如果需要备份多个库,执行 backup-all.bat(双击执行即可)

backup-all.bat 文件

::------------------------------------------------------------
@echo off

echo %date%
echo %time%

::2020/04/03 周五
::17:14:21.91

::日期格式化
if "%date:~2,2%" lss "10" (set yy=0%date:~2,2%) else (set yy=%date:~2,2%)
if "%date:~0,4%" lss "10" (set yyyy=0%date:~0,4%) else (set yyyy=%date:~0,4%)
if "%date:~5,2%" lss "10" (set mm=0%date:~6,1%) else (set mm=%date:~5,2%)
if "%date:~8,2%" lss "10" (set dd=0%date:~9,1%) else (set dd=%date:~8,2%)
if "%time:~0,2%" lss "10" (set hh=0%time:~1,1%) else (set hh=%time:~0,2%)
if "%time:~3,2%" lss "10" (set nn=0%time:~4,1%) else (set nn=%time:~3,2%)

echo [yyyy=%yyyy% yy=%yy% mm=%mm% dd=%dd% hh=%hh% nn=%nn%]

set dt=%yy%%mm%%dd%

::call backup.bat test.db %dt%

:: test1、test2 替换为你的库
call backup.bat test1.db %dt%
call backup.bat test2.db %dt%

::pause
@echo on
::------------------------------------------------------------

 

标签:bat,set,date,windows,db,echo,2%,sqlite3,backup
From: https://blog.51cto.com/u_4518216/5804841

相关文章