( words)
One of the most annoying things about administering a system is when it fails and you have no backup. A simple but effective method of mitigating this risk is to backup to a remote storage facility. Services such as Dropbox / Google Drive ensure that your data is safe and secure. In this tutorial I cover how to set-up a simple backup script to cover the most important files on your system:
Download the Dropbox uploader script:
cd /usr/local/sbin/ wget https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh
Give execution permissions to the script and run it:
chmod +x dropbox_uploader.sh ./dropbox_uploader.sh
Create the backup wrapper script using a text editor of your choosing:
#!/bin/bash # # Copyright 2014 Kieran Brahney <kieran@miami-nice.co.uk> # SERVER_NAME="SERVER-1" # Name of the server to identify backups DROPBOX_EXE="/usr/local/sbin/dropbox_uploader.sh" # location of dropbox uploader DROPBOX_DIR="" # where you want to save it in your Dropbox account BACKUP_SRC="/var/www /home /etc /root" # anything else you can think of that is a critical setting file for you BACKUP_DST="/tmp" MYSQL_SERVER="" MYSQL_USER="" # best to use the root account for this MYSQL_PASS="" # Your msql password MYSQL_EXCL="information_schema" # exclude these DBs from the backup (separated by | no spaces) FTP_SERVER="" # Remote FTP server to backup FTP_USER="" # best to use root for this FTP_PASS="" # your ftp password # # Stop editing here. NOW="Backup-$SERVER_NAME-"$(date +"%Y.%m.%d") DESTFILE="$BACKUP_DST/$NOW.tgz" BACKUP_FILES=$BACKUP_SRC # Backup MySQL (if set) if [ $MYSQL_USER ]; then # Pull the DB mysql -h$MYSQL_SERVER -N -e "show databases like '%';" | grep -v -E "$MYSQL_EXCL" | xargs mysqldump -h$MYSQL_SERVER --databases | bzip2 -c > "$BACKUP_DST/$NOW-Databases.sql.bz2" # Append to backup file list BACKUP_FILES="$BACKUP_FILES $BACKUP_DST/$NOW-Databases.sql.bz2" fi # Backup Remote FTP server (if set) if [ $FTP_USER ]; then # Url encode to escape any special chars e.g. @ symbols FTP_USER="$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$FTP_USER")" FTP_PASS="$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$FTP_PASS")" # Create dir for files mkdir -p "$BACKUP_DST/FTP_BACKUP" # Pull remote FTP files wget -q -P "$BACKUP_DST/FTP_BACKUP" -m ftp://$FTP_USER:$FTP_PASS\@$FTP_SERVER # Append to backup file list BACKUP_FILES="$BACKUP_FILES $BACKUP_DST/FTP_BACKUP" fi # Compress all of the backup files tar cfzP "$DESTFILE" $BACKUP_FILES # Upload to dropbox $DROPBOX_EXE upload "$DESTFILE" "$DROPBOX_DIR" # Clean up rm -rf "$DESTFILE" "$BACKUP_DST/$NOW-Databases.sql.bz2" "$BACKUP_DST/FTP_BACKUP" (find /tmp/du_* -mtime +1 -exec rm {} \;) >& /dev/null # Delete files older than a day # Check for older backups while read -r state size file rest do if [[ $state = "[F]" && $file = "Backup-$SERVER_NAME-"* ]] then if [[ $file != "$NOW.tgz" ]] then $DROPBOX_EXE delete "$file" fi fi done < <($DROPBOX_EXE list /)
Configure the options at the top of the file. If you’re not interested in MySQL or FTP backups you can simply ignore these.
Create a nightly backup:
crontab -e 0 23 * * * /usr/local/sbin/dropbox_backup
Enjoy!
FAQs
How do I stop receiving: “Warning: Using a password on the command line interface can be insecure.”
Create ~/.my.cnf and input your username and password in the below format:
[client]
user="change-me"
password="change-me"
1 Comment
Excellent script!! Works very good. On the portion of the script that checks for older backups on Dropbox, is there a way to keep 7 days instead of just the current backup? Could you please provide a code snippet for that? Thank you very much!