最後更新: 2017-07-31
目錄
- rsnapshot
- Rsync remote backup
- FTP fetch data
- bakrotate.sh
rsnapshot
Setup Cron job
/etc/crontab
# rsnapshot backup 1 1 * * * /root/script/bakscript.sh > /dev/null 2>&1
Local backup
/root/scripts/bakscript.sh
#!/bin/bash # last update: 2016-03-10 _SERVERNAME="????????????" _ADMIN="?????????" _BAKLOCATIONCHK="/backup/BackupDisk.txt" _LASTCHK="/home/samba/LASTCHECK.txt" ############################################ ############################################ echo `date` echo "Server: "$_SERVERNAME function mailtoadmin(){ echo `date` | mail -s "$_SERVERNAME backup fail" $_ADMIN echo "Notice admin backup error" } # for trace the backup is working echo `date` > $_LASTCHK if [ -f $_BAKLOCATIONCHK ] then echo "Backup Start" nice rsnapshot sync && rsnapshot daily || ( mailtoadmin; exit 1 ) echo "Backup Done" fi
# Script Permisson
chmod 700 /root/script/bakscript.sh
Rsync remote backup
# rsync data to remote backup server
mkdir -p /root/scripts/bakscript
touch /root/scripts/bakscript/password.txt
chmod 600 /root/scripts/bakscript/password.txt
echo "??????" > /root/scripts/bakscript/password.txt
/root/scripts/bakscript/bakscript.sh
#!/bin/bash RSYNC=/usr/bin/rsync BACKUPUSER=`hostname` BACKUPHOST=$BACKUPUSER BACKUPSERVER= PASSWORDFILE=/root/scripts/bakscript/password.txt EXCLUDEPATTER='--delete-excluded --exclude=/proc/* --exclude=/dev/* --exclude=/sys/* --exclude=/var/cache/* --exclude=/var/lib/php/session/* --exclude=/tmp/* --exclude=_X/ --exclude=_bak/ --exclude=.vscode-server/ --exclude=var/lib/lxcfs/ --exclude=lost+found/' ############################################################## function backupfolder(){ if [ -d $1 ];then $RSYNC -azvHS \ --bwlimit=3000 \ --delete \ --password-file=$PASSWORDFILE \ $EXCLUDEPATTER \ $1 \ $BACKUPUSER@$BACKUPSERVER::$BACKUPHOST fi } ############################################################## backupfolder /etc; #backupfolder /root; #backupfolder /var/lib/lxc; #backupfolder /var; #backupfolder /home;
chmod 700 /root/scripts/bakscript/bakscript.sh
# Cron Jobs
# daily backup 4 4 * * * /root/scripts/bakscript/bakscript.sh > /var/log/backup.log 2>&1
Notes
mail server 可以 exclude 的
--exclude=dovecot.index --exclude=dovecot.index.cache --exclude=dovecot-uidlist --exclude=dovecot.index.log --exclude=var/spool/amavisd
php 可以 exclude 的
php/session/sess_*
FTP fetch data
#!/bin/bash #### Setting _host=x.x.x.x _user=username _pw=pass _bak_folder="public_html" _MyDate=(Mon Tue Wed Thu Fri Sat Sun) for _mydate in ${_MyDate[@]} do if [ ! -d $_mydate ]; then mkdir $_mydate fi done _today=`date +%a` cd $_today lftp $_user:$_pw@$_host -e "cd $_bak_folder; mirror -v -c --log=log.txt; exit" > /dev/null 2>&1 cd -
bakrotate.sh
#!/bin/bash # create by tim@20170410 #### setting _KEEPCOPY=7 _BACKUPPATH=/backup bakrotate () { local _TMPFILE1=`mktemp` # folder list local _TMPFILE2=`mktemp` # folder need to delete cd $_BACKUPPATH # all folder name start with "bak_" ls -1 -d bak_* | sort -r > $_TMPFILE1 awk "NR > $_KEEPCOPY {print $1}" $_TMPFILE1 > $_TMPFILE2 mkdir _X 2> /dev/null while read line do if [[ $line == 'bak_'* ]]; then mv $line _X else echo "Can not delete $line" fi done < $_TMPFILE2 #### cleanup rm -f $_TMPFILE1 rm -f $_TMPFILE2 } bakrotate