How To Remotely Backup A Web Server With No Install
I was researching simple methods for backing up a basic Linux web server, and I found an excellent script that handles files and databases, and also dumps everything to a remote server. I was looking for something a bit less complicated and overkill than Bacula, but more comprehensive than simply archiving the filesystem, and this fills the niche nicely.
Taking this solution one step further, I simplified the install requirements a bit by rewriting the script to run the backup remotely by logging in via SSH and pulling the data directly from the source machine. Instead of having to install the script and cron job on each machine you want to backup, you just install it once on the destination server, and ensure it has SSH access to each target. I also modified it to record a list of all installed packages, which would be useful if the system had to be reconstructed. Intuitively, it seems easier to deal with a single file, so I also wrap all backup parts into a single archive.
To start, you’ll want to ensure the script’s user has SSH access to the target machine by locally running something like:
ssh-copy-id tunneluser@12.345.678.90You’ll also want to ensure it has permission to access all the data it needs to backup. On the target machine, you’d run something like:
sudo /usr/sbin/useradd tunneluser sudo /usr/sbin/usermod -a -G apache tunneluser sudo passwd tunneluser sudo mysql -e "GRANT USAGEG ON *.* TO tunneluser@localhost IDENTIFIED BY 'mysqlpassword';GRANT ALL PRIVILEGES ON *.* TO tunneluser@localhost;"
Finally, the script itself:
#!/bin/bash # Simple script for remotely backing up all data on a webserver. # Adapted from http://www.cyberciti.biz/faq/how-to-back-up-a-web-server/ # Define remote login. SSHUSER="tunneluser" SSHHOST="12.345.678.90" # Define remote and local backup destinations. LOCAL_BACKUP_ROOT="/var/backups" NOW=$(date +"%F") BACKUP_ROOT="/tmp/backup/$NOW" BACKUP_FILENAME="backup-$SSHHOST-$NOW.tar.gz" BACKUP_PATH="$BACKUP_ROOT/$BACKUP_FILENAME" # Run a script on the remote host. ssh -T $SSHUSER@$SSHHOST <<EOI TERM=dumb export TERM # File directories to backup. DIRS="/var/www/html/ /etc" # Paths for binary files TAR="/bin/tar" PGDUMP="/usr/bin/pg_dump" MYSQLDUMP="/usr/bin/mysqldump" GZIP="/bin/gzip" LOGGER="/usr/bin/logger" # Set Pgsql username PGSQLUSER="tunneluser" # Set MySQL username and password MYSQLUSER="tunneluser" MYSQLPASSWORD="mysqlpassword" # Backup file name hostname.time.tar.gz BFILE="web.tar.gz" PFILE="pg.sql.gz" MFILE="mysql.sq.gz" PKGFILE="installed-software.log" # Store todays date. NOW=\$(date +"%F") echo \$NOW # Store backup path. BACKUP_ROOT="$BACKUP_ROOT" [ ! -d \$BACKUP_ROOT ] && mkdir -p \${BACKUP_ROOT} BACKUP_FILENAME="$BACKUP_FILENAME" # Log backup start time in /var/log/messages \$LOGGER "*** Backup started @ \$(date) ***" # Backup package list. echo "Backing up package list..." rpm -qa > \${BACKUP}/\${PKGFILE} # Redhat #dpkg --get-selections > \${BACKUP}/\${PKGFILE} # Debian # Backup webserver directories. echo "Backing up directories..." \$TAR -zcf \${BACKUP_ROOT}/\${BFILE} \${DIRS} # Backup PgSQL. echo "Backing up PgSQL..." \$PGDUMP -x -D -U\${PGSQLUSER} | \$GZIP -c > \${BACKUP_ROOT}/\${PFILE} # Backup MySQL. echo "Backing up MySQL..." \$MYSQLDUMP -u \${MYSQLUSER} -h localhost -p\${MYSQLPASSWORD} --all-databases --lock-all-tables | \$GZIP -9 > \${BACKUP_ROOT}/\${MFILE} # Create final file and cleanup. echo "Cleaning up..." \$TAR -zcf \${BACKUP_FILENAME} \${BACKUP_ROOT}/\${BFILE} \${BACKUP_ROOT}/\${MFILE} \${BACKUP_ROOT}/\${PKGFILE} \${BACKUP_ROOT}/\${PFILE} rm -Rf \${BACKUP_ROOT} # Log backup end time in /var/log/messages \$LOGGER "*** Backup Ended @ \$(date) ***" exit EOI echo "Downloading archive..." [ ! -d $LOCAL_BACKUP_ROOT ] && mkdir -p ${LOCAL_BACKUP_ROOT} scp -rp $SSHUSER@$SSHHOST:$BACKUP_PATH ${LOCAL_BACKUP_ROOT}/${BACKUP_FILENAME} echo "Backup complete."
Filed under: Apache, Linux, MySQL, PostgreSQL
Leave a Reply