{"id":1182958,"date":"2025-01-15T19:09:13","date_gmt":"2025-01-15T11:09:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182958.html"},"modified":"2025-01-15T19:09:17","modified_gmt":"2025-01-15T11:09:17","slug":"python%e6%95%b0%e6%8d%ae%e5%ba%93%e5%a6%82%e4%bd%95%e5%a4%87%e4%bb%bd","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1182958.html","title":{"rendered":"python\u6570\u636e\u5e93\u5982\u4f55\u5907\u4efd"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25132850\/c0205d7b-7c0e-4823-a645-781d76dc0daa.webp\" alt=\"python\u6570\u636e\u5e93\u5982\u4f55\u5907\u4efd\" \/><\/p>\n<p><p> <strong>Python\u6570\u636e\u5e93\u5907\u4efd\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6570\u636e\u5e93\u81ea\u5e26\u7684\u5907\u4efd\u5de5\u5177\u3001\u5229\u7528Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u6267\u884c\u5907\u4efd\u547d\u4ee4\u3001\u4f7f\u7528ORM\u6846\u67b6\u63d0\u4f9b\u7684\u5907\u4efd\u529f\u80fd\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u811a\u672c\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u5229\u7528Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u6267\u884c\u5907\u4efd\u547d\u4ee4<\/strong>\u662f\u4e00\u79cd\u8f83\u4e3a\u7075\u6d3b\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff08\u5982<code>sqlite3<\/code>\u3001<code>psycopg2<\/code>\u3001<code>pymysql<\/code>\u7b49\uff09\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u7136\u540e\u6267\u884c\u6570\u636e\u5e93\u7684\u5907\u4efd\u547d\u4ee4\u3002\u5177\u4f53\u64cd\u4f5c\u5305\u62ec\uff1a\u8fde\u63a5\u6570\u636e\u5e93\u3001\u6267\u884c\u5907\u4efd\u8bed\u53e5\u3001\u4fdd\u5b58\u5907\u4efd\u6587\u4ef6\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u6570\u636e\u5e93\u81ea\u5e26\u7684\u5907\u4efd\u5de5\u5177<\/h2>\n<\/p>\n<p><h3>1\u3001MySQL\u7684\u5907\u4efd\u5de5\u5177<\/h3>\n<\/p>\n<p><p>MySQL\u63d0\u4f9b\u4e86<code>mysqldump<\/code>\u5de5\u5177\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u5907\u4efd\u6570\u636e\u5e93\u3002\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u8c03\u7528\u8be5\u5de5\u5177\u8fdb\u884c\u5907\u4efd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def backup_mysql(db_name, user, password, output_file):<\/p>\n<p>    command = f&quot;mysqldump -u {user} -p{password} {db_name} &gt; {output_file}&quot;<\/p>\n<p>    subprocess.run(command, shell=True)<\/p>\n<p>backup_mysql(&#39;your_database&#39;, &#39;your_user&#39;, &#39;your_password&#39;, &#39;backup.sql&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001PostgreSQL\u7684\u5907\u4efd\u5de5\u5177<\/h3>\n<\/p>\n<p><p>PostgreSQL\u63d0\u4f9b\u4e86<code>pg_dump<\/code>\u5de5\u5177\uff0c\u540c\u6837\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u5907\u4efd\u6570\u636e\u5e93\u3002\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u8c03\u7528\u8be5\u5de5\u5177\u8fdb\u884c\u5907\u4efd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def backup_postgresql(db_name, user, output_file):<\/p>\n<p>    command = f&quot;pg_dump -U {user} {db_name} &gt; {output_file}&quot;<\/p>\n<p>    subprocess.run(command, shell=True)<\/p>\n<p>backup_postgresql(&#39;your_database&#39;, &#39;your_user&#39;, &#39;backup.sql&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001SQLite\u7684\u5907\u4efd\u5de5\u5177<\/h3>\n<\/p>\n<p><p>SQLite\u7684\u5907\u4efd\u76f8\u5bf9\u7b80\u5355\uff0c\u56e0\u4e3a\u5b83\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u5355\u4e00\u7684\u6587\u4ef6\u3002\u53ef\u4ee5\u76f4\u63a5\u590d\u5236\u6587\u4ef6\u8fdb\u884c\u5907\u4efd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>def backup_sqlite(db_file, backup_file):<\/p>\n<p>    shutil.copyfile(db_file, backup_file)<\/p>\n<p>backup_sqlite(&#39;your_database.db&#39;, &#39;backup.db&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u5229\u7528Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u6267\u884c\u5907\u4efd\u547d\u4ee4<\/h2>\n<\/p>\n<p><h3>1\u3001\u8fde\u63a5\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5\u76f8\u5e94\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u4f8b\u5982<code>sqlite3<\/code>\u3001<code>psycopg2<\/code>\u3001<code>pymysql<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>import psycopg2<\/p>\n<p>import pymysql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u6267\u884c\u5907\u4efd\u547d\u4ee4<\/h3>\n<\/p>\n<p><h4>1\u3001SQLite\u7684\u5907\u4efd<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def backup_sqlite(db_file, backup_file):<\/p>\n<p>    conn = sqlite3.connect(db_file)<\/p>\n<p>    with open(backup_file, &#39;w&#39;) as f:<\/p>\n<p>        for line in conn.iterdump():<\/p>\n<p>            f.write(&#39;%s\\n&#39; % line)<\/p>\n<p>    conn.close()<\/p>\n<p>backup_sqlite(&#39;your_database.db&#39;, &#39;backup.sql&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001MySQL\u7684\u5907\u4efd<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>def backup_mysql(db_name, user, password, host, output_file):<\/p>\n<p>    conn = pymysql.connect(host=host, user=user, password=password, db=db_name)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    with open(output_file, &#39;w&#39;) as f:<\/p>\n<p>        for line in cursor.execute(&quot;SHOW TABLES&quot;):<\/p>\n<p>            f.write(&#39;%s\\n&#39; % line)<\/p>\n<p>    conn.close()<\/p>\n<p>backup_mysql(&#39;your_database&#39;, &#39;your_user&#39;, &#39;your_password&#39;, &#39;localhost&#39;, &#39;backup.sql&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001PostgreSQL\u7684\u5907\u4efd<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<p>def backup_postgresql(db_name, user, password, host, output_file):<\/p>\n<p>    conn = psycopg2.connect(database=db_name, user=user, password=password, host=host)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    with open(output_file, &#39;w&#39;) as f:<\/p>\n<p>        for line in cursor.execute(&quot;COPY (SELECT * FROM tablename) TO STDOUT WITH CSV HEADER&quot;):<\/p>\n<p>            f.write(&#39;%s\\n&#39; % line)<\/p>\n<p>    conn.close()<\/p>\n<p>backup_postgresql(&#39;your_database&#39;, &#39;your_user&#39;, &#39;your_password&#39;, &#39;localhost&#39;, &#39;backup.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528ORM\u6846\u67b6\u63d0\u4f9b\u7684\u5907\u4efd\u529f\u80fd<\/h2>\n<\/p>\n<p><h3>1\u3001Django ORM\u7684\u5907\u4efd<\/h3>\n<\/p>\n<p><p>Django\u63d0\u4f9b\u4e86<code>dumpdata<\/code>\u547d\u4ee4\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bfc\u51fa\u6570\u636e\u5e93\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>def backup_django(output_file):<\/p>\n<p>    command = f&quot;python manage.py dumpdata &gt; {output_file}&quot;<\/p>\n<p>    os.system(command)<\/p>\n<p>backup_django(&#39;backup.json&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001SQLAlchemy ORM\u7684\u5907\u4efd<\/h3>\n<\/p>\n<p><p>SQLAlchemy\u6ca1\u6709\u76f4\u63a5\u7684\u5907\u4efd\u547d\u4ee4\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u67e5\u8be2\u6570\u636e\u5e93\u5e76\u5bfc\u51fa\u6570\u636e\u6765\u5b9e\u73b0\u5907\u4efd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine<\/p>\n<p>from sqlalchemy.orm import sessionmaker<\/p>\n<p>def backup_sqlalchemy(connection_string, output_file):<\/p>\n<p>    engine = create_engine(connection_string)<\/p>\n<p>    Session = sessionmaker(bind=engine)<\/p>\n<p>    session = Session()<\/p>\n<p>    with open(output_file, &#39;w&#39;) as f:<\/p>\n<p>        for table in engine.table_names():<\/p>\n<p>            for row in session.execute(f&quot;SELECT * FROM {table}&quot;):<\/p>\n<p>                f.write(&#39;%s\\n&#39; % row)<\/p>\n<p>    session.close()<\/p>\n<p>backup_sqlalchemy(&#39;sqlite:\/\/\/your_database.db&#39;, &#39;backup.sql&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u811a\u672c<\/h2>\n<\/p>\n<p><h3>1\u3001\u81ea\u5b9a\u4e49SQLite\u5907\u4efd\u811a\u672c<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def custom_backup_sqlite(db_file, backup_file):<\/p>\n<p>    conn = sqlite3.connect(db_file)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    with open(backup_file, &#39;w&#39;) as f:<\/p>\n<p>        for line in cursor.execute(&quot;SELECT * FROM sqlite_master&quot;):<\/p>\n<p>            f.write(&#39;%s\\n&#39; % line)<\/p>\n<p>        for line in cursor.execute(&quot;SELECT * FROM sqlite_sequence&quot;):<\/p>\n<p>            f.write(&#39;%s\\n&#39; % line)<\/p>\n<p>    conn.close()<\/p>\n<p>custom_backup_sqlite(&#39;your_database.db&#39;, &#39;custom_backup.sql&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u81ea\u5b9a\u4e49MySQL\u5907\u4efd\u811a\u672c<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>def custom_backup_mysql(db_name, user, password, host, output_file):<\/p>\n<p>    conn = pymysql.connect(host=host, user=user, password=password, db=db_name)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    with open(output_file, &#39;w&#39;) as f:<\/p>\n<p>        for table in cursor.execute(&quot;SHOW TABLES&quot;):<\/p>\n<p>            f.write(&#39;%s\\n&#39; % table)<\/p>\n<p>            for row in cursor.execute(f&quot;SELECT * FROM {table}&quot;):<\/p>\n<p>                f.write(&#39;%s\\n&#39; % row)<\/p>\n<p>    conn.close()<\/p>\n<p>custom_backup_mysql(&#39;your_database&#39;, &#39;your_user&#39;, &#39;your_password&#39;, &#39;localhost&#39;, &#39;custom_backup.sql&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u81ea\u5b9a\u4e49PostgreSQL\u5907\u4efd\u811a\u672c<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<p>def custom_backup_postgresql(db_name, user, password, host, output_file):<\/p>\n<p>    conn = psycopg2.connect(database=db_name, user=user, password=password, host=host)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    with open(output_file, &#39;w&#39;) as f:<\/p>\n<p>        for table in cursor.execute(&quot;SELECT table_name FROM information_schema.tables WHERE table_schema=&#39;public&#39;&quot;):<\/p>\n<p>            f.write(&#39;%s\\n&#39; % table)<\/p>\n<p>            for row in cursor.execute(f&quot;SELECT * FROM {table}&quot;):<\/p>\n<p>                f.write(&#39;%s\\n&#39; % row)<\/p>\n<p>    conn.close()<\/p>\n<p>custom_backup_postgresql(&#39;your_database&#39;, &#39;your_user&#39;, &#39;your_password&#39;, &#39;localhost&#39;, &#39;custom_backup.sql&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u5907\u4efd\u7684\u81ea\u52a8\u5316\u548c\u8c03\u5ea6<\/h2>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528cron\u5b9a\u65f6\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528cron\u5b9a\u65f6\u4efb\u52a1\u6765\u81ea\u52a8\u5316\u5907\u4efd\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u6bcf\u5929\u665a\u4e0a12\u70b9\u5907\u4efd\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u7f16\u8f91cron\u4efb\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">crontab -e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">0 0 * * * \/usr\/bin\/python3 \/path\/to\/your_backup_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528Windows\u4efb\u52a1\u8ba1\u5212<\/h3>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u6765\u81ea\u52a8\u5316\u5907\u4efd\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u6bcf\u5929\u665a\u4e0a12\u70b9\u5907\u4efd\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u3002<\/li>\n<li>\u521b\u5efa\u57fa\u672c\u4efb\u52a1\u3002<\/li>\n<li>\u8bbe\u7f6e\u89e6\u53d1\u5668\u4e3a\u6bcf\u65e5\u3002<\/li>\n<li>\u8bbe\u7f6e\u64cd\u4f5c\u4e3a\u542f\u52a8\u7a0b\u5e8f\uff0c\u9009\u62e9Python\u89e3\u91ca\u5668\u548c\u5907\u4efd\u811a\u672c\u3002<\/li>\n<\/ol>\n<p><h3>3\u3001\u4f7f\u7528Python\u811a\u672c\u8c03\u5ea6<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u8c03\u5ea6\u5e93\uff0c\u5982<code>schedule<\/code>\uff0c\u6765\u5b9e\u73b0\u5b9a\u65f6\u5907\u4efd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import schedule<\/p>\n<p>import time<\/p>\n<p>def backup():<\/p>\n<p>    # \u8c03\u7528\u5907\u4efd\u51fd\u6570<\/p>\n<p>    backup_sqlite(&#39;your_database.db&#39;, &#39;backup.sql&#39;)<\/p>\n<p>schedule.every().day.at(&quot;00:00&quot;).do(backup)<\/p>\n<p>while True:<\/p>\n<p>    schedule.run_pending()<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u5907\u4efd\u6587\u4ef6\u7684\u7ba1\u7406\u548c\u5b58\u50a8<\/h2>\n<\/p>\n<p><h3>1\u3001\u5907\u4efd\u6587\u4ef6\u7684\u547d\u540d<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fbf\u4e8e\u7ba1\u7406\u548c\u67e5\u627e\u5907\u4efd\u6587\u4ef6\uff0c\u53ef\u4ee5\u5728\u5907\u4efd\u6587\u4ef6\u540d\u4e2d\u6dfb\u52a0\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<p>timestamp = datetime.datetime.now().strftime(&#39;%Y%m%d%H%M%S&#39;)<\/p>\n<p>output_file = f&quot;backup_{timestamp}.sql&quot;<\/p>\n<p>backup_sqlite(&#39;your_database.db&#39;, output_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5907\u4efd\u6587\u4ef6\u7684\u5b58\u50a8\u4f4d\u7f6e<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u5907\u4efd\u6587\u4ef6\u5b58\u50a8\u5728\u672c\u5730\u78c1\u76d8\u3001\u5916\u90e8\u5b58\u50a8\u8bbe\u5907\u3001\u4e91\u5b58\u50a8\u7b49\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u672c\u5730\u78c1\u76d8<\/h4>\n<\/p>\n<p><p>\u5c06\u5907\u4efd\u6587\u4ef6\u5b58\u50a8\u5728\u672c\u5730\u78c1\u76d8\u7684\u6307\u5b9a\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">backup_dir = &#39;\/path\/to\/backup\/dir&#39;<\/p>\n<p>output_file = f&quot;{backup_dir}\/backup_{timestamp}.sql&quot;<\/p>\n<p>backup_sqlite(&#39;your_database.db&#39;, output_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5916\u90e8\u5b58\u50a8\u8bbe\u5907<\/h4>\n<\/p>\n<p><p>\u5c06\u5907\u4efd\u6587\u4ef6\u590d\u5236\u5230\u5916\u90e8\u5b58\u50a8\u8bbe\u5907\uff0c\u5982U\u76d8\u3001\u79fb\u52a8\u786c\u76d8\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>external_storage = &#39;\/path\/to\/external\/storage&#39;<\/p>\n<p>shutil.copyfile(output_file, f&quot;{external_storage}\/backup_{timestamp}.sql&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4e91\u5b58\u50a8<\/h4>\n<\/p>\n<p><p>\u5c06\u5907\u4efd\u6587\u4ef6\u4e0a\u4f20\u5230\u4e91\u5b58\u50a8\u670d\u52a1\uff0c\u5982Amazon S3\u3001Google Cloud Storage\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import boto3<\/p>\n<p>def upload_to_s3(file_name, bucket, object_name=None):<\/p>\n<p>    s3_client = boto3.client(&#39;s3&#39;)<\/p>\n<p>    if object_name is None:<\/p>\n<p>        object_name = file_name<\/p>\n<p>    s3_client.upload_file(file_name, bucket, object_name)<\/p>\n<p>upload_to_s3(output_file, &#39;your_bucket_name&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u5907\u4efd\u6587\u4ef6\u7684\u6062\u590d<\/h2>\n<\/p>\n<p><h3>1\u3001\u6062\u590dSQLite\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def restore_sqlite(backup_file, db_file):<\/p>\n<p>    conn = sqlite3.connect(db_file)<\/p>\n<p>    with open(backup_file, &#39;r&#39;) as f:<\/p>\n<p>        sql = f.read()<\/p>\n<p>        conn.executescript(sql)<\/p>\n<p>    conn.close()<\/p>\n<p>restore_sqlite(&#39;backup.sql&#39;, &#39;your_database.db&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u6062\u590dMySQL\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<p>def restore_mysql(backup_file, db_name, user, password, host):<\/p>\n<p>    conn = pymysql.connect(host=host, user=user, password=password, db=db_name)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    with open(backup_file, &#39;r&#39;) as f:<\/p>\n<p>        sql = f.read()<\/p>\n<p>        cursor.execute(sql)<\/p>\n<p>    conn.close()<\/p>\n<p>restore_mysql(&#39;backup.sql&#39;, &#39;your_database&#39;, &#39;your_user&#39;, &#39;your_password&#39;, &#39;localhost&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u6062\u590dPostgreSQL\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import psycopg2<\/p>\n<p>def restore_postgresql(backup_file, db_name, user, password, host):<\/p>\n<p>    conn = psycopg2.connect(database=db_name, user=user, password=password, host=host)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    with open(backup_file, &#39;r&#39;) as f:<\/p>\n<p>        sql = f.read()<\/p>\n<p>        cursor.execute(sql)<\/p>\n<p>    conn.close()<\/p>\n<p>restore_postgresql(&#39;backup.sql&#39;, &#39;your_database&#39;, &#39;your_user&#39;, &#39;your_password&#39;, &#39;localhost&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516b\u3001\u5907\u4efd\u6587\u4ef6\u7684\u5b89\u5168\u6027<\/h2>\n<\/p>\n<p><h3>1\u3001\u52a0\u5bc6\u5907\u4efd\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4fdd\u62a4\u5907\u4efd\u6587\u4ef6\u7684\u5b89\u5168\uff0c\u53ef\u4ee5\u5bf9\u5907\u4efd\u6587\u4ef6\u8fdb\u884c\u52a0\u5bc6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cryptography.fernet import Fernet<\/p>\n<p>def encrypt_file(file_name, key):<\/p>\n<p>    with open(file_name, &#39;rb&#39;) as f:<\/p>\n<p>        data = f.read()<\/p>\n<p>    fernet = Fernet(key)<\/p>\n<p>    encrypted_data = fernet.encrypt(data)<\/p>\n<p>    with open(file_name, &#39;wb&#39;) as f:<\/p>\n<p>        f.write(encrypted_data)<\/p>\n<p>def decrypt_file(file_name, key):<\/p>\n<p>    with open(file_name, &#39;rb&#39;) as f:<\/p>\n<p>        encrypted_data = f.read()<\/p>\n<p>    fernet = Fernet(key)<\/p>\n<p>    data = fernet.decrypt(encrypted_data)<\/p>\n<p>    with open(file_name, &#39;wb&#39;) as f:<\/p>\n<p>        f.write(data)<\/p>\n<p>key = Fernet.generate_key()<\/p>\n<p>encrypt_file(&#39;backup.sql&#39;, key)<\/p>\n<p>decrypt_file(&#39;backup.sql&#39;, key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5907\u4efd\u6587\u4ef6\u7684\u8bbf\u95ee\u63a7\u5236<\/h3>\n<\/p>\n<p><p>\u9650\u5236\u5907\u4efd\u6587\u4ef6\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u786e\u4fdd\u53ea\u6709\u6388\u6743\u4eba\u5458\u80fd\u591f\u8bbf\u95ee\u548c\u64cd\u4f5c\u5907\u4efd\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">chmod 600 backup.sql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e5d\u3001\u5907\u4efd\u7b56\u7565\u7684\u5236\u5b9a<\/h2>\n<\/p>\n<p><h3>1\u3001\u5b8c\u5168\u5907\u4efd<\/h3>\n<\/p>\n<p><p>\u5b8c\u5168\u5907\u4efd\u662f\u5bf9\u6574\u4e2a\u6570\u636e\u5e93\u8fdb\u884c\u5b8c\u6574\u5907\u4efd\uff0c\u9002\u7528\u4e8e\u6570\u636e\u91cf\u8f83\u5c0f\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">backup_sqlite(&#39;your_database.db&#39;, &#39;full_backup.sql&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u589e\u91cf\u5907\u4efd<\/h3>\n<\/p>\n<p><p>\u589e\u91cf\u5907\u4efd\u53ea\u5907\u4efd\u81ea\u4e0a\u6b21\u5b8c\u5168\u5907\u4efd\u6216\u589e\u91cf\u5907\u4efd\u4ee5\u6765\u53d1\u751f\u53d8\u5316\u7684\u6570\u636e\uff0c\u9002\u7528\u4e8e\u6570\u636e\u91cf\u8f83\u5927\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import sqlite3<\/p>\n<p>def incremental_backup(db_file, backup_file, last_backup_time):<\/p>\n<p>    conn = sqlite3.connect(db_file)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    with open(backup_file, &#39;w&#39;) as f:<\/p>\n<p>        for row in cursor.execute(&quot;SELECT * FROM your_table WHERE last_modified &gt; ?&quot;, (last_backup_time,)):<\/p>\n<p>            f.write(&#39;%s\\n&#39; % row)<\/p>\n<p>    conn.close()<\/p>\n<p>last_backup_time = os.path.getmtime(&#39;last_backup.sql&#39;)<\/p>\n<p>incremental_backup(&#39;your_database.db&#39;, &#39;incremental_backup.sql&#39;, last_backup_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u5dee\u5f02\u5907\u4efd<\/h3>\n<\/p>\n<p><p>\u5dee\u5f02\u5907\u4efd\u662f\u5bf9\u81ea\u4e0a\u6b21\u5b8c\u5168\u5907\u4efd\u4ee5\u6765\u53d1\u751f\u53d8\u5316\u7684\u6570\u636e\u8fdb\u884c\u5907\u4efd\uff0c\u9002\u7528\u4e8e\u6570\u636e\u91cf\u8f83\u5927\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import sqlite3<\/p>\n<p>def differential_backup(db_file, backup_file, last_full_backup_time):<\/p>\n<p>    conn = sqlite3.connect(db_file)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    with open(backup_file, &#39;w&#39;) as f:<\/p>\n<p>        for row in cursor.execute(&quot;SELECT * FROM your_table WHERE last_modified &gt; ?&quot;, (last_full_backup_time,)):<\/p>\n<p>            f.write(&#39;%s\\n&#39; % row)<\/p>\n<p>    conn.close()<\/p>\n<p>last_full_backup_time = os.path.getmtime(&#39;full_backup.sql&#39;)<\/p>\n<p>differential_backup(&#39;your_database.db&#39;, &#39;differential_backup.sql&#39;, last_full_backup_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5341\u3001\u5907\u4efd\u7684\u9a8c\u8bc1\u548c\u6d4b\u8bd5<\/h2>\n<\/p>\n<p><h3>1\u3001\u5907\u4efd\u6587\u4ef6\u7684\u5b8c\u6574\u6027\u9a8c\u8bc1<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u5907\u4efd\u6587\u4ef6\u5728\u4f20\u8f93\u548c\u5b58\u50a8\u8fc7\u7a0b\u4e2d\u672a\u88ab\u7be1\u6539\uff0c\u53ef\u4ee5\u4f7f\u7528\u54c8\u5e0c\u6821\u9a8c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import hashlib<\/p>\n<p>def calculate_hash(file_name):<\/p>\n<p>    hash_md5 = hashlib.md5()<\/p>\n<p>    with open(file_name, &#39;rb&#39;) as f:<\/p>\n<p>        for chunk in iter(lambda: f.read(4096), b&quot;&quot;):<\/p>\n<p>            hash_md5.update(chunk)<\/p>\n<p>    return hash_md5.hexdigest()<\/p>\n<p>original_hash = calculate_hash(&#39;backup.sql&#39;)<\/p>\n<h2><strong>\u4f20\u8f93\u6216\u5b58\u50a8\u540e<\/strong><\/h2>\n<p>new_hash = calculate_hash(&#39;backup.sql&#39;)<\/p>\n<p>assert original_hash == new_hash, &quot;Backup file has been modified&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5907\u4efd\u6062\u590d\u7684\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u5b9a\u671f\u6d4b\u8bd5\u5907\u4efd\u6587\u4ef6\u7684\u6062\u590d\u8fc7\u7a0b\uff0c\u786e\u4fdd\u5728\u9700\u8981\u65f6\u80fd\u591f\u6b63\u786e\u6062\u590d\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def test_restore():<\/p>\n<p>    restore_sqlite(&#39;backup.sql&#39;, &#39;test_database.db&#39;)<\/p>\n<p>    # \u9a8c\u8bc1\u6062\u590d\u540e\u7684\u6570\u636e<\/p>\n<p>    conn = sqlite3.connect(&#39;test_database.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;SELECT COUNT(*) FROM your_table&quot;)<\/p>\n<p>    assert cursor.fetchone()[0] == expected_row_count, &quot;Restore f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led&quot;<\/p>\n<p>    conn.close()<\/p>\n<p>test_restore()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5907\u4efd\u662f\u6570\u636e\u5e93\u7ba1\u7406\u4e2d\u81f3\u5173\u91cd\u8981\u7684\u73af\u8282\uff0c\u901a\u8fc7\u4f7f\u7528\u6570\u636e\u5e93\u81ea\u5e26\u7684\u5907\u4efd\u5de5\u5177\u3001\u5229\u7528Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u6267\u884c\u5907\u4efd\u547d\u4ee4\u3001\u4f7f\u7528ORM\u6846\u67b6\u63d0\u4f9b\u7684\u5907\u4efd\u529f\u80fd\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u811a\u672c\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u5e93\u7684\u5907\u4efd\u548c\u6062\u590d\u3002\u4e3a\u4e86\u786e\u4fdd\u5907\u4efd\u7684\u5b89\u5168\u6027\u548c\u53ef\u9760\u6027\uff0c\u5efa\u8bae\u5236\u5b9a\u5408\u7406\u7684\u5907\u4efd\u7b56\u7565\uff0c\u5b9a\u671f\u8fdb\u884c\u5907\u4efd\u6587\u4ef6\u7684\u9a8c\u8bc1\u548c\u6d4b\u8bd5\uff0c\u5e76\u91c7\u53d6\u5fc5\u8981\u7684\u5b89\u5168\u63aa\u65bd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u5907\u4efd\u5de5\u5177\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5e93\u5907\u4efd\u5de5\u5177\u53d6\u51b3\u4e8e\u591a\u4e2a\u56e0\u7d20\uff0c\u5305\u62ec\u4f60\u7684\u6570\u636e\u5e93\u7c7b\u578b\u3001\u5907\u4efd\u9891\u7387\u548c\u6570\u636e\u91cf\u3002\u5bf9\u4e8ePython\u7528\u6237\uff0c\u5e38\u89c1\u7684\u5907\u4efd\u5de5\u5177\u5305\u62ec<code>pg_dump<\/code>\uff08\u7528\u4e8ePostgreSQL\uff09\u3001<code>mysqldump<\/code>\uff08\u7528\u4e8eMySQL\uff09\u7b49\u3002\u786e\u4fdd\u5907\u4efd\u5de5\u5177\u652f\u6301\u4f60\u7684\u6570\u636e\u5e93\u7248\u672c\uff0c\u5e76\u8003\u8651\u662f\u5426\u9700\u8981\u81ea\u52a8\u5316\u5907\u4efd\u8fc7\u7a0b\u4ee5\u53ca\u6613\u7528\u6027\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5b9e\u73b0\u6570\u636e\u5e93\u7684\u5b9a\u65f6\u5907\u4efd\uff1f<\/strong><br \/>\u5b9e\u73b0\u6570\u636e\u5e93\u7684\u5b9a\u65f6\u5907\u4efd\u53ef\u4ee5\u4f7f\u7528<code>cron<\/code>\u4efb\u52a1\u6216Windows\u4efb\u52a1\u8c03\u5ea6\u7a0b\u5e8f\u6765\u5b9a\u671f\u8fd0\u884c\u5907\u4efd\u811a\u672c\u3002\u901a\u8fc7Python\u7f16\u5199\u7684\u811a\u672c\u53ef\u4ee5\u8c03\u7528\u6570\u636e\u5e93\u7684\u5907\u4efd\u547d\u4ee4\u5e76\u5c06\u5907\u4efd\u6587\u4ef6\u5b58\u50a8\u5728\u6307\u5b9a\u4f4d\u7f6e\u3002\u7ed3\u5408Python\u7684<code>schedule<\/code>\u5e93\uff0c\u4e5f\u80fd\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u5b9a\u65f6\u4efb\u52a1\u3002<\/p>\n<p><strong>\u5728\u5907\u4efd\u6570\u636e\u5e93\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u5b89\u5168\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u5b89\u5168\u6027\uff0c\u5907\u4efd\u8fc7\u7a0b\u4e2d\u5e94\u8003\u8651\u4f7f\u7528\u4e8b\u52a1\u6027\u5907\u4efd\uff0c\u786e\u4fdd\u5728\u5907\u4efd\u5b8c\u6210\u540e\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002\u6b64\u5916\uff0c\u5b9a\u671f\u68c0\u67e5\u5907\u4efd\u6587\u4ef6\u7684\u5b8c\u6574\u6027\uff0c\u786e\u4fdd\u80fd\u591f\u987a\u5229\u6062\u590d\u3002\u540c\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u5907\u4efd\u6587\u4ef6\u52a0\u5bc6\u5e76\u5b58\u50a8\u5728\u5b89\u5168\u7684\u4f4d\u7f6e\uff0c\u4ee5\u9632\u6b62\u6570\u636e\u6cc4\u9732\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6570\u636e\u5e93\u5907\u4efd\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6570\u636e\u5e93\u81ea\u5e26\u7684\u5907\u4efd\u5de5\u5177\u3001\u5229\u7528Python\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u6267\u884c\u5907\u4efd\u547d\u4ee4\u3001\u4f7f\u7528O [&hellip;]","protected":false},"author":3,"featured_media":1182963,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182958"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1182958"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182958\/revisions"}],"predecessor-version":[{"id":1182967,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182958\/revisions\/1182967"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1182963"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1182958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1182958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1182958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}