我想运行一个cron作业,每天从hostgator cpanel备份我的mysql数据库和我的wordpress文件。
我找到了一个示例脚本并使用我的信息编辑了参数,但它似乎没有正常工作。我对cron工作没有太多经验,所以我不确定我的所有问题是什么。
我的.sh文件名为backups.sh,保存在主目录中,并且有一个名为backups with folder with subfolders database and wordpress。
这是.sh文件。我用我的凭据替换了{my info},并为我的数据库信息填写了所有内容:
#!/bin/bash
# Script Function:
# This bash script backups up the db everyday dependent on
# when you set the cron job to run with a file name time stamp
# and tar.gz zips the file.
# The db will be saved in /backups/database/
# Db backups older than 5 days will be deleted.\
#[Changes Directory]
cd /home/{my info}/backups/
#[Old DB Deletion and Files Script]
find /home/{my info}/backups/database -name "*.tar.gz" -mtime +5 -exec rm -f {};
find /home/{my info}/backups/wordpress -name "*.tar.gz" -mtime +5 -exec rm -f {};
#[Stamps the file name with a date]
TIMESTAMP=`date +%m-%d-%y-%H%M`
#[DB Backup Scripts]
# DB Name
HOST=localhost
DBNAME=""
USER=""
PASSWORD=""
DUMP_PATH=/home/{my info}/backups/database/
BACK_PATH=/home/{my info}/backups/wordpress/
mysqldump --opt -c -e -Q -h$HOST -u$USER -p$PASSWORD $DBNAME > $DBNAME.sql
tar czpf $DUMP_PATH/$DBNAME.$TIMESTAMP.tar.gz $DBNAME.sql
rm -f $DBNAME.sql
#Backing up Wordpress files @ root
tar czf $BACK_PATH/wordpress.$TIMESTAMP.tar.gz /home/{my info}/public_html/mydomain
以下是命令行的内容:
/bin/sh ~/backups.sh
我在cron工作执行后收到一封电子邮件,它通知我说, “没有这样的文件或目录”,还有一堆未找到的命令。
答案 0 :(得分:1)
cron
并不总是与您的登录shell具有相同的PATH
变量,因此无法始终找到mysqldump
甚至tar
等内容的可执行文件。
我建议使用每个命令的完整路径,即:/usr/bin/mysqldump
而不是mysqldump
。您可以使用which
命令轻松确定路径。
$ which mysqldump
/usr/bin/mysqldump
答案 1 :(得分:0)
您可以使用下面的bash脚本来备份wordpress网站和数据库。详细信息在这里-> https://www.easyaslinux.com/tutorials/how-to-periodically-backup-your-website-and-mysql-database-to-local-server-as-well-as-s3-bucket-using-bash-script/
#!/bin/bash
###################################
backup_dir="give_directory_here"
webapp_path="website_patj"
database_name="testdb"
database_user="dbuser"
database_pwd="password"
database_host="db_host_name.net"
retention_days=10
##################################
date=`date +%d-%m-%y`
path="$backup_dir$date"
echo $date
mkdir -p $path > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "-Successfully created directory $path"
mysqldump -u $database_user -p$database_pwd -h $database_host $database_name > $path/db_backup.sql
if [ $? -eq 0 ]; then
echo "-Successfully created database dump"
tar -czvf $path/backup_with_db.tar.gz $webapp_path $path/db_backup.sql > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "-Successfully completed file + db backup process"
rm -rf $path/db_backup.sql
old_date=`date --date="$retention_days day ago" +%d-%m-%y`
old_path="$backup_dir$old_date"
ls $old_path > /dev/null 2>&1
if [ $? -eq 0 ]; then
rm -rf $old_path > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "-Sucessfully removed old backup on $old_date"
else
echo "-Failed old backup removal $old_path" && exit 1
fi
fi
else
echo "-Failed file +db backup process" && exit 1
fi
else
echo "-Failed creating database dump, backup process failed" && exit 1
fi
else
echo "-Failed creating directory $path, backup process failed" && exit 1
fi