我想在本地然后远程同步文件夹
看来我陷入了循环并填满了本地服务器。原始文件夹有3.9GB,我在本地服务器上有超过17GB
我想要做的就是在本地 rsync 文件夹作为备份,然后将原始文件夹 rsync 到另一个备份服务器。我看不出哪里出了问题。请帮忙!
以下是整个脚本;
#!/bin/bash
# check that BACKUPDIR exists
BACKUPDIR="/home/deploy/backups"
if [ ! -d $BACKUPDIR ]; then
mkdir -p $BACKUPDIR/{directories,databases,logs}
else
:
fi
# set time variable
NOW=$(date +"%F_%H:%M") # year-month-day_hour:minute format
# set logs
LOCALLOG="$BACKUPDIR/logs/$NOW.webapps.log"
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$LOCALLOG 2>&1
# Everything below will go to the file 'webapps.log'
# Remove files older than 7 days
find $BACKUPDIR/{directories,databases,logs} -mtime +8 -exec rm {} \;
# set directory variables
LOCALDIR="$BACKUPDIR/directories"
BKUP_SERV="[email protected]"
BKUP_DIR="/home/deploy/backups/$HOSTNAME/directories"
BKUP_LOG="/home/deploy/backups/$HOSTNAME/logs"
DJANGODIR="/usr/local/django"
WEBAPPSDIR="/webapps"
# set output variables
WEBAPPS_YES="SUCCESSFULL sync of webapps folder"
WEBAPPS_NO="FAILED to sync webapps folder"
RSYNC_YES="SUCCESSFULL rsync to log file"
RSYNC_NO="FAILED to rsync log file"
# check webapps or django folder to rsync
if [ ! -d "$WEBAPPSDIR" ]; then
rsync -avh "$DJANGODIR" "$LOCALDIR"
else
rsync -avh "$WEBAPPSDIR" "$LOCALDIR"
fi
RESULT1="$?"
# Outputs whether the rsync was successful or not
if [ "$RESULT1" != "0" ]; then
echo -e "EXIT Code:" $RESULT1 "\n$WEBAPPS_NO"
else
echo "$WEBAPPS_YES"
fi
# check webapps or django folder to rsync
if [ ! -d "$WEBAPPSDIR" ]; then
rsync -azvPh "$DJANGODIR" -e ssh "$BKUP_SERV":"$BKUP_DIR"
else
rsync -avzPh "$WEBAPPSDIR" -e ssh "$BKUP_SERV":"$BKUP_DIR"
fi
RESULT2="$?"
# Outputs whether the rsync was successful or not
if [ "$RESULT2" != "0" ]; then
echo -e "EXIT Code:" $RESULT2 "\n$RSYNC_NO"
else
echo "$RSYNC_YES"
fi
# Command to rsync 'webapps.log'
rsync -azvPh "$LOCALLOG" -e ssh "$BKUP_SERV":"$BKUP_LOG"
RESULT3="$?"
# Outputs whether the rsync was successful or not
if [ "$RESULT3" != "0" ]; then
echo -e "EXIT Code:" $RESULT3 "\n$RSYNC_NO"
else
echo "$RSYNC_YES"
fi
答案1
如果您的变量$DJANGODIR
和$LOCALDIR
是有效的目录,并且$BKUP_SERV
变量是有效的目标主机,那么命令是合理的。
您可以使用该--dry-run
参数来查看会发生什么。
您应该引用变量"$DJANGODIR"
等,以便保护空格和其他奇怪的字符免受 shell 的处理。
本地备份将忽略该-z
标志和隐含的--partial
设置。
仅当本地备份成功完成时才会尝试远程备份。您应该考虑这是否正确且有用。就我个人而言,我认为我不应该希望一个备份依赖于另一个备份,但这不是我的系统。
答案2
问题似乎是;
# Remove files older than 7 days
find $BACKUPDIR/{directories,databases,logs} -mtime +8 -exec rm {} \;
-rf
后面的就不见了rm
。这似乎导致了脚本的挂起。
现在效果很好。这是下面的完整版本;
#!/bin/bash
# Set local directory variables
NOW=$(date +"%F_%H:%M") # year-month-day_hour:minute format
BACKUPDIR="/home/deploy/backups"
LOGDIR="$BACKUPDIR/logs"
DBDIR="$BACKUPDIR/databases"
LOGFILE="$LOGDIR/$NOW.db_bkup.log"
# check that BACKUPDIR exists
if [ ! -d $BACKUPDIR ]; then
mkdir -p $BACKUPDIR/{databases,logs}
else
:
fi
# Set remote directory variables
BKUPSSH="[email protected]"
BKUPSERVDIR="/home/deploy/backups/$HOSTNAME/databases"
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$LOGFILE 2>&1
# Everything below will go to the file 'db_bkup.log'
# DO NOT BACKUP these databases
IGNOREDB="
information_schema
performance_schema
mysql
test"
#* MySQL binaries *#
MYSQLDUMP=$(which mysqldump)
GZIP=$(which gzip)
# Remove files older than 7 days
find $DBDIR -mtime +8 -exec rm -rf {} \;
# get all database listing
DBS="$(mysql --login-path=dbbkup -Bse 'show databases')"
# start to dump database one by one
for db in $DBS
do
DUMP="yes";
if [ "$IGNOREDB" != "" ]; then
for i in $IGNOREDB # Store all value of $IGNOREDB ON i
do
if [ "$db" == "$i" ]; then # If result of $DBS(db) is equal to $IGNOREDB(i) then
DUMP="NO"; # SET value of DUMP to "no"
#echo "$i database is being ignored!";
fi
done
fi
if [ "$DUMP" == "yes" ]; then # If value of DUMP is "yes" then backup database
FILE="$DBDIR/$NOW-$db.sql.gz";
echo "BACKING UP $db";
$MYSQLDUMP --login-path=dbbkup --add-drop-database --single-transaction --triggers --routines --events --set-gtid-purged=OFF --verbose $db | $GZIP > $FILE
fi
done
# change permissions on files
chmod -R 755 $BACKUPDIR
# rsync backup to 'Larry' the backup server and append the log file
rsync -azvh $DBDIR/ -e ssh $BKUPSSH:$BKUPSERVDIR
RESULT="$?"
# check result of rsync db's
if [ "$RESULT" != "0" ]; then
echo -e "rsync exit Code:" $RESULT "\nFAILED to rsync databases"
else
echo "SUCCESSFULL rsync of databases"
fi