Код: Выделить всё
2024-11-13
Starting restore-database script. This may take several minutes, please wait...
[2024-11-13 08:15:06.688775] Starting database restore
[2024-11-13 08:15:06.688795] Connecting to server
[2024-11-13 08:15:06.714685] Got connection to server
[2024-11-13 08:15:06.985795] Beginning database restore
[2024-11-13 08:15:06.985814] Just restoring
Traceback (most recent call last):
File "
/database_restore_py.py", line 108, in
restore_database(dict, conn)
File "/database_restore_py.py", line 77, in restore_database
cur.execute(sql_command)
^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'sql_command' where it is not associated with a value
Код: Выделить всё
[2024-11-13 14:48:58.991324] Starting database restore
[2024-11-13 14:48:58.991346] Connecting to server
[2024-11-13 14:48:58.999633] Got connection to server
/database_restore_py.py:51: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.
df = pd.read_sql_query(sql, conn)
[2024-11-13 14:48:59.146764] Beginning database restore
[2024-11-13 14:48:59.146842] Just restoring
[2024-11-13 14:48:59.148813] Restoring . This may take up to 15 minutes.
[2024-11-13 15:04:04.425423] Restored user:
(etc...)
[2024-11-13 15:04:05.807346] Database restore complete!
Код: Выделить всё
GNU nano 7.2 /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
# download and update the database
15 8 * * 1-4 root <
/restore-database >> logs/log.txt 2>&1
Код: Выделить всё
echo $(TZ='America/Los_Angeles' date --iso-8601)
echo "Starting restore-database script. This may take several minutes, please wait..."
exec /usr/bin/python3
/database_restore_py.py
Код: Выделить всё
def restore_database(restore_files_dict, conn, restore_all=False):
if not restore_all:
print(f'[{datetime.datetime.now()}] Just restoring Ktcoc')
restore_files_dict = {key:df for key, df in restore_files_dict.items() if 'ktcoc' in key.lower()}
else:
print(f'[{datetime.datetime.now()}] Restoring all of the databases')
for key, df in restore_files_dict.items():
db_name = df.loc[df['FileGroupName']=='PRIMARY','LogicalName'][0]
sql_command = f"USE MASTER RESTORE DATABASE {db_name} FROM DISK = '{key}' WITH "
print(f'[{datetime.datetime.now()}] Restoring {db_name}. This may take up to 15 minutes.')
for ind, row in df.iterrows():
logi_name = row['LogicalName']
file_name = row['PhysicalName'].split('\\')[-1]
sql_command += f"MOVE '{logi_name}' TO '/var/opt/mssql/data/{file_name}', "
sql_command += "REPLACE"
cur = conn.cursor()
cur.execute(sql_command)
Сначала я попытался запустить сценарий bash, который восстановил базу данных, но это не помогло. сработал правильно, затем я попытался запустить скрипт Python напрямую с помощью cron, но он просто пропустил функцию, которая должна была восстановить базу данных. Это самое близкое к желаемому эффекту.
Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/791 ... -fedora-39