使用脚本注意事项:
-
所有的参数信息需要改成自己环境的信息。
-
默认是通过MaxKB安装包一键安装。
-
根据需求选择合适的脚本。
一、备份数据 {#%E4%B8%80%E3%80%81%E5%A4%87%E4%BB%BD%E6%95%B0%E6%8D%AE}
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| shell #!/bin/bash 定义变量 ==== BACKUP_DIR="/opt/db_bak" # 备份文件存储目录 DATE=$(date +%Y%m%d_%H%M%S) # 创建时间戳 DB_NAME="maxkb" # 数据库名 DB_USER="root" # 数据库用户 DB_PASS="Password123@postgres" # 数据库密码 DB_HOST="10.1.12.103" # 数据库主机地址,默认为localhost 创建备份文件名 ======= BACKUP_FILE="$BACKUP_DIR/$DB_NAME-$DATE.dump" 检查备份目录是否存在,如果不存在则创建 =================== if \[ ! -d "$BACKUP_DIR" \]; then mkdir -p "$BACKUP_DIR" fi 执行备份 ==== echo "Starting backup of $DB_NAME..." docker exec -i -e PGPASSWORD="Password123@postgres" pgsql pg_dump -h $DB_HOST -U $DB_USER -d maxkb -Fc \> $BACKUP_FILE 检查备份是否成功 ======== `if [ $? -eq 0 ]; then echo "Backup completed successfully." else echo "Backup failed." exit 1 fi`
|
|---------------------------------------------------------------------|
| shell # 授权 chmod 755 maxkb_backup.sh 执行 === `bash backup.sh`
|
二、恢复数据 {#%E4%BA%8C%E3%80%81%E6%81%A2%E5%A4%8D%E6%95%B0%E6%8D%AE}
恢复数据时可以直接在容器中手动执行脚本,也可封装在脚本中一键执行。
1、复制数据到容器中 {#1%E3%80%81%E5%A4%8D%E5%88%B6%E6%95%B0%E6%8D%AE%E5%88%B0%E5%AE%B9%E5%99%A8%E4%B8%AD}
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| shell # maxkb-20240805_184020.dump 为需要恢复的文件名 docker cp /opt/db_bak/maxkb-20240805_184020.dump pgsql:/ 进入pgsql容器 ========= docker exec -it pgsql sh 恢复数据,test为新库名 ============= `pg_restore -U root -d test -v /maxkb-20240805_184020.dump`
|
2、通过脚本执行 {#2%E3%80%81%E9%80%9A%E8%BF%87%E8%84%9A%E6%9C%AC%E6%89%A7%E8%A1%8C}
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| shell !/bin/bash 定义变量 ==== RESTORE_FILE="/opt/db_bak/maxkb-20240805_184020.dump" # 备份文件路径,需要手动改成需要恢复的数据文件 DB_NAME="mydatabase" # 要恢复的数据库名 DB_USER="root" # 数据库用户 DB_PASS="Password123@postgres" # 数据库密码 DB_HOST="10.1.12.103" # 数据库主机地址,默认为localhost 检查备份文件是否存在 ========== if \[ ! -f "$RESTORE_FILE" \]; then echo "Restore file does not exist: $RESTORE_FILE" exit 1 fi 执行恢复 ==== echo "Starting restore of $DB_NAME from $RESTORE_FILE..." docker cp $RESTORE_FILE pgsql:/ docker exec -i -e PGPASSWORD="Password123@postgres" pgsql pg_restore -h $DB_HOST -U $DB_USER -d $DB_NAME -v /maxkb-20240805_184020.dump 检查恢复是否成功 ======== `if [ $? -eq 0 ]; then echo "Restore completed successfully." else echo "Restore failed." exit 1 fi`
|
|-----------------------------------------------------------------------------|
| shell # 授权 chmod 755 maxkb_restore.sh 执行 === `bash maxkb_restore.sh`
|
三、定时备份 {#%E4%B8%89%E3%80%81%E5%AE%9A%E6%97%B6%E5%A4%87%E4%BB%BD}
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| shell vi install_maxkb_backup.sh 内容如下--------- ------------- #!/bin/bash timedate_fields="0 1 \* \* \*" #每天凌晨1:00执行备份程序 cmd="bash /opt/db_bak/maxkb_backup.sh" #替换 ms_backup.sh 所在路径 crontab -l \| grep "$cmd " \> /dev/null 2\>\&1 if test $? -ne 0; then crontab -l \> crontab.tmp echo "$timedate_fields $cmd" \>\> crontab.tmp crontab crontab.tmp fi ----------- ----------- `chmod 755 install_maxkb_backup.sh bash install_maxkb_backup.sh`
|
注:根据实际情况对备份时间、路径进行修改。