以下是代码:
#!/bin/bash
# -------------------------------------------------
# -------------------------------------------------
# Use a comma-delimited list of single-quoted
# strings of the usernames to batch
# if it's empty it will backup all user directories
# in /home
# -------------------------------------------------
# -------------------------------------------------
USER_ACCOUNT=();
# -------------------------------------------------
# -------------------------------------------------
# Make sure the aws script is installed on the
# server, and the bucket name to upload these too
# are exact... case-sensitive
# -------------------------------------------------
# -------------------------------------------------
S3BUCKET='Kevs-Bucket/Test';
# Loop through the user array
# If it's empty, then get all users in the /home
# directory, based on each folder
# do not include root user
if [ ${#USER_ACCOUNT[@]} -eq 0 ]; then
# turn off dotglob and nullglob
cd /home;
shopt -s dotglob;
shopt -s nullglob;
DIRARR=(*/);
# we have our directories, now loop them and grab the user
# once we have the user, skip the root user
for d in ${!DIRARR[@]}; do
# Assign an account variable
ACCT=stat -c '%U' ${DIRARR[$i]}; #NOT WORKING HERE
if [ "$ACCT" == "root" ]; then
echo "ROOT";
else
run_backup $ACCT $S3BUCKET;
fi;
done;
else
# we have our list, now loop through them all
for i in ${!USER_ACCOUNT[@]}; do
# Assign an account variable
ACCT=${USER_ACCOUNT[$i]};
run_backup $ACCT $S3BUCKET;
done;
fi;
# -------------------------------------------------
# -------------------------------------------------
# Run the actual backup
run_backup(){
LOGFILE=/batch-move.log
# Package the account
./scripts/pkgacct $1;
echo '##########################################' >> $LOGFILE;
echo "# Start: date +'%T'" >> $LOGFILE;
echo "# Backing Up: $1" >> $LOGFILE;
echo '##########################################' >> $LOGFILE;
# Upload it to S3
s3put $2/cpmove-$1.tar.gz /home/cpmove-$1.tar.gz;
echo '##########################################' >> $LOGFILE;
echo "# Uploading Backup: $1" >> $LOGFILE;
echo '##########################################' >> $LOGFILE;
# Remove the file from the server
/bin/rm -f /home/cpmove-$1.tar.gz;
echo '##########################################' >> $LOGFILE;
echo "# Removing Backup Up: $1" >> $LOGFILE;
echo "# Finish: date +'%T'" >> $LOGFILE;
echo '##########################################' >> $LOGFILE;
}
我在这里收到错误ACCT=stat -c '%U' ${DIRARR[$i]}; #NOT WORKING HERE
并且错误表明-c不是我的CentOS服务器上stat的有效选项
我已通过其他方式验证stat -c
是否有效,因此我假设我的代码尝试将文件夹所有者放入变量中是不正确的。
答案 0 :(得分:1)
不起作用的行(下面)包含您尚未定义的变量$ i且$()表示法不存在,请参阅下面的“EDIT - 试试这个”中添加的代码。
ACCT=stat -c '%U' ${DIRARR[$i]}; #NOT WORKING HERE
您以不寻常的方式循环遍历数组。下面是一个如何在Bash中循环遍历文件名数组元素的示例。
files=( "/home/User/FileName1" "/home/User/FileName2" "/home/User/FileName3" )
for fileName in "${files[@]}" ; do
echo "$fileName"
done
而不是使用globbing来构建你的数组 - DIRARR =(* /); - 您可能需要考虑使用循环来迭代文件,例如:
for fileName in /home/* ; do
echo "$fileName"
done
希望这有帮助。
编辑 - 试试这个:
注意:在我的系统中,以下忽略'。'和'..'。
# To avoid confusion 'ACCT' would be better named as 'OWNER'.
# Loop through the files in: /home/
for filename in /home/* ; do
# Get the owner of $filename.
ACCT=$(stat -c '%U' "$filename")
# If the file is a directory NOT owned by root, run backup.
if [ -d "$filename" -a "$ACCT" != "root" ]; then
# Uncomment when satisfied
# run_backup "$ACCT" "$S3BUCKET"
echo "Run backup - not owned by root:"
echo "filename: $filename owner: $ACCT"
# If the file is a directory owned by root, DO NOT run backup.
elif [ -d "$filename" -a "$ACCT" = "root" ]; then
# Remove elif clause when satisfied.
echo "Do not run backup - owned by root:"
echo "filename: $filename owner: $ACCT"
fi
done
请注意在“ACCT =”行中使用$()。