Ubuntu下設定Redmine備份

发布于:2023-07-17 22:4214人浏览
Ubuntu下設定Redmine備份 備份redmine資料夾 編輯用於備份/home/redmine/redmine資料夾的腳本: touch /home/redmine/backup/backup.sh chmod x /home/redmine/backup/backup.sh vim /home/redmine/backup/backup.sh填入以下內容: # backup the folder /home/redmine/redmi…

Ubuntu下設定Redmine備份

備份redmine資料夾

編輯用於備份/home/redmine/redmine資料夾的腳本:

touch /home/redmine/backup/backup.shchmod +x /home/redmine/backup/backup.shvim /home/redmine/backup/backup.sh

填入以下內容:

# backup the folder '/home/redmine/redmine/'echo '<your_passwd>' | sudo -S rsync -av --no-o --no-g --progress --delete --log-file=/home/redmine/backup/$(date +%Y%m%d-%H%M%S)backup.log /home/redmine/redmine /mnt/tmp/Temp/backup/dataBackup-$(date +%Y%m%d-%H%M%S)date +%Y%m%d-%H%M%S >> /home/redmine/backup/backup_time.txt

備份的目的地為/mnt/tmp/Temp/backup/dataBackup-<date-time>

備份時的log會記錄於/home/redmine/backup/<date-time>backup.log

備份的時間點會寫入/home/redmine/backup/backup_time.txt

備份redmine資料庫

/home/redmine/backup.sh中加入以下內容:

# backup databases 'redmine'PGPASSWORD=<your_db_passwd> /usr/bin/pg_dump -U postgres -h localhost -Fc --file=/home/redmine/backup/redmine.sqlc redminegzip -c /home/redmine/backup/redmine.sqlc > /home/redmine/backup/redmine_`date +%Y%m%d-%H%M%S`.sqlc.gzsudo cp -r -f --no-preserve=mode,ownership /home/redmine/backup/*.gz /mnt/tmp/Temp/backup && rm -r /home/redmine/backup/*.gz

redmine資料庫導出壓縮成.sqlc.gz檔,並備份到/mnt/tmp/Temp/backup

設定備份時程

設定備份時程:

crontab -e

以上指令會開啟編輯器,填入以下內容後,儲存並退出:

0 2 * * * /home/redmine/backup/backup.sh >> /home/redmine/backup/backup.log 2>&1 #2am every day

還原redmine資料夾

編輯用於還原/home/redmine/redmine資料夾的腳本:

touch /home/redmine/backup/restore.shchmod +x /home/redmine/backup/restore.shvim /home/redmine/backup/restore.sh

填入以下內容:

echo '<your_passwd>' | sudo -S rsync -av --no-o --no-g --progress --log-file=/home/redmine/backup/$(date +%Y%m%d-%H%M%S)restore.log `find /mnt/tmp/Temp/backup -maxdepth 2 -wholename "/mnt/tmp/Temp/backup/dataBackup*/redmine" -type d -exec stat -c '%n' {} \; | sort -nr  | awk 'NR==1,NR==1 {print $1}'` /home/redmine/sudo chown -R redmine:redmine /home/redmine/redmine

表示將最近備份的redmine資料夾複製到/home/redmine路徑下,並修改其擁有者為redmine。

還原時的log會記錄於/home/redmine/backup/<date-time>restore.log

還原redmine資料庫

/home/redmine/backup/restore.sh中加入以下內容:

echo "<your_passwd>" | sudo -S cp -r -f --no-preserve=mode,ownership `find /mnt/tmp/Temp/backup -maxdepth 1 -name "*.gz" -type f -exec stat -c '%Y %n' {} \; | sort -nr | awk 'NR==1,NR==1 {print $2}'` .gunzip *.gzPGPASSWORD=<your_db_passwd> pg_restore -U postgres -h localhost -d redmine redmine*.sqlc #user is "postgres" for database "redmine", check database.yml!

/mnt/tmp/Temp/backup複製最近備份的.gz檔,將它解壓後導入到redmine資料中。

註:在還原redmine資料庫前需先建立一個名為redmine的空的資料庫。進入psql後:

CREATE DATABASE redmine;

others

find last modified file:

https://unix.stackexchange.com/questions/240418/find-latest-files

find . -maxdepth 1 -name "*.gz" -type f -exec stat -c '%Y %n' {} \; | sort -nr | awk 'NR==1,NR==1 {print $2}'

find last modified folder:

find . -maxdepth 1 -name "dataBackup*" -type d -exec stat -c '%Y %n' {} \; | sort -nr | awk 'NR==1,NR==1 {print $2}'

find folder whose name is largest:

find /mnt/tmp/Temp/backup -maxdepth 2 -wholename "/mnt/tmp/Temp/backup/dataBackup*/redmine" -type d -exec stat -c '%n' {} \; | sort -nr  | awk 'NR==1,NR==1 {print $1}'

參考連結

Scheduled folder backup

Backing up and restoring Redmine

相关文章
    最新文章
    热门标签