linux under Vps automatically backup web and mysql database scripts
- 2020-05-06 12:02:10
- OfStack
You have to worry about everything with Vps, and so do backups. Backup services like Linode cost $5 a month. Instead of spending the five dollars, I'd rather feed myself. Dropbox is installed for the service, and the synchronization service is set to start at 4:00-6:00 every day. Two scripts were run to automatically back up the web folder and mysql database.
Both scripts backup the files to /root/Dropbox/backup directory and automatically delete the backup that was 10 days old. An web backup is a weekly backup that removes backups up to 30 days old.
1. Back up WEB folder
1. Backup /home/users/public_html directory
2. Modify crontab to run
at 3:22 on the first day of the week
3. Copy the script to /etc/ cron.weekly directory
4. Modify permission
5. The script is
6. Note that the filename sentence is not a single quote, it is above the tab key,
7. Fill the public_html directory of the user to be backed up into users
Backup
database daily
1. For security, create a new user backup with a strong password (which can be generated automatically). The global permission is select,lock tables
2. Start execution at 3:00 every day and modify crontab
3. Copy the script to /etc/ cron.daily
4. Modify permission
5. Script content
6.
is the same as article 6 above 7. Fill in datas
for the database to be backed up
3. Feel
Since it is all in the us, it is very fast to backup to dropbox. A 10M file is backed up to dropbox.
If traffic is tight, you can also set dropbox to run weekly, every 5 days, etc. Should be required to set the backup cycle, or used in combination.
Both scripts backup the files to /root/Dropbox/backup directory and automatically delete the backup that was 10 days old. An web backup is a weekly backup that removes backups up to 30 days old.
1. Back up WEB folder
1. Backup /home/users/public_html directory
2. Modify crontab to run
at 3:22 on the first day of the week
22 3 * * 0 root run-parts /etc/cron.weekly
3. Copy the script to /etc/ cron.weekly directory
4. Modify permission
chmod 0755 /etc/cron.weekly/webbackup.sh
5. The script is
#!/bin/sh
filename=`date +%Y%m%d`
backup_dir="/root/Dropbox/backup/web"
#delete all files 10 days ago
find $backup_dir -mtime +30 -name "*" -exec rm -rf {} \;
#each user you want backup
users="baidu sina"
for user in ${users}; do
tar zcvf $backup_dir/$user$filename.tar.gz /home/$user/public_html
done
6. Note that the filename sentence is not a single quote, it is above the tab key,
7. Fill the public_html directory of the user to be backed up into users
Backup
database daily
1. For security, create a new user backup with a strong password (which can be generated automatically). The global permission is select,lock tables
2. Start execution at 3:00 every day and modify crontab
3. Copy the script to /etc/ cron.daily
4. Modify permission
5. Script content
#!/bin/sh
dbuser="backup"
dbpassword="youpassword"
datas="db1 db2 db3"
filename=`date +%Y%m%d`
bin_dir="/usr/local/mysql/bin"
backup_dir="/root/Dropbox/backup/mysql"
#delete all files 10 days ago
find $backup_dir -mtime +10 -name "*" -exec rm -rf {} \;
#Do each database backup
for data in ${datas}; do
$bin_dir/mysqldump --opt -u$dbuser -p$dbpassword $data | gzip > $backup_dir/$data$filename.sql.gz
done
6.
is the same as article 6 above 7. Fill in datas
for the database to be backed up
3. Feel
Since it is all in the us, it is very fast to backup to dropbox. A 10M file is backed up to dropbox.
If traffic is tight, you can also set dropbox to run weekly, every 5 days, etc. Should be required to set the backup cycle, or used in combination.