編輯用於備份/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/redmine/'
echo '' | 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-
。
備份時的log會記錄於/home/redmine/backup/
。
備份的時間點會寫入/home/redmine/backup/backup_time.txt
。
在/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 redmine
gzip -c /home/redmine/backup/redmine.sqlc > /home/redmine/backup/redmine_`date +%Y%m%d-%H%M%S`.sqlc.gz
sudo 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
編輯用於還原/home/redmine/redmine
資料夾的腳本:
touch /home/redmine/backup/restore.sh
chmod +x /home/redmine/backup/restore.sh
vim /home/redmine/backup/restore.sh
填入以下內容:
echo '' | 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/
。
在/home/redmine/backup/restore.sh
中加入以下內容:
echo "" | 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 *.gz
PGPASSWORD=<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;
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